github.com/tcncloud/wollemi@v0.8.1/adapters/logrus/logger.go (about) 1 package logrus 2 3 import ( 4 "io" 5 "os" 6 7 "github.com/sirupsen/logrus" 8 9 "github.com/tcncloud/wollemi/ports/logging" 10 ) 11 12 func NewLogger(out io.Writer) *Logger { 13 log := &logrus.Logger{ 14 Formatter: &logrus.TextFormatter{}, 15 Level: logrus.InfoLevel, 16 ExitFunc: os.Exit, 17 Out: out, 18 } 19 20 return &Logger{ 21 entry: logrus.NewEntry(log), 22 logrus: log, 23 } 24 } 25 26 type Logger struct { 27 logrus *logrus.Logger 28 entry *logrus.Entry 29 } 30 31 func (this *Logger) WithError(err error) logging.Logger { 32 return &Logger{ 33 entry: this.entry.WithError(err), 34 logrus: this.logrus, 35 } 36 } 37 38 func (this *Logger) WithFields(fields logging.Fields) logging.Logger { 39 return &Logger{ 40 entry: this.entry.WithFields(logrus.Fields(fields)), 41 logrus: this.logrus, 42 } 43 } 44 45 func (this *Logger) WithField(keys string, value interface{}) logging.Logger { 46 return &Logger{ 47 entry: this.entry.WithField(keys, value), 48 logrus: this.logrus, 49 } 50 } 51 52 func (this *Logger) Infof(format string, args ...interface{}) { 53 this.entry.Infof(format, args...) 54 } 55 56 func (this *Logger) Info(args ...interface{}) { 57 this.entry.Info(args...) 58 } 59 60 func (this *Logger) Warnf(format string, args ...interface{}) { 61 this.entry.Warnf(format, args...) 62 } 63 64 func (this *Logger) Warn(args ...interface{}) { 65 this.entry.Warn(args...) 66 } 67 68 func (this *Logger) Error(args ...interface{}) { 69 this.entry.Error(args...) 70 } 71 72 func (this *Logger) Debugf(format string, args ...interface{}) { 73 this.entry.Debugf(format, args...) 74 } 75 76 func (this *Logger) Debug(args ...interface{}) { 77 this.entry.Debug(args...) 78 } 79 80 func (this *Logger) GetLevel() logging.Level { 81 return logging.Level(this.logrus.GetLevel()) 82 } 83 84 func (this *Logger) SetLevel(lvl logging.Level) { 85 switch lvl { 86 case logging.PanicLevel: 87 this.logrus.SetLevel(logrus.PanicLevel) 88 case logging.FatalLevel: 89 this.logrus.SetLevel(logrus.FatalLevel) 90 case logging.ErrorLevel: 91 this.logrus.SetLevel(logrus.ErrorLevel) 92 case logging.WarnLevel: 93 this.logrus.SetLevel(logrus.WarnLevel) 94 case logging.InfoLevel: 95 this.logrus.SetLevel(logrus.InfoLevel) 96 case logging.DebugLevel: 97 this.logrus.SetLevel(logrus.DebugLevel) 98 case logging.TraceLevel: 99 this.logrus.SetLevel(logrus.TraceLevel) 100 } 101 } 102 103 func (*Logger) Exit(code int) { 104 logrus.Exit(code) 105 } 106 107 func (this *Logger) SetFormatter(formatter logging.Formatter) { 108 switch formatter.(type) { 109 case *logging.TextFormatter: 110 this.logrus.SetFormatter(&logrus.TextFormatter{}) 111 case *logging.JsonFormatter: 112 this.logrus.SetFormatter(&logrus.JSONFormatter{}) 113 } 114 }