Codebase list golang-github-go-kit-kit / 803da74
Combine config and Levels structs. Chris Hines 8 years ago
1 changed file(s) with 64 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
66 // want a different set of levels, you can create your own levels type very
77 // easily, and you can elide the configuration.
88 type Levels struct {
9 ctx log.Context
10 cfg *config
11 }
12
13 // New creates a new leveled logger, wrapping the passed logger.
14 func New(logger log.Logger, options ...Option) Levels {
15 cfg := &config{
16 levelKey: "level",
17 debugValue: "debug",
18 infoValue: "info",
19 warnValue: "warn",
20 errorValue: "error",
21 critValue: "crit",
22 }
23 for _, option := range options {
24 option(cfg)
25 }
26 return Levels{
27 ctx: log.NewContext(logger),
28 cfg: cfg,
29 }
30 }
31
32 // With returns a new leveled logger that includes keyvals in all log events.
33 func (l Levels) With(keyvals ...interface{}) Levels {
34 return Levels{
35 ctx: l.ctx.With(keyvals...),
36 cfg: l.cfg,
37 }
38 }
39
40 // Debug logs a debug event along with keyvals.
41 func (l Levels) Debug(keyvals ...interface{}) error {
42 return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.debugValue).Log(keyvals...)
43 }
44
45 // Info logs an info event along with keyvals.
46 func (l Levels) Info(keyvals ...interface{}) error {
47 return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.infoValue).Log(keyvals...)
48 }
49
50 // Warn logs a warn event along with keyvals.
51 func (l Levels) Warn(keyvals ...interface{}) error {
52 return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.warnValue).Log(keyvals...)
53 }
54
55 // Error logs an error event along with keyvals.
56 func (l Levels) Error(keyvals ...interface{}) error {
57 return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.errorValue).Log(keyvals...)
58 }
59
60 // Crit logs a crit event along with keyvals.
61 func (l Levels) Crit(keyvals ...interface{}) error {
62 return l.ctx.WithPrefix(l.cfg.levelKey, l.cfg.critValue).Log(keyvals...)
63 }
64
65 type config struct {
9 ctx log.Context
6610 levelKey string
6711
6812 debugValue string
7216 critValue string
7317 }
7418
19 // New creates a new leveled logger, wrapping the passed logger.
20 func New(logger log.Logger, options ...Option) Levels {
21 l := Levels{
22 ctx: log.NewContext(logger),
23 levelKey: "level",
24
25 debugValue: "debug",
26 infoValue: "info",
27 warnValue: "warn",
28 errorValue: "error",
29 critValue: "crit",
30 }
31 for _, option := range options {
32 option(&l)
33 }
34 return l
35 }
36
37 // With returns a new leveled logger that includes keyvals in all log events.
38 func (l Levels) With(keyvals ...interface{}) Levels {
39 return Levels{
40 ctx: l.ctx.With(keyvals...),
41 levelKey: l.levelKey,
42 debugValue: l.debugValue,
43 infoValue: l.infoValue,
44 warnValue: l.warnValue,
45 errorValue: l.errorValue,
46 critValue: l.critValue,
47 }
48 }
49
50 // Debug logs a debug event along with keyvals.
51 func (l Levels) Debug(keyvals ...interface{}) error {
52 return l.ctx.WithPrefix(l.levelKey, l.debugValue).Log(keyvals...)
53 }
54
55 // Info logs an info event along with keyvals.
56 func (l Levels) Info(keyvals ...interface{}) error {
57 return l.ctx.WithPrefix(l.levelKey, l.infoValue).Log(keyvals...)
58 }
59
60 // Warn logs a warn event along with keyvals.
61 func (l Levels) Warn(keyvals ...interface{}) error {
62 return l.ctx.WithPrefix(l.levelKey, l.warnValue).Log(keyvals...)
63 }
64
65 // Error logs an error event along with keyvals.
66 func (l Levels) Error(keyvals ...interface{}) error {
67 return l.ctx.WithPrefix(l.levelKey, l.errorValue).Log(keyvals...)
68 }
69
70 // Crit logs a crit event along with keyvals.
71 func (l Levels) Crit(keyvals ...interface{}) error {
72 return l.ctx.WithPrefix(l.levelKey, l.critValue).Log(keyvals...)
73 }
74
7575 // Option sets a parameter for leveled loggers.
76 type Option func(*config)
76 type Option func(*Levels)
7777
7878 // Key sets the key for the field used to indicate log level. By default,
7979 // the key is "level".
8080 func Key(key string) Option {
81 return func(c *config) { c.levelKey = key }
81 return func(l *Levels) { l.levelKey = key }
8282 }
8383
8484 // DebugValue sets the value for the field used to indicate the debug log
8585 // level. By default, the value is "debug".
8686 func DebugValue(value string) Option {
87 return func(c *config) { c.debugValue = value }
87 return func(l *Levels) { l.debugValue = value }
8888 }
8989
9090 // InfoValue sets the value for the field used to indicate the info log level.
9191 // By default, the value is "info".
9292 func InfoValue(value string) Option {
93 return func(c *config) { c.infoValue = value }
93 return func(l *Levels) { l.infoValue = value }
9494 }
9595
9696 // WarnValue sets the value for the field used to indicate the warning log
9797 // level. By default, the value is "warn".
9898 func WarnValue(value string) Option {
99 return func(c *config) { c.warnValue = value }
99 return func(l *Levels) { l.warnValue = value }
100100 }
101101
102102 // ErrorValue sets the value for the field used to indicate the error log
103103 // level. By default, the value is "error".
104104 func ErrorValue(value string) Option {
105 return func(c *config) { c.errorValue = value }
105 return func(l *Levels) { l.errorValue = value }
106106 }
107107
108108 // CritValue sets the value for the field used to indicate the critical log
109109 // level. By default, the value is "crit".
110110 func CritValue(value string) Option {
111 return func(c *config) { c.critValue = value }
111 return func(l *Levels) { l.critValue = value }
112112 }