Skip to content

Commit d4a3c9d

Browse files
committed
log/experimental_levels: review feedback
- Unexport level key and default level values - Migrate Allow vars to functions - By default, un-leveled log events are allowed
1 parent 78f29dc commit d4a3c9d

File tree

3 files changed

+88
-86
lines changed

3 files changed

+88
-86
lines changed

log/experimental_level/benchmark_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func BenchmarkNopBaseline(b *testing.B) {
1414

1515
func BenchmarkNopDisallowedLevel(b *testing.B) {
1616
benchmarkRunner(b, level.New(log.NewNopLogger(), level.Config{
17-
Allowed: level.AllowInfoAndAbove,
17+
Allowed: level.AllowInfoAndAbove(),
1818
}))
1919
}
2020

2121
func BenchmarkNopAllowedLevel(b *testing.B) {
2222
benchmarkRunner(b, level.New(log.NewNopLogger(), level.Config{
23-
Allowed: level.AllowAll,
23+
Allowed: level.AllowAll(),
2424
}))
2525
}
2626

@@ -30,13 +30,13 @@ func BenchmarkJSONBaseline(b *testing.B) {
3030

3131
func BenchmarkJSONDisallowedLevel(b *testing.B) {
3232
benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard), level.Config{
33-
Allowed: level.AllowInfoAndAbove,
33+
Allowed: level.AllowInfoAndAbove(),
3434
}))
3535
}
3636

3737
func BenchmarkJSONAllowedLevel(b *testing.B) {
3838
benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard), level.Config{
39-
Allowed: level.AllowAll,
39+
Allowed: level.AllowAll(),
4040
}))
4141
}
4242

@@ -46,13 +46,13 @@ func BenchmarkLogfmtBaseline(b *testing.B) {
4646

4747
func BenchmarkLogfmtDisallowedLevel(b *testing.B) {
4848
benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard), level.Config{
49-
Allowed: level.AllowInfoAndAbove,
49+
Allowed: level.AllowInfoAndAbove(),
5050
}))
5151
}
5252

5353
func BenchmarkLogfmtAllowedLevel(b *testing.B) {
5454
benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard), level.Config{
55-
Allowed: level.AllowAll,
55+
Allowed: level.AllowAll(),
5656
}))
5757
}
5858

log/experimental_level/level.go

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,88 @@ import (
55
)
66

77
var (
8-
// LevelKey is the key part of a level keyval.
9-
LevelKey = "level"
10-
11-
// ErrorLevelValue is the val part of an error-level keyval.
12-
ErrorLevelValue = "error"
13-
14-
// WarnLevelValue is the val part of a warn-level keyval.
15-
WarnLevelValue = "warn"
16-
17-
// InfoLevelValue is the val part of an info-level keyval.
18-
InfoLevelValue = "info"
19-
20-
// DebugLevelValue is the val part of a debug-level keyval.
21-
DebugLevelValue = "debug"
8+
levelKey = "level"
9+
errorLevelValue = "error"
10+
warnLevelValue = "warn"
11+
infoLevelValue = "info"
12+
debugLevelValue = "debug"
2213
)
2314

