Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
log: Allow eggos implementations to programatically set log levels (#96)
When running applications on environments where there is no environment, calls to os.Setenv to set a loglevel have inconsistent results. Similarly, setting this environment variable before the first log event, and thus making that call moot, is racy. Instead, then, export log levels, drop the potential stutter in log.LogLvl to log.Level, and allow implementations/ callers of this package to set a log level accordingly, while maintaining the default behaviour
- Loading branch information
Showing
2 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package log_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/icexin/eggos/log" | ||
) | ||
|
||
func ExampleSetLogLevel() { | ||
// Set the log level to Debug | ||
log.Level = log.LoglvlDebug | ||
} | ||
|
||
func TestLog_SetLevel(t *testing.T) { | ||
for _, test := range []struct { | ||
name string | ||
l log.LogLevel | ||
expectError bool | ||
}{ | ||
// These two cases require users to do something pretty explcitly | ||
// wrong to hit, but they're worth catching | ||
{"log level is too low", log.LogLevel(-1), true}, | ||
{"log level is too high", log.LogLevel(6), true}, | ||
|
||
// Included log levels | ||
{"log.LoglvlDebug is valid", log.LoglvlDebug, false}, | ||
{"log.LoglvlInfo is valid", log.LoglvlInfo, false}, | ||
{"log.LoglvlWarn is valid", log.LoglvlWarn, false}, | ||
{"log.LoglvlError is valid", log.LoglvlError, false}, | ||
{"log.LoglvlNone is valid", log.LoglvlNone, false}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := log.SetLevel(test.l) | ||
|
||
if err == nil && test.expectError { | ||
t.Error("expected error, received none") | ||
} else if err != nil && !test.expectError { | ||
t.Errorf("unexpected error: %#v", err) | ||
} | ||
|
||
if !test.expectError { | ||
if log.Level != test.l { | ||
t.Errorf("log.Level should be %#v, received %#v", log.Level, test.l) | ||
} | ||
} | ||
}) | ||
} | ||
} |