github.com/xmidt-org/webpa-common@v1.11.9/logging/logger.go (about) 1 package logging 2 3 import ( 4 "os" 5 "strings" 6 7 "github.com/go-kit/kit/log" 8 "github.com/go-kit/kit/log/level" 9 ) 10 11 var ( 12 defaultLogger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout)) 13 14 callerKey interface{} = "caller" 15 messageKey interface{} = "msg" 16 errorKey interface{} = "error" 17 timestampKey interface{} = "ts" 18 ) 19 20 // CallerKey returns the logging key to be used for the stack location of the logging call 21 func CallerKey() interface{} { 22 return callerKey 23 } 24 25 // MessageKey returns the logging key to be used for the textual message of the log entry 26 func MessageKey() interface{} { 27 return messageKey 28 } 29 30 // ErrorKey returns the logging key to be used for error instances 31 func ErrorKey() interface{} { 32 return errorKey 33 } 34 35 // TimestampKey returns the logging key to be used for the timestamp 36 func TimestampKey() interface{} { 37 return timestampKey 38 } 39 40 // DefaultLogger returns a global singleton NOP logger. 41 // This returned instance is safe for concurrent access. 42 func DefaultLogger() log.Logger { 43 return defaultLogger 44 } 45 46 // New creates a go-kit Logger from a set of options. The options object can be nil, 47 // in which case a default logger that logs to os.Stdout is returned. The returned logger 48 // includes the timestamp in UTC format and will filter according to the Level field. 49 // 50 // In order to allow arbitrary decoration, this function does not insert the caller information. 51 // Use either DefaultCaller in this package or the go-kit/kit/log API to add a Caller to the 52 // returned Logger. 53 func New(o *Options) log.Logger { 54 return NewFilter( 55 log.WithPrefix( 56 o.loggerFactory()(o.output()), 57 TimestampKey(), log.DefaultTimestampUTC, 58 ), 59 o, 60 ) 61 } 62 63 // NewFilter applies the Options filtering rules in the package to an arbitrary go-kit Logger. 64 func NewFilter(next log.Logger, o *Options) log.Logger { 65 switch strings.ToUpper(o.level()) { 66 case "DEBUG": 67 return level.NewFilter(next, level.AllowDebug()) 68 69 case "INFO": 70 return level.NewFilter(next, level.AllowInfo()) 71 72 case "WARN": 73 return level.NewFilter(next, level.AllowWarn()) 74 75 default: 76 return level.NewFilter(next, level.AllowError()) 77 } 78 } 79 80 // DefaultCaller produces a contextual logger as with log.With, but automatically prepends the 81 // caller under the CallerKey. 82 // 83 // The logger returned by this function should not be further decorated. This will cause the 84 // callstack to include the decorators, which is pointless. Instead, decorate the next parameter 85 // prior to passing it to this function. 86 func DefaultCaller(next log.Logger, keyvals ...interface{}) log.Logger { 87 return log.WithPrefix( 88 next, 89 append([]interface{}{CallerKey(), log.DefaultCaller}, keyvals...)..., 90 ) 91 } 92 93 // Error places both the caller and a constant error level into the prefix of the returned logger. 94 // Additional key value pairs may also be added. 95 func Error(next log.Logger, keyvals ...interface{}) log.Logger { 96 return log.WithPrefix( 97 next, 98 append([]interface{}{CallerKey(), log.DefaultCaller, level.Key(), level.ErrorValue()}, keyvals...)..., 99 ) 100 } 101 102 // Info places both the caller and a constant info level into the prefix of the returned logger. 103 // Additional key value pairs may also be added. 104 func Info(next log.Logger, keyvals ...interface{}) log.Logger { 105 return log.WithPrefix( 106 next, 107 append([]interface{}{CallerKey(), log.DefaultCaller, level.Key(), level.InfoValue()}, keyvals...)..., 108 ) 109 } 110 111 // Warn places both the caller and a constant warn level into the prefix of the returned logger. 112 // Additional key value pairs may also be added. 113 func Warn(next log.Logger, keyvals ...interface{}) log.Logger { 114 return log.WithPrefix( 115 next, 116 append([]interface{}{CallerKey(), log.DefaultCaller, level.Key(), level.WarnValue()}, keyvals...)..., 117 ) 118 } 119 120 // Debug places both the caller and a constant debug level into the prefix of the returned logger. 121 // Additional key value pairs may also be added. 122 func Debug(next log.Logger, keyvals ...interface{}) log.Logger { 123 return log.WithPrefix( 124 next, 125 append([]interface{}{CallerKey(), log.DefaultCaller, level.Key(), level.DebugValue()}, keyvals...)..., 126 ) 127 }