1 // Package levels implements leveled logging on top of Go kit's log package. 2 // 3 // Deprecated: Use github.com/go-kit/log/level instead. 4 package levels 5 6 import "github.com/go-kit/log" 7 8 // Levels provides a leveled logging wrapper around a logger. It has five 9 // levels: debug, info, warning (warn), error, and critical (crit). If you 10 // want a different set of levels, you can create your own levels type very 11 // easily, and you can elide the configuration. 12 type Levels struct { 13 logger log.Logger 14 levelKey string 15 16 // We have a choice between storing level values in string fields or 17 // making a separate context for each level. When using string fields the 18 // Log method must combine the base context, the level data, and the 19 // logged keyvals; but the With method only requires updating one context. 20 // If we instead keep a separate context for each level the Log method 21 // must only append the new keyvals; but the With method would have to 22 // update all five contexts. 23 24 // Roughly speaking, storing multiple contexts breaks even if the ratio of 25 // Log/With calls is more than the number of levels. We have chosen to 26 // make the With method cheap and the Log method a bit more costly because 27 // we do not expect most applications to Log more than five times for each 28 // call to With. 29 30 debugValue string 31 infoValue string 32 warnValue string 33 errorValue string 34 critValue string 35 } 36 37 // New creates a new leveled logger, wrapping the passed logger. 38 func New(logger log.Logger, options ...Option) Levels { 39 l := Levels{ 40 logger: logger, 41 levelKey: "level", 42 43 debugValue: "debug", 44 infoValue: "info", 45 warnValue: "warn", 46 errorValue: "error", 47 critValue: "crit", 48 } 49 for _, option := range options { 50 option(&l) 51 } 52 return l 53 } 54 55 // With returns a new leveled logger that includes keyvals in all log events. 56 func (l Levels) With(keyvals ...interface{}) Levels { 57 return Levels{ 58 logger: log.With(l.logger, keyvals...), 59 levelKey: l.levelKey, 60 debugValue: l.debugValue, 61 infoValue: l.infoValue, 62 warnValue: l.warnValue, 63 errorValue: l.errorValue, 64 critValue: l.critValue, 65 } 66 } 67 68 // Debug returns a debug level logger. 69 func (l Levels) Debug() log.Logger { 70 return log.WithPrefix(l.logger, l.levelKey, l.debugValue) 71 } 72 73 // Info returns an info level logger. 74 func (l Levels) Info() log.Logger { 75 return log.WithPrefix(l.logger, l.levelKey, l.infoValue) 76 } 77 78 // Warn returns a warning level logger. 79 func (l Levels) Warn() log.Logger { 80 return log.WithPrefix(l.logger, l.levelKey, l.warnValue) 81 } 82 83 // Error returns an error level logger. 84 func (l Levels) Error() log.Logger { 85 return log.WithPrefix(l.logger, l.levelKey, l.errorValue) 86 } 87 88 // Crit returns a critical level logger. 89 func (l Levels) Crit() log.Logger { 90 return log.WithPrefix(l.logger, l.levelKey, l.critValue) 91 } 92 93 // Option sets a parameter for leveled loggers. 94 type Option func(*Levels) 95 96 // Key sets the key for the field used to indicate log level. By default, 97 // the key is "level". 98 func Key(key string) Option { 99 return func(l *Levels) { l.levelKey = key } 100 } 101 102 // DebugValue sets the value for the field used to indicate the debug log 103 // level. By default, the value is "debug". 104 func DebugValue(value string) Option { 105 return func(l *Levels) { l.debugValue = value } 106 } 107 108 // InfoValue sets the value for the field used to indicate the info log level. 109 // By default, the value is "info". 110 func InfoValue(value string) Option { 111 return func(l *Levels) { l.infoValue = value } 112 } 113 114 // WarnValue sets the value for the field used to indicate the warning log 115 // level. By default, the value is "warn". 116 func WarnValue(value string) Option { 117 return func(l *Levels) { l.warnValue = value } 118 } 119 120 // ErrorValue sets the value for the field used to indicate the error log 121 // level. By default, the value is "error". 122 func ErrorValue(value string) Option { 123 return func(l *Levels) { l.errorValue = value } 124 } 125 126 // CritValue sets the value for the field used to indicate the critical log 127 // level. By default, the value is "crit". 128 func CritValue(value string) Option { 129 return func(l *Levels) { l.critValue = value } 130 } 131