Codebase list golang-github-go-kit-kit / ae92291
Use variadic function to pass parameters instead of Config struct Using function to pass optional params makes the API more consistent with the rest of go-kit packages. For #427 Victor Vrantchan 6 years ago
4 changed file(s) with 80 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
1212 }
1313
1414 func BenchmarkNopDisallowedLevel(b *testing.B) {
15 benchmarkRunner(b, level.New(log.NewNopLogger(), level.Config{
16 Allowed: level.AllowInfoAndAbove(),
17 }))
15 benchmarkRunner(b, level.New(log.NewNopLogger(),
16 level.Allowed(level.AllowInfoAndAbove())))
1817 }
1918
2019 func BenchmarkNopAllowedLevel(b *testing.B) {
21 benchmarkRunner(b, level.New(log.NewNopLogger(), level.Config{
22 Allowed: level.AllowAll(),
23 }))
20 benchmarkRunner(b, level.New(log.NewNopLogger(),
21 level.Allowed(level.AllowAll())))
2422 }
2523
2624 func BenchmarkJSONBaseline(b *testing.B) {
2826 }
2927
3028 func BenchmarkJSONDisallowedLevel(b *testing.B) {
31 benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard), level.Config{
32 Allowed: level.AllowInfoAndAbove(),
33 }))
29 benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard),
30 level.Allowed(level.AllowInfoAndAbove())))
3431 }
3532
3633 func BenchmarkJSONAllowedLevel(b *testing.B) {
37 benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard), level.Config{
38 Allowed: level.AllowAll(),
39 }))
34 benchmarkRunner(b, level.New(log.NewJSONLogger(ioutil.Discard),
35 level.Allowed(level.AllowAll())))
4036 }
4137
4238 func BenchmarkLogfmtBaseline(b *testing.B) {
4440 }
4541
4642 func BenchmarkLogfmtDisallowedLevel(b *testing.B) {
47 benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard), level.Config{
48 Allowed: level.AllowInfoAndAbove(),
49 }))
43 benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard),
44 level.Allowed(level.AllowInfoAndAbove())))
5045 }
5146
5247 func BenchmarkLogfmtAllowedLevel(b *testing.B) {
53 benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard), level.Config{
54 Allowed: level.AllowAll(),
55 }))
48 benchmarkRunner(b, level.New(log.NewLogfmtLogger(ioutil.Discard),
49 level.Allowed(level.AllowAll())))
5650 }
5751
5852 func benchmarkRunner(b *testing.B, logger log.Logger) {
55 //
66 // var logger log.Logger
77 // logger = log.NewLogfmtLogger(os.Stderr)
8 // logger = level.New(logger, level.Config{Allowed: level.AllowInfoAndAbove}) // <--
8 // logger = level.New(logger, level.Allowed(level.AllowInfoAndAbove())) // <--
99 // logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
1010 //
1111 // Then, at the callsites, use one of the level.Debug, Info, Warn, or Error
1919 //
2020 // The leveled logger allows precise control over what should happen if a log
2121 // event is emitted without a level key, or if a squelched level is used. Check
22 // the Config struct for details. And, you can easily use non-default level
22 // the Option functions for details. And, you can easily use non-default level
2323 // values: create new string constants for whatever you want to change, pass
24 // them explicitly to the Config struct, and write your own level.Foo-style
24 // them explicitly to the Allowed Option function, and write your own level.Foo-style
2525 // helper methods.
2626 package level
1717 }
1818
1919 // AllowDebugAndAbove allows all of the four default log levels.
20 // Its return value may be provided as the Allowed parameter in the Config.
20 // Its return value may be provided with the Allowed Option.
2121 func AllowDebugAndAbove() []string {
2222 return []string{errorLevelValue, warnLevelValue, infoLevelValue, debugLevelValue}
2323 }
2424
2525 // AllowInfoAndAbove allows the default info, warn, and error log levels.
26 // Its return value may be provided as the Allowed parameter in the Config.
26 // Its return value may be provided with the Allowed Option.
2727 func AllowInfoAndAbove() []string {
2828 return []string{errorLevelValue, warnLevelValue, infoLevelValue}
2929 }
3030
3131 // AllowWarnAndAbove allows the default warn and error log levels.
32 // Its return value may be provided as the Allowed parameter in the Config.
32 // Its return value may be provided with the Allowed Option.
3333 func AllowWarnAndAbove() []string {
3434 return []string{errorLevelValue, warnLevelValue}
3535 }
3636
3737 // AllowErrorOnly allows only the default error log level.
38 // Its return value may be provided as the Allowed parameter in the Config.
38 // Its return value may be provided with the Allowed Option.
3939 func AllowErrorOnly() []string {
4040 return []string{errorLevelValue}
4141 }
4242
4343 // AllowNone allows none of the default log levels.
44 // Its return value may be provided as the Allowed parameter in the Config.
44 // Its return value may be provided with the Allowed Option.
4545 func AllowNone() []string {
4646 return []string{}
4747 }
6666 return log.NewContext(logger).WithPrefix(levelKey, debugLevelValue)
6767 }
6868
69 // Config parameterizes the leveled logger.
70 type Config struct {
71 // Allowed enumerates the accepted log levels. If a log event is encountered
72 // with a level key set to a value that isn't explicitly allowed, the event
73 // will be squelched, and ErrNotAllowed returned.
74 Allowed []string
75
76 // ErrNotAllowed is returned to the caller when Log is invoked with a level
77 // key that hasn't been explicitly allowed. By default, ErrNotAllowed is
78 // nil; in this case, the log event is squelched with no error.
79 ErrNotAllowed error
80
81 // SquelchNoLevel will squelch log events with no level key, so that they
82 // don't proceed through to the wrapped logger. If SquelchNoLevel is set to
83 // true and a log event is squelched in this way, ErrNoLevel is returned to
84 // the caller.
85 SquelchNoLevel bool
86
87 // ErrNoLevel is returned to the caller when SquelchNoLevel is true, and Log
88 // is invoked without a level key. By default, ErrNoLevel is nil; in this
89 // case, the log event is squelched with no error.
90 ErrNoLevel error
69 // New wraps the logger and implements level checking. See the commentary on the
70 // Option functions for a detailed description of how to configure levels.
71 func New(next log.Logger, options ...Option) log.Logger {
72 l := logger{
73 next: next,
74 }
75 for _, option := range options {
76 option(&l)
77 }
78 return &l
9179 }
9280
93 // New wraps the logger and implements level checking. See the commentary on the
94 // Config object for a detailed description of how to configure levels.
95 func New(next log.Logger, config Config) log.Logger {
96 return &logger{
97 next: next,
98 allowed: makeSet(config.Allowed),
99 errNotAllowed: config.ErrNotAllowed,
100 squelchNoLevel: config.SquelchNoLevel,
101 errNoLevel: config.ErrNoLevel,
102 }
81 // Allowed enumerates the accepted log levels. If a log event is encountered
82 // with a level key set to a value that isn't explicitly allowed, the event
83 // will be squelched, and ErrNotAllowed returned.
84 func Allowed(allowed []string) Option {
85 return func(l *logger) { l.allowed = makeSet(allowed) }
10386 }
87
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
90 // case, the log event is squelched with no error.
91 func ErrNotAllowed(err error) Option {
92 return func(l *logger) { l.errNotAllowed = err }
93 }
94
95 // SquelchNoLevel will squelch log events with no level key, so that they
96 // don't proceed through to the wrapped logger. If SquelchNoLevel is set to
97 // true and a log event is squelched in this way, ErrNoLevel is returned to
98 // the caller.
99 func SquelchNoLevel(squelch bool) Option {
100 return func(l *logger) { l.squelchNoLevel = squelch }
101 }
102
103 // ErrNoLevel is returned to the caller when SquelchNoLevel is true, and Log
104 // is invoked without a level key. By default, ErrNoLevel is nil; in this
105 // case, the log event is squelched with no error.
106 func ErrNoLevel(err error) Option {
107 return func(l *logger) { l.errNoLevel = err }
108 }
109
110 // Option sets a parameter for the leveled logger.
111 type Option func(*logger)
104112
105113 type logger struct {
106114 next log.Logger
5959 },
6060 } {
6161 var buf bytes.Buffer
62 logger := level.New(log.NewJSONLogger(&buf), level.Config{Allowed: testcase.allowed})
62 logger := level.New(log.NewJSONLogger(&buf), level.Allowed(testcase.allowed))
6363
6464 level.Debug(logger).Log("this is", "debug log")
6565 level.Info(logger).Log("this is", "info log")
7474
7575 func TestErrNotAllowed(t *testing.T) {
7676 myError := errors.New("squelched!")
77 logger := level.New(log.NewNopLogger(), level.Config{
78 Allowed: level.AllowWarnAndAbove(),
79 ErrNotAllowed: myError,
80 })
77 opts := []level.Option{
78 level.Allowed(level.AllowWarnAndAbove()),
79 level.ErrNotAllowed(myError),
80 }
81 logger := level.New(log.NewNopLogger(), opts...)
8182
8283 if want, have := myError, level.Info(logger).Log("foo", "bar"); want != have {
8384 t.Errorf("want %#+v, have %#+v", want, have)
9293 myError := errors.New("no level specified")
9394
9495 var buf bytes.Buffer
95 logger := level.New(log.NewJSONLogger(&buf), level.Config{
96 SquelchNoLevel: true,
97 ErrNoLevel: myError,
98 })
96 opts := []level.Option{
97 level.SquelchNoLevel(true),
98 level.ErrNoLevel(myError),
99 }
100 logger := level.New(log.NewJSONLogger(&buf), opts...)
99101
100102 if want, have := myError, logger.Log("foo", "bar"); want != have {
101103 t.Errorf("want %v, have %v", want, have)
107109
108110 func TestAllowNoLevel(t *testing.T) {
109111 var buf bytes.Buffer
110 logger := level.New(log.NewJSONLogger(&buf), level.Config{
111 SquelchNoLevel: false,
112 ErrNoLevel: errors.New("I should never be returned!"),
113 })
112 opts := []level.Option{
113 level.SquelchNoLevel(false),
114 level.ErrNoLevel(errors.New("I should never be returned!")),
115 }
116 logger := level.New(log.NewJSONLogger(&buf), opts...)
114117
115118 if want, have := error(nil), logger.Log("foo", "bar"); want != have {
116119 t.Errorf("want %v, have %v", want, have)
127130 // log.DefaultCaller as per normal.
128131 var logger log.Logger
129132 logger = log.NewLogfmtLogger(&buf)
130 logger = level.New(logger, level.Config{Allowed: level.AllowAll()})
133 logger = level.New(logger, level.Allowed(level.AllowAll()))
131134 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
132135
133136 level.Info(logger).Log("foo", "bar")
134 if want, have := `level=info caller=level_test.go:134 foo=bar`, strings.TrimSpace(buf.String()); want != have {
137 if want, have := `level=info caller=level_test.go:137 foo=bar`, strings.TrimSpace(buf.String()); want != have {
135138 t.Errorf("want %q, have %q", want, have)
136139 }
137140 }
144147 var logger log.Logger
145148 logger = log.NewLogfmtLogger(&buf)
146149 logger = log.NewContext(logger).With("caller", log.Caller(5))
147 logger = level.New(logger, level.Config{Allowed: level.AllowAll()})
150 logger = level.New(logger, level.Allowed(level.AllowAll()))
148151
149152 level.Info(logger).Log("foo", "bar")
150 if want, have := `caller=level_test.go:150 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {
153 if want, have := `caller=level_test.go:153 level=info foo=bar`, strings.TrimSpace(buf.String()); want != have {
151154 t.Errorf("want %q, have %q", want, have)
152155 }
153156 }