24-
var (
25-
// AllowAll is an alias for AllowDebugAndAbove.
26-
AllowAll = AllowDebugAndAbove
15+
// AllowAll is an alias for AllowDebugAndAbove.
16+
func AllowAll() []string {
17+
return AllowDebugAndAbove()
18+
}
2719

28-
// AllowDebugAndAbove allows all of the four default log levels.
29-
// It may be provided as the Allowed parameter in the Config struct.
30-
AllowDebugAndAbove = []string{ErrorLevelValue, WarnLevelValue, InfoLevelValue, DebugLevelValue}
20+
// AllowDebugAndAbove allows all of the four default log levels.
21+
// Its return value may be provided as the Allowed parameter in the Config.
22+
func AllowDebugAndAbove() []string {
23+
return []string{errorLevelValue, warnLevelValue, infoLevelValue, debugLevelValue}
24+
}
3125

32-
// AllowInfoAndAbove allows the default info, warn, and error log levels.
33-
// It may be provided as the Allowed parameter in the Config struct.
34-
AllowInfoAndAbove = []string{ErrorLevelValue, WarnLevelValue, InfoLevelValue}
26+
// AllowInfoAndAbove allows the default info, warn, and error log levels.
27+
// Its return value may be provided as the Allowed parameter in the Config.
28+
func AllowInfoAndAbove() []string {
29+
return []string{errorLevelValue, warnLevelValue, infoLevelValue}
30+
}
3531

36-
// AllowWarnAndAbove allows the default warn and error log levels.
37-
// It may be provided as the Allowed parameter in the Config struct.
38-
AllowWarnAndAbove = []string{ErrorLevelValue, WarnLevelValue}
32+
// AllowWarnAndAbove allows the default warn and error log levels.
33+
// Its return value may be provided as the Allowed parameter in the Config.
34+
func AllowWarnAndAbove() []string {
35+
return []string{errorLevelValue, warnLevelValue}
36+
}
3937

40-
// AllowErrorOnly allows only the default error log level.
41-
// It may be provided as the Allowed parameter in the Config struct.
42-
AllowErrorOnly = []string{ErrorLevelValue}
38+
// AllowErrorOnly allows only the default error log level.
39+
// Its return value may be provided as the Allowed parameter in the Config.
40+
func AllowErrorOnly() []string {
41+
return []string{errorLevelValue}
42+
}
4343

44-
// AllowNone allows none of the default log levels.
45-
// It may be provided as the Allowed parameter in the Config struct.
46-
AllowNone = []string{}
47-
)
44+
// AllowNone allows none of the default log levels.
45+
// Its return value may be provided as the Allowed parameter in the Config.
46+
func AllowNone() []string {
47+
return []string{}
48+
}
4849

49-
// Error returns a logger with the LevelKey set to ErrorLevelValue.
50+
// Error returns a logger with the level key set to ErrorLevelValue.
5051
func Error(logger log.Logger) log.Logger {
51-
return log.NewContext(logger).With(LevelKey, ErrorLevelValue)
52+
return log.NewContext(logger).With(levelKey, errorLevelValue)
5253
}
5354

54-
// Warn returns a logger with the LevelKey set to WarnLevelValue.
55+
// Warn returns a logger with the level key set to WarnLevelValue.
5556
func Warn(logger log.Logger) log.Logger {
56-
return log.NewContext(logger).With(LevelKey, WarnLevelValue)
57+
return log.NewContext(logger).With(levelKey, warnLevelValue)
5758
}
5859

59-
// Info returns a logger with the LevelKey set to InfoLevelValue.
60+
// Info returns a logger with the level key set to InfoLevelValue.
6061
func Info(logger log.Logger) log.Logger {
61-
return log.NewContext(logger).With(LevelKey, InfoLevelValue)
62+
return log.NewContext(logger).With(levelKey, infoLevelValue)
6263
}
6364

64-
// Debug returns a logger with the LevelKey set to DebugLevelValue.
65+
// Debug returns a logger with the level key set to DebugLevelValue.
6566
func Debug(logger log.Logger) log.Logger {
66-
return log.NewContext(logger).With(LevelKey, DebugLevelValue)
67+
return log.NewContext(logger).With(levelKey, debugLevelValue)
6768
}
6869

