diff --git a/infrastructure/config/config.go b/infrastructure/config/config.go index 09dc35925..268d0937b 100644 --- a/infrastructure/config/config.go +++ b/infrastructure/config/config.go @@ -102,7 +102,7 @@ type Flags struct { ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"` DbType string `long:"dbtype" description:"Database backend to use for the Block DAG"` Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` - DebugLevel string `short:"d" long:"debuglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify =,=,... to set the log level for individual subsystems -- Use show to list available subsystems"` + LogLevel string `short:"d" long:"loglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify =,=,... to set the log level for individual subsystems -- Use show to list available subsystems"` Upnp bool `long:"upnp" description:"Use UPnP to map our listening port outside of NAT"` MinRelayTxFee float64 `long:"minrelaytxfee" description:"The minimum transaction fee in KAS/kB to be considered a non-zero fee."` MaxOrphanTxs int `long:"maxorphantx" description:"Max number of orphan transactions to keep in memory"` @@ -166,7 +166,7 @@ func newConfigParser(cfgFlags *Flags, options flags.Options) *flags.Parser { func defaultFlags() *Flags { return &Flags{ ConfigFile: defaultConfigFile, - DebugLevel: defaultLogLevel, + LogLevel: defaultLogLevel, TargetOutboundPeers: defaultTargetOutboundPeers, MaxInboundPeers: defaultMaxInboundPeers, BanDuration: defaultBanDuration, @@ -326,7 +326,7 @@ func LoadConfig() (*Config, error) { cfg.LogDir = filepath.Join(cfg.LogDir, cfg.NetParams().Name) // Special show command to list supported subsystems and exit. - if cfg.DebugLevel == "show" { + if cfg.LogLevel == "show" { fmt.Println("Supported subsystems", logger.SupportedSubsystems()) os.Exit(0) } @@ -336,7 +336,7 @@ func LoadConfig() (*Config, error) { logger.InitLog(filepath.Join(cfg.LogDir, defaultLogFilename), filepath.Join(cfg.LogDir, defaultErrLogFilename)) // Parse, validate, and set debug log level(s). - if err := logger.ParseAndSetDebugLevels(cfg.DebugLevel); err != nil { + if err := logger.ParseAndSetLogLevels(cfg.LogLevel); err != nil { err := errors.Errorf("%s: %s", funcName, err.Error()) fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, usageMessage) diff --git a/infrastructure/config/sample-kaspad.conf b/infrastructure/config/sample-kaspad.conf index b0116484f..589da2039 100644 --- a/infrastructure/config/sample-kaspad.conf +++ b/infrastructure/config/sample-kaspad.conf @@ -223,9 +223,9 @@ ; Debug logging level. ; Valid levels are {trace, debug, info, warn, error, critical} ; You may also specify =,=,... to set -; log level for individual subsystems. Use kaspad --debuglevel=show to list +; log level for individual subsystems. Use kaspad --loglevel=show to list ; available subsystems. -; debuglevel=info +; loglevel=info ; The port used to listen for HTTP profile requests. The profile server will ; be disabled if this option is not specified. The profile information can be diff --git a/infrastructure/config/sample_config.go b/infrastructure/config/sample_config.go index a3c0b0ff6..19dfe17ed 100644 --- a/infrastructure/config/sample_config.go +++ b/infrastructure/config/sample_config.go @@ -227,9 +227,9 @@ var sampleConfig = `[Application Options] ; Debug logging level. ; Valid levels are {trace, debug, info, warn, error, critical} ; You may also specify =,=,... to set -; log level for individual subsystems. Use kaspad --debuglevel=show to list +; log level for individual subsystems. Use kaspad --loglevel=show to list ; available subsystems. -; debuglevel=info +; loglevel=info ; The port used to listen for HTTP profile requests. The profile server will ; be disabled if this option is not specified. The profile information can be diff --git a/infrastructure/logger/logger.go b/infrastructure/logger/logger.go index fc5c4a8ed..dc17a76ad 100644 --- a/infrastructure/logger/logger.go +++ b/infrastructure/logger/logger.go @@ -236,28 +236,28 @@ func Get(tag string) (logger *Logger, ok bool) { return } -// ParseAndSetDebugLevels attempts to parse the specified debug level and set +// ParseAndSetLogLevels attempts to parse the specified debug level and set // the levels accordingly. An appropriate error is returned if anything is // invalid. -func ParseAndSetDebugLevels(debugLevel string) error { +func ParseAndSetLogLevels(logLevel string) error { // When the specified string doesn't have any delimters, treat it as // the log level for all subsystems. - if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") { + if !strings.Contains(logLevel, ",") && !strings.Contains(logLevel, "=") { // Validate debug log level. - if !validLogLevel(debugLevel) { + if !validLogLevel(logLevel) { str := "The specified debug level [%s] is invalid" - return errors.Errorf(str, debugLevel) + return errors.Errorf(str, logLevel) } // Change the logging level for all subsystems. - SetLogLevels(debugLevel) + SetLogLevels(logLevel) return nil } // Split the specified string into subsystem/level pairs while detecting // issues and update the log levels accordingly. - for _, logLevelPair := range strings.Split(debugLevel, ",") { + for _, logLevelPair := range strings.Split(logLevel, ",") { if !strings.Contains(logLevelPair, "=") { str := "The specified debug level contains an invalid " + "subsystem/level pair [%s]" diff --git a/infrastructure/logger/logs.go b/infrastructure/logger/logs.go index a8bf4801d..ca6409522 100644 --- a/infrastructure/logger/logs.go +++ b/infrastructure/logger/logs.go @@ -129,7 +129,7 @@ func (l Level) String() string { // NewBackend creates a new logger backend. func NewBackend(opts ...BackendOption) *Backend { - b := &Backend{flag: defaultFlags, toStdout: true} + b := &Backend{flag: defaultFlags, stdoutLevel: LevelInfo} for _, o := range opts { o(b) } @@ -145,10 +145,10 @@ type backendLogRotator struct { // the backend's Writer. Backend provides atomic writes to the Writer from all // subsystems. type Backend struct { - rotators []*backendLogRotator - mu sync.Mutex // ensures atomic writes - flag uint32 - toStdout bool + rotators []*backendLogRotator + mu sync.Mutex // ensures atomic writes + flag uint32 + stdoutLevel Level } // BackendOption is a function used to modify the behavior of a Backend. @@ -352,7 +352,7 @@ func (b *Backend) printf(lvl Level, tag string, format string, args ...interface func (b *Backend) write(lvl Level, bytesToWrite []byte) { b.mu.Lock() defer b.mu.Unlock() - if b.toStdout { + if lvl >= b.StdoutLevel() { os.Stdout.Write(bytesToWrite) } @@ -363,6 +363,16 @@ func (b *Backend) write(lvl Level, bytesToWrite []byte) { } } +// StdoutLevel returns the current stdout logging level +func (b *Backend) StdoutLevel() Level { + return Level(atomic.LoadUint32((*uint32)(&b.stdoutLevel))) +} + +// SetStdoutLevel changes the logging level to the passed level. +func (b *Backend) SetStdoutLevel(level Level) { + atomic.StoreUint32((*uint32)(&b.stdoutLevel), uint32(level)) +} + // Close finalizes all log rotators for this backend func (b *Backend) Close() { for _, r := range b.rotators { @@ -370,11 +380,6 @@ func (b *Backend) Close() { } } -// WriteToStdout sets if the backend will print to stdout or not (default: true) -func (b *Backend) WriteToStdout(stdout bool) { - b.toStdout = stdout -} - // Logger returns a new logger for a particular subsystem that writes to the // Backend b. A tag describes the subsystem and is included in all log // messages. The logger uses the info verbosity level by default.