github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/logging/logrus.go (about) 1 package logging 2 3 import ( 4 "os" 5 6 "github.com/sirupsen/logrus" 7 ) 8 9 // NewLogrusFormat makes a new Interface backed by a logrus logger 10 // format can be "json" or defaults to logfmt 11 func NewLogrusFormat(level Level, f Format) Interface { 12 log := logrus.New() 13 log.Out = os.Stderr 14 log.Level = level.Logrus 15 log.Formatter = f.Logrus 16 return logrusLogger{log} 17 } 18 19 // NewLogrus makes a new Interface backed by a logrus logger 20 func NewLogrus(level Level) Interface { 21 return NewLogrusFormat(level, Format{Logrus: &logrus.TextFormatter{}}) 22 } 23 24 // Logrus wraps an existing Logrus logger. 25 func Logrus(l *logrus.Logger) Interface { 26 return logrusLogger{l} 27 } 28 29 type logrusLogger struct { 30 *logrus.Logger 31 } 32 33 func (l logrusLogger) WithField(key string, value interface{}) Interface { 34 return logrusEntry{ 35 Entry: l.Logger.WithField(key, value), 36 } 37 } 38 39 func (l logrusLogger) WithFields(fields Fields) Interface { 40 return logrusEntry{ 41 Entry: l.Logger.WithFields(map[string]interface{}(fields)), 42 } 43 } 44 45 type logrusEntry struct { 46 *logrus.Entry 47 } 48 49 func (l logrusEntry) WithField(key string, value interface{}) Interface { 50 return logrusEntry{ 51 Entry: l.Entry.WithField(key, value), 52 } 53 } 54 55 func (l logrusEntry) WithFields(fields Fields) Interface { 56 return logrusEntry{ 57 Entry: l.Entry.WithFields(map[string]interface{}(fields)), 58 } 59 }