Skip to content

Commit 803da74

Browse files
committed
Combine config and Levels structs.
1 parent 8899b82 commit 803da74

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

log/levels/levels.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,107 +7,107 @@ import "github.com/go-kit/kit/log"
77
// want a different set of levels, you can create your own levels type very
88
// easily, and you can elide the configuration.
99
type Levels struct {
10-
ctx log.Context
11-
cfg *config
10+
ctx log.Context
11+
levelKey string
12+
13+
debugValue string
14+
infoValue string
15+
warnValue string
16+
errorValue string
17+
critValue string
1218
}
1319

1420
// New creates a new leveled logger, wrapping the passed logger.
1521
func New(logger log.Logger, options ...Option) Levels {
16-
cfg := &config{
17-
levelKey: "level",
22+
l := Levels{
23+
ctx: log.NewContext(logger),
24+
levelKey: "level",
25+
1826
debugValue: "debug",
1927
infoValue: "info",
2028
warnValue: "warn",
2129
errorValue: "error",
2230
critValue: "crit",
2331
}
2432
for _, option := range options {
25-
option(cfg)
26-
}
27-
return Levels{
28-
ctx: log.NewContext(logger),
29-
cfg: cfg,
33+
option(&l)
3034
}
35+
return l
3136
}
3237

3338
// With returns a new leveled logger that includes keyvals in all log events.
3439
func (l Levels) With(keyvals ...interface{}) Levels {
3540
return Levels{
36-
ctx: l.ctx.With(keyvals...),
37-
cfg: l.cfg,
41+
ctx: l.ctx.With(keyvals...),
42+
levelKey: l.levelKey,
43+
debugValue: l.debugValue,
44+
infoValue: l.infoValue,
45+
warnValue: l.warnValue,
46+
errorValue: l.errorValue,
47+
critValue: l.critValue,
3848
}
3949
}
4050

4151
// Debug logs a debug event along with keyvals.
4252
func (l Levels) Debug(keyvals ...interface{}) error {
43-
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.debugValue).Log(keyvals...)
53+
return l.ctx.WithPrefix(l.levelKey, l.debugValue).Log(keyvals...)
4454
}
4555

4656
// Info logs an info event along with keyvals.
4757
func (l Levels) Info(keyvals ...interface{}) error {
48-
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.infoValue).Log(keyvals...)
58+
return l.ctx.WithPrefix(l.levelKey, l.infoValue).Log(keyvals...)
4959
}
5060

5161
// Warn logs a warn event along with keyvals.
5262
func (l Levels) Warn(keyvals ...interface{}) error {
53-
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.warnValue).Log(keyvals...)
63+
return l.ctx.WithPrefix(l.levelKey, l.warnValue).Log(keyvals...)
5464
}
5565

5666
// Error logs an error event along with keyvals.
5767
func (l Levels) Error(keyvals ...interface{}) error {
58-
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.errorValue).Log(keyvals...)
68+
return l.ctx.WithPrefix(l.levelKey, l.errorValue).Log(keyvals...)
5969
}
6070

6171
// Crit logs a crit event along with keyvals.
6272
func (l Levels) Crit(keyvals ...interface{}) error {
63-
return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.critValue).Log(keyvals...)
64-
}
65-
66-
type config struct {
67-
levelKey string
68-
69-
debugValue string
70-
infoValue string
71-
warnValue string
72-
errorValue string
73-
critValue string
73+
return l.ctx.WithPrefix(l.levelKey, l.critValue).Log(keyvals...)
7474
}
7575

7676
// Option sets a parameter for leveled loggers.
77-
type Option func(*config)
77+
type Option func(*Levels)
7878

7979
// Key sets the key for the field used to indicate log level. By default,
8080
// the key is "level".
8181
func Key(key string) Option {
82-
return func(c *config) { c.levelKey = key }
82+
return func(l *Levels) { l.levelKey = key }
8383
}
8484

8585
// DebugValue sets the value for the field used to indicate the debug log
8686
// level. By default, the value is "debug".
8787
func DebugValue(value string) Option {
88-
return func(c *config) { c.debugValue = value }
88+
return func(l *Levels) { l.debugValue = value }
8989
}
9090

9191
// InfoValue sets the value for the field used to indicate the info log level.
9292
// By default, the value is "info".
9393
func InfoValue(value string) Option {
94-
return func(c *config) { c.infoValue = value }
94+
return func(l *Levels) { l.infoValue = value }
9595
}
9696

9797
// WarnValue sets the value for the field used to indicate the warning log
9898
// level. By default, the value is "warn".
9999
func WarnValue(value string) Option {
100-
return func(c *config) { c.warnValue = value }
100+
return func(l *Levels) { l.warnValue = value }
101101
}
102102

103103
// ErrorValue sets the value for the field used to indicate the error log
104104
// level. By default, the value is "error".
105105
func ErrorValue(value string) Option {
106-
return func(c *config) { c.errorValue = value }
106+
return func(l *Levels) { l.errorValue = value }
107107
}
108108

109109
// CritValue sets the value for the field used to indicate the critical log
110110
// level. By default, the value is "crit".
111111
func CritValue(value string) Option {
112-
return func(c *config) { c.critValue = value }
112+
return func(l *Levels) { l.critValue = value }
113113
}

0 commit comments

Comments
 (0)