3 import "github.com/go-kit/kit/log"
5 // Levels provides a leveled logging wrapper around a logger. It has five
6 // levels: debug, info, warning (warn), error, and critical (crit). If you
7 // want a different set of levels, you can create your own levels type very
8 // easily, and you can elide the configuration.
13 // We have a choice between storing level values in string fields or
14 // making a separate context for each level. When using string fields the
15 // Log method must combine the base context, the level data, and the
16 // logged keyvals; but the With method only requires updating one context.
17 // If we instead keep a separate context for each level the Log method
18 // must only append the new keyvals; but the With method would have to
19 // update all five contexts.
21 // Roughly speaking, storing multiple contexts breaks even if the ratio of
22 // Log/With calls is more than the number of levels. We have chosen to
23 // make the With method cheap and the Log method a bit more costly because
24 // we do not expect most applications to Log more than five times for each
34 // New creates a new leveled logger, wrapping the passed logger.
35 func New(logger log.Logger, options ...Option) Levels {
46 for _, option := range options {
52 // With returns a new leveled logger that includes keyvals in all log events.
53 func (l Levels) With(keyvals ...interface{}) Levels {
55 logger: log.With(l.logger, keyvals...),
57 debugValue: l.debugValue,
58 infoValue: l.infoValue,
59 warnValue: l.warnValue,
60 errorValue: l.errorValue,
61 critValue: l.critValue,
65 // Debug returns a debug level logger.
66 func (l Levels) Debug() log.Logger {
67 return log.WithPrefix(l.logger, l.levelKey, l.debugValue)
70 // Info returns an info level logger.
71 func (l Levels) Info() log.Logger {
72 return log.WithPrefix(l.logger, l.levelKey, l.infoValue)
75 // Warn returns a warning level logger.
76 func (l Levels) Warn() log.Logger {
77 return log.WithPrefix(l.logger, l.levelKey, l.warnValue)
80 // Error returns an error level logger.
81 func (l Levels) Error() log.Logger {
82 return log.WithPrefix(l.logger, l.levelKey, l.errorValue)
85 // Crit returns a critical level logger.
86 func (l Levels) Crit() log.Logger {
87 return log.WithPrefix(l.logger, l.levelKey, l.critValue)
90 // Option sets a parameter for leveled loggers.
91 type Option func(*Levels)
93 // Key sets the key for the field used to indicate log level. By default,
94 // the key is "level".
95 func Key(key string) Option {
96 return func(l *Levels) { l.levelKey = key }
99 // DebugValue sets the value for the field used to indicate the debug log
100 // level. By default, the value is "debug".
101 func DebugValue(value string) Option {
102 return func(l *Levels) { l.debugValue = value }
105 // InfoValue sets the value for the field used to indicate the info log level.
106 // By default, the value is "info".
107 func InfoValue(value string) Option {
108 return func(l *Levels) { l.infoValue = value }
111 // WarnValue sets the value for the field used to indicate the warning log
112 // level. By default, the value is "warn".
113 func WarnValue(value string) Option {
114 return func(l *Levels) { l.warnValue = value }
117 // ErrorValue sets the value for the field used to indicate the error log
118 // level. By default, the value is "error".
119 func ErrorValue(value string) Option {
120 return func(l *Levels) { l.errorValue = value }
123 // CritValue sets the value for the field used to indicate the critical log
124 // level. By default, the value is "crit".
125 func CritValue(value string) Option {
126 return func(l *Levels) { l.critValue = value }