Combine config and Levels structs.
Chris Hines
8 years ago
6 | 6 | // want a different set of levels, you can create your own levels type very |
7 | 7 | // easily, and you can elide the configuration. |
8 | 8 | 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 | |
66 | 10 | levelKey string |
67 | 11 | |
68 | 12 | debugValue string |
72 | 16 | critValue string |
73 | 17 | } |
74 | 18 | |
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 | ||
75 | 75 | // Option sets a parameter for leveled loggers. |
76 | type Option func(*config) | |
76 | type Option func(*Levels) | |
77 | 77 | |
78 | 78 | // Key sets the key for the field used to indicate log level. By default, |
79 | 79 | // the key is "level". |
80 | 80 | func Key(key string) Option { |
81 | return func(c *config) { c.levelKey = key } | |
81 | return func(l *Levels) { l.levelKey = key } | |
82 | 82 | } |
83 | 83 | |
84 | 84 | // DebugValue sets the value for the field used to indicate the debug log |
85 | 85 | // level. By default, the value is "debug". |
86 | 86 | func DebugValue(value string) Option { |
87 | return func(c *config) { c.debugValue = value } | |
87 | return func(l *Levels) { l.debugValue = value } | |
88 | 88 | } |
89 | 89 | |
90 | 90 | // InfoValue sets the value for the field used to indicate the info log level. |
91 | 91 | // By default, the value is "info". |
92 | 92 | func InfoValue(value string) Option { |
93 | return func(c *config) { c.infoValue = value } | |
93 | return func(l *Levels) { l.infoValue = value } | |
94 | 94 | } |
95 | 95 | |
96 | 96 | // WarnValue sets the value for the field used to indicate the warning log |
97 | 97 | // level. By default, the value is "warn". |
98 | 98 | func WarnValue(value string) Option { |
99 | return func(c *config) { c.warnValue = value } | |
99 | return func(l *Levels) { l.warnValue = value } | |
100 | 100 | } |
101 | 101 | |
102 | 102 | // ErrorValue sets the value for the field used to indicate the error log |
103 | 103 | // level. By default, the value is "error". |
104 | 104 | func ErrorValue(value string) Option { |
105 | return func(c *config) { c.errorValue = value } | |
105 | return func(l *Levels) { l.errorValue = value } | |
106 | 106 | } |
107 | 107 | |
108 | 108 | // CritValue sets the value for the field used to indicate the critical log |
109 | 109 | // level. By default, the value is "crit". |
110 | 110 | func CritValue(value string) Option { |
111 | return func(c *config) { c.critValue = value } | |
111 | return func(l *Levels) { l.critValue = value } | |
112 | 112 | } |