Skip to content

Commit 384cc47

Browse files
author
Nick Thomas
committed
Default to info level for an empty log-level
I'd assumed that the `omitempty` directive for LogLevel in internal/config/config.go would get us this behaviour. If it did, we wouldn't have had to specify the default twice. Unfortunately, it doesn't, which is to say that given a config file like: ``` log_level: ``` The default *is* overridden by the empty string. It's an easy enough fix. Changelog: fixed
1 parent ede95ae commit 384cc47

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

internal/logger/logger.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ func logFmt(inFmt string) string {
2222
return inFmt
2323
}
2424

25+
func logLevel(inLevel string) string {
26+
if inLevel == "" {
27+
return "info"
28+
}
29+
30+
return inLevel
31+
}
32+
2533
func logFile(inFile string) string {
2634
if inFile == "" {
2735
return "stderr"
@@ -35,7 +43,7 @@ func buildOpts(cfg *config.Config) []log.LoggerOption {
3543
log.WithFormatter(logFmt(cfg.LogFormat)),
3644
log.WithOutputName(logFile(cfg.LogFile)),
3745
log.WithTimezone(time.UTC),
38-
log.WithLogLevel(cfg.LogLevel),
46+
log.WithLogLevel(logLevel(cfg.LogLevel)),
3947
}
4048
}
4149

internal/logger/logger_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ func TestConfigure(t *testing.T) {
3030
tmpFile.Close()
3131

3232
data, err := os.ReadFile(tmpFile.Name())
33+
dataStr := string(data)
3334
require.NoError(t, err)
34-
require.Contains(t, string(data), `msg":"this is a test"`)
35-
require.NotContains(t, string(data), `msg:":"debug log message"`)
35+
require.Contains(t, dataStr, `"msg":"this is a test"`)
36+
require.NotContains(t, dataStr, `"msg":"debug log message"`)
37+
require.NotContains(t, dataStr, `"msg":"unknown log level`)
3638
}
3739

3840
func TestConfigureWithDebugLogLevel(t *testing.T) {

0 commit comments

Comments
 (0)