Browse Source

build/log: support parsing global+subsystem levels

This makes it possible to specify both a global+subsystem loglevels,
like:
--debuglevel=debug,PEER=info,SRVR=trace
master
Johan T. Halseth 4 years ago
parent
commit
c1d423dc07
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
  1. 28
      build/log.go
  2. 14
      build/log_test.go

28
build/log.go

@ -122,24 +122,32 @@ type LeveledSubLogger interface {
// the levels accordingly on the given logger. An appropriate error is returned
// if anything is invalid.
func ParseAndSetDebugLevels(level string, logger LeveledSubLogger) error {
// When the specified string doesn't have any delimiters, treat it as
// the log level for all subsystems.
if !strings.Contains(level, ",") && !strings.Contains(level, "=") {
// Split at the delimiter.
levels := strings.Split(level, ",")
if len(levels) == 0 {
return fmt.Errorf("invalid log level: %v", level)
}
// If the first entry has no =, treat is as the log level for all
// subsystems.
globalLevel := levels[0]
if !strings.Contains(globalLevel, "=") {
// Validate debug log level.
if !validLogLevel(level) {
if !validLogLevel(globalLevel) {
str := "the specified debug level [%v] is invalid"
return fmt.Errorf(str, level)
return fmt.Errorf(str, globalLevel)
}
// Change the logging level for all subsystems.
logger.SetLogLevels(level)
logger.SetLogLevels(globalLevel)
return nil
// The rest will target specific subsystems.
levels = levels[1:]
}
// Split the specified string into subsystem/level pairs while detecting
// issues and update the log levels accordingly.
for _, logLevelPair := range strings.Split(level, ",") {
// Go through the subsystem/level pairs while detecting issues and
// update the log levels accordingly.
for _, logLevelPair := range levels {
if !strings.Contains(logLevelPair, "=") {
str := "the specified debug level contains an " +
"invalid subsystem/level pair [%v]"

14
build/log_test.go

@ -75,6 +75,20 @@ func TestParseAndSetDebugLevels(t *testing.T) {
"SRVR": "debug",
},
},
{
name: "valid global+subsystem debug level",
debugLevel: "trace,PEER=info,SRVR=debug",
expGlobal: "trace",
expSubLevels: map[string]string{
"PEER": "info",
"SRVR": "debug",
},
},
{
name: "invalid global+subsystem debug level",
debugLevel: "PEER=info,debug,SRVR=debug",
expErr: "invalid",
},
}
for _, test := range testCases {

Loading…
Cancel
Save