6970
// Config parameterizes the leveled logger.
7071
type Config struct {
7172
// Allowed enumerates the accepted log levels. If a log event is encountered
72-
// with a LevelKey set to a value that isn't explicitly allowed, the event
73-
// will be squelched, and ErrSquelched returned.
73+
// with a level key set to a value that isn't explicitly allowed, the event
74+
// will be squelched, and ErrNotAllowed returned.
7475
Allowed []string
7576

76-
// ErrSquelched is returned to the caller when Log is invoked with a
77-
// LevelKey that hasn't been explicitly allowed. By default, ErrSquelched is
77+
// ErrNotAllowed is returned to the caller when Log is invoked with a level
78+
// key that hasn't been explicitly allowed. By default, ErrNotAllowed is
7879
// nil; in this case, the log event is squelched with no error.
79-
ErrSquelched error
80+
ErrNotAllowed error
8081

81-
// AllowNoLevel will allow log events with no LevelKey to proceed through to
82-
// the wrapped logger without error. By default, log events with no LevelKey
83-
// will be squelched, and ErrNoLevel returned.
84-
AllowNoLevel bool
82+
// SquelchNoLevel will squelch log events with no level key, so that they
83+
// don't proceed through to the wrapped logger. If SquelchNoLevel is set to
84+
// true and a log event is squelched in this way, ErrNoLevel is returned to
85+
// the caller.
86+
SquelchNoLevel bool
8587

86-
// ErrNoLevel is returned to the caller when AllowNoLevel is false, and Log
87-
// is invoked without a LevelKey. By default, ErrNoLevel is nil; in this
88+
// ErrNoLevel is returned to the caller when SquelchNoLevel is true, and Log
89+
// is invoked without a level key. By default, ErrNoLevel is nil; in this
8890
// case, the log event is squelched with no error.
8991
ErrNoLevel error
9092
}
@@ -93,26 +95,26 @@ type Config struct {
9395
// Config object for a detailed description of how to configure levels.
9496
func New(next log.Logger, config Config) log.Logger {
9597
return &logger{
96-
next: next,
97-
allowed: makeSet(config.Allowed),
98-
errSquelched: config.ErrSquelched,
99-
allowNoLevel: config.AllowNoLevel,
100-
errNoLevel: config.ErrNoLevel,
98+
next: next,
99+
allowed: makeSet(config.Allowed),
100+
errNotAllowed: config.ErrNotAllowed,
101+
squelchNoLevel: config.SquelchNoLevel,
102+
errNoLevel: config.ErrNoLevel,
101103
}
102104
}
103105

104106
type logger struct {
105-
next log.Logger
106-
allowed map[string]struct{}
107-
errSquelched error
108-
allowNoLevel bool
109-
errNoLevel error
107+
next log.Logger
108+
allowed map[string]struct{}
109+
errNotAllowed error
110+
squelchNoLevel bool
111+
errNoLevel error
110112
}
111113

112114
func (l *logger) Log(keyvals ...interface{}) error {
113115
var hasLevel, levelAllowed bool
114116
for i := 0; i < len(keyvals); i += 2 {
115-
if k, ok := keyvals[i].(string); !ok || k != LevelKey {
117+
if k, ok := keyvals[i].(string); !ok || k != levelKey {
116118
continue
117119
}
118120
hasLevel = true
@@ -126,11 +128,11 @@ func (l *logger) Log(keyvals ...interface{}) error {
126128
_, levelAllowed = l.allowed[v]
127129
break
128130
}
129-
if !hasLevel && !l.allowNoLevel {
131+
if !hasLevel && l.squelchNoLevel {
130132
return l.errNoLevel
131133
}
132134
if hasLevel && !levelAllowed {
133-
return l.errSquelched
135+
return l.errNotAllowed
134136
}
135137
return l.next.Log(keyvals...)
136138
}

log/experimental_level/level_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestVariousLevels(t *testing.T) {
1616
want string
1717
}{
1818
{
19-
level.AllowAll,
19+
level.AllowAll(),
2020
strings.Join([]string{
2121
`{"level":"debug","this is":"debug log"}`,
2222
`{"level":"info","this is":"info log"}`,
@@ -25,7 +25,7 @@ func TestVariousLevels(t *testing.T) {
2525
}, "\n"),
2626
},
2727
{
28-
level.AllowDebugAndAbove,
28+
level.AllowDebugAndAbove(),
2929
strings.Join([]string{
3030
`{"level":"debug","this is":"debug log"}`,
3131
`{"level":"info","this is":"info log"}`,
@@ -34,28 +34,28 @@ func TestVariousLevels(t *testing.T) {
3434
}, "\n"),
3535
},
3636
{
37-
level.AllowInfoAndAbove,
37+
level.AllowInfoAndAbove(),
3838
strings.Join([]string{
3939
`{"level":"info","this is":"info log"}`,
4040
`{"level":"warn","this is":"warn log"}`,
4141
`{"level":"error","this is":"error log"}`,
4242
}, "\n"),
4343
},
4444
{
45-
level.AllowWarnAndAbove,
45+
level.AllowWarnAndAbove(),
4646
strings.Join([]string{
4747
`{"level":"warn","this is":"warn log"}`,
4848
`{"level":"error","this is":"error log"}`,
4949
}, "\n"),
5050
},
5151
{
52-
level.AllowErrorOnly,
52+
level.AllowErrorOnly(),
5353
strings.Join([]string{
5454
`{"level":"error","this is":"error log"}`,
5555
}, "\n"),
5656
},
5757
{
58-
level.AllowNone,
58+
level.AllowNone(),
5959
``,
6060
},
6161
} {
@@ -73,11 +73,11 @@ func TestVariousLevels(t *testing.T) {
7373
}
7474
}
7575

76-
func TestErrSquelch(t *testing.T) {
76+
func TestErrNotAllowed(t *testing.T) {
7777
myError := errors.New("squelched!")
7878
logger := level.New(log.NewNopLogger(), level.Config{
79-
Allowed: level.AllowWarnAndAbove,
80-
ErrSquelched: myError,
79+
Allowed: level.AllowWarnAndAbove(),
80+
ErrNotAllowed: myError,
8181
})
8282

8383
if want, have := myError, level.Info(logger).Log("foo", "bar"); want != have {
@@ -94,8 +94,8 @@ func TestErrNoLevel(t *testing.T) {
9494

9595
var buf bytes.Buffer
9696
logger := level.New(log.NewJSONLogger(&buf), level.Config{
97-
AllowNoLevel: false,
98-
ErrNoLevel: myError,
97+
SquelchNoLevel: true,
98+
ErrNoLevel: myError,
9999
})
100100

101101
if want, have := myError, logger.Log("foo", "bar"); want != have {
@@ -109,8 +109,8 @@ func TestErrNoLevel(t *testing.T) {
109109
func TestAllowNoLevel(t *testing.T) {
110110
var buf bytes.Buffer
111111
logger := level.New(log.NewJSONLogger(&buf), level.Config{
112-
AllowNoLevel: true,
113-
ErrNoLevel: errors.New("I should never be returned!"),
112+
SquelchNoLevel: false,
113+
ErrNoLevel: errors.New("I should never be returned!"),
114114
})
115115

116116
if want, have := error(nil), logger.Log("foo", "bar"); want != have {
@@ -128,7 +128,7 @@ func TestLevelContext(t *testing.T) {
128128
// log.DefaultCaller as per normal.
129129
var logger log.Logger
130130
logger = log.NewLogfmtLogger(&buf)
131-
logger = level.New(logger, level.Config{Allowed: level.AllowAll})
131+
logger = level.New(logger, level.Config{Allowed: level.AllowAll()})
132132
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
133133

134134
level.Info(logger).Log("foo", "bar")
@@ -145,7 +145,7 @@ func TestContextLevel(t *testing.T) {
145145
var logger log.Logger
146146
logger = log.NewLogfmtLogger(&buf)
147147
logger = log.NewContext(logger).With("caller", log.Caller(5))
148-
logger = level.New(logger, level.Config{Allowed: level.AllowAll})
148+
logger = level.New(logger, level.Config{Allowed: level.AllowAll()})
149149

150150
level.Info(logger).Log("foo", "bar")
151151
if want, have := `caller=level_test.go:150 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {

0 commit comments

Comments
 (0)