mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 22:26:47 +00:00
Limit stdout log level to info (#1518)
* Rename debuglevel to loglevel * Limit stdout level to info by default Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com> Co-authored-by: Svarog <feanorr@gmail.com>
This commit is contained in:
parent
171deded4e
commit
7c3beb526e
@ -102,7 +102,7 @@ type Flags struct {
|
|||||||
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
|
ProxyPass string `long:"proxypass" default-mask:"-" description:"Password for proxy server"`
|
||||||
DbType string `long:"dbtype" description:"Database backend to use for the Block DAG"`
|
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"`
|
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 <subsystem>=<level>,<subsystem2>=<level>,... 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 <subsystem>=<level>,<subsystem2>=<level>,... 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"`
|
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."`
|
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"`
|
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 {
|
func defaultFlags() *Flags {
|
||||||
return &Flags{
|
return &Flags{
|
||||||
ConfigFile: defaultConfigFile,
|
ConfigFile: defaultConfigFile,
|
||||||
DebugLevel: defaultLogLevel,
|
LogLevel: defaultLogLevel,
|
||||||
TargetOutboundPeers: defaultTargetOutboundPeers,
|
TargetOutboundPeers: defaultTargetOutboundPeers,
|
||||||
MaxInboundPeers: defaultMaxInboundPeers,
|
MaxInboundPeers: defaultMaxInboundPeers,
|
||||||
BanDuration: defaultBanDuration,
|
BanDuration: defaultBanDuration,
|
||||||
@ -326,7 +326,7 @@ func LoadConfig() (*Config, error) {
|
|||||||
cfg.LogDir = filepath.Join(cfg.LogDir, cfg.NetParams().Name)
|
cfg.LogDir = filepath.Join(cfg.LogDir, cfg.NetParams().Name)
|
||||||
|
|
||||||
// Special show command to list supported subsystems and exit.
|
// Special show command to list supported subsystems and exit.
|
||||||
if cfg.DebugLevel == "show" {
|
if cfg.LogLevel == "show" {
|
||||||
fmt.Println("Supported subsystems", logger.SupportedSubsystems())
|
fmt.Println("Supported subsystems", logger.SupportedSubsystems())
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
@ -336,7 +336,7 @@ func LoadConfig() (*Config, error) {
|
|||||||
logger.InitLog(filepath.Join(cfg.LogDir, defaultLogFilename), filepath.Join(cfg.LogDir, defaultErrLogFilename))
|
logger.InitLog(filepath.Join(cfg.LogDir, defaultLogFilename), filepath.Join(cfg.LogDir, defaultErrLogFilename))
|
||||||
|
|
||||||
// Parse, validate, and set debug log level(s).
|
// 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())
|
err := errors.Errorf("%s: %s", funcName, err.Error())
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
fmt.Fprintln(os.Stderr, usageMessage)
|
fmt.Fprintln(os.Stderr, usageMessage)
|
||||||
|
@ -223,9 +223,9 @@
|
|||||||
; Debug logging level.
|
; Debug logging level.
|
||||||
; Valid levels are {trace, debug, info, warn, error, critical}
|
; Valid levels are {trace, debug, info, warn, error, critical}
|
||||||
; You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set
|
; You may also specify <subsystem>=<level>,<subsystem2>=<level>,... 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.
|
; available subsystems.
|
||||||
; debuglevel=info
|
; loglevel=info
|
||||||
|
|
||||||
; The port used to listen for HTTP profile requests. The profile server will
|
; 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
|
; be disabled if this option is not specified. The profile information can be
|
||||||
|
@ -227,9 +227,9 @@ var sampleConfig = `[Application Options]
|
|||||||
; Debug logging level.
|
; Debug logging level.
|
||||||
; Valid levels are {trace, debug, info, warn, error, critical}
|
; Valid levels are {trace, debug, info, warn, error, critical}
|
||||||
; You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set
|
; You may also specify <subsystem>=<level>,<subsystem2>=<level>,... 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.
|
; available subsystems.
|
||||||
; debuglevel=info
|
; loglevel=info
|
||||||
|
|
||||||
; The port used to listen for HTTP profile requests. The profile server will
|
; 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
|
; be disabled if this option is not specified. The profile information can be
|
||||||
|
@ -236,28 +236,28 @@ func Get(tag string) (logger *Logger, ok bool) {
|
|||||||
return
|
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
|
// the levels accordingly. An appropriate error is returned if anything is
|
||||||
// invalid.
|
// invalid.
|
||||||
func ParseAndSetDebugLevels(debugLevel string) error {
|
func ParseAndSetLogLevels(logLevel string) error {
|
||||||
// When the specified string doesn't have any delimters, treat it as
|
// When the specified string doesn't have any delimters, treat it as
|
||||||
// the log level for all subsystems.
|
// the log level for all subsystems.
|
||||||
if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") {
|
if !strings.Contains(logLevel, ",") && !strings.Contains(logLevel, "=") {
|
||||||
// Validate debug log level.
|
// Validate debug log level.
|
||||||
if !validLogLevel(debugLevel) {
|
if !validLogLevel(logLevel) {
|
||||||
str := "The specified debug level [%s] is invalid"
|
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.
|
// Change the logging level for all subsystems.
|
||||||
SetLogLevels(debugLevel)
|
SetLogLevels(logLevel)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the specified string into subsystem/level pairs while detecting
|
// Split the specified string into subsystem/level pairs while detecting
|
||||||
// issues and update the log levels accordingly.
|
// issues and update the log levels accordingly.
|
||||||
for _, logLevelPair := range strings.Split(debugLevel, ",") {
|
for _, logLevelPair := range strings.Split(logLevel, ",") {
|
||||||
if !strings.Contains(logLevelPair, "=") {
|
if !strings.Contains(logLevelPair, "=") {
|
||||||
str := "The specified debug level contains an invalid " +
|
str := "The specified debug level contains an invalid " +
|
||||||
"subsystem/level pair [%s]"
|
"subsystem/level pair [%s]"
|
||||||
|
@ -129,7 +129,7 @@ func (l Level) String() string {
|
|||||||
|
|
||||||
// NewBackend creates a new logger backend.
|
// NewBackend creates a new logger backend.
|
||||||
func NewBackend(opts ...BackendOption) *Backend {
|
func NewBackend(opts ...BackendOption) *Backend {
|
||||||
b := &Backend{flag: defaultFlags, toStdout: true}
|
b := &Backend{flag: defaultFlags, stdoutLevel: LevelInfo}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(b)
|
o(b)
|
||||||
}
|
}
|
||||||
@ -145,10 +145,10 @@ type backendLogRotator struct {
|
|||||||
// the backend's Writer. Backend provides atomic writes to the Writer from all
|
// the backend's Writer. Backend provides atomic writes to the Writer from all
|
||||||
// subsystems.
|
// subsystems.
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
rotators []*backendLogRotator
|
rotators []*backendLogRotator
|
||||||
mu sync.Mutex // ensures atomic writes
|
mu sync.Mutex // ensures atomic writes
|
||||||
flag uint32
|
flag uint32
|
||||||
toStdout bool
|
stdoutLevel Level
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackendOption is a function used to modify the behavior of a Backend.
|
// 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) {
|
func (b *Backend) write(lvl Level, bytesToWrite []byte) {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
defer b.mu.Unlock()
|
defer b.mu.Unlock()
|
||||||
if b.toStdout {
|
if lvl >= b.StdoutLevel() {
|
||||||
os.Stdout.Write(bytesToWrite)
|
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
|
// Close finalizes all log rotators for this backend
|
||||||
func (b *Backend) Close() {
|
func (b *Backend) Close() {
|
||||||
for _, r := range b.rotators {
|
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
|
// 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
|
// Backend b. A tag describes the subsystem and is included in all log
|
||||||
// messages. The logger uses the info verbosity level by default.
|
// messages. The logger uses the info verbosity level by default.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user