github.com/tuingking/flamingo@v0.0.0-20220403134817-2796ae0e84ca/infra/logger/logger.go (about) 1 package logger 2 3 import ( 4 "os" 5 "runtime" 6 7 "github.com/sirupsen/logrus" 8 ) 9 10 type Logger interface { 11 Debug(args ...interface{}) 12 Debugf(format string, args ...interface{}) 13 Info(args ...interface{}) 14 Infof(format string, args ...interface{}) 15 Warn(args ...interface{}) 16 Warnf(format string, args ...interface{}) 17 Error(args ...interface{}) 18 Errorf(format string, args ...interface{}) 19 Fatal(args ...interface{}) 20 Fatalf(format string, args ...interface{}) 21 } 22 23 type logger struct { 24 log *logrus.Logger 25 } 26 27 type Config struct { 28 Format string 29 Level string 30 } 31 32 func New(cfg Config) Logger { 33 log := logrus.New() 34 log.SetOutput(os.Stdout) 35 log.SetReportCaller(false) 36 37 // formatter 38 switch cfg.Format { 39 case "json": 40 log.SetFormatter(&logrus.JSONFormatter{}) 41 default: 42 log.SetFormatter(&logrus.TextFormatter{ 43 DisableColors: false, 44 TimestampFormat: "2006-01-02 15:04:05.000", 45 FullTimestamp: true, 46 }) 47 } 48 49 // level 50 switch cfg.Level { 51 case "panic": 52 log.SetLevel(logrus.PanicLevel) 53 case "fatal": 54 log.SetLevel(logrus.FatalLevel) 55 case "error": 56 log.SetLevel(logrus.ErrorLevel) 57 case "warn": 58 log.SetLevel(logrus.WarnLevel) 59 case "info": 60 log.SetLevel(logrus.InfoLevel) 61 case "debug": 62 log.SetLevel(logrus.DebugLevel) 63 case "trace": 64 log.SetLevel(logrus.TraceLevel) 65 default: 66 log.SetLevel(logrus.DebugLevel) 67 } 68 69 logrus.Infof("%-7s %s", "Logger", "✅") 70 71 return &logger{ 72 log: log, 73 } 74 } 75 76 func (l *logger) Debug(args ...interface{}) { 77 l.log.WithField("goroutines", runtime.NumGoroutine()).Debug(args...) 78 } 79 80 func (l *logger) Debugf(format string, args ...interface{}) { 81 l.log.WithField("goroutines", runtime.NumGoroutine()).Debugf(format, args...) 82 } 83 84 func (l *logger) Info(args ...interface{}) { 85 l.log.WithField("goroutines", runtime.NumGoroutine()).Info(args...) 86 } 87 88 func (l *logger) Infof(format string, args ...interface{}) { 89 l.log.WithField("goroutines", runtime.NumGoroutine()).Infof(format, args...) 90 } 91 92 func (l *logger) Warn(args ...interface{}) { 93 l.log.WithField("goroutines", runtime.NumGoroutine()).Warn(args...) 94 } 95 96 func (l *logger) Warnf(format string, args ...interface{}) { 97 l.log.WithField("goroutines", runtime.NumGoroutine()).Warnf(format, args...) 98 } 99 100 func (l *logger) Error(args ...interface{}) { 101 l.log.WithField("goroutines", runtime.NumGoroutine()).Error(args...) 102 } 103 104 func (l *logger) Errorf(format string, args ...interface{}) { 105 l.log.WithField("goroutines", runtime.NumGoroutine()).Errorf(format, args...) 106 } 107 108 func (l *logger) Fatal(args ...interface{}) { 109 l.log.WithField("goroutines", runtime.NumGoroutine()).Fatal(args...) 110 } 111 112 func (l *logger) Fatalf(format string, args ...interface{}) { 113 l.log.WithField("goroutines", runtime.NumGoroutine()).Fatalf(format, args...) 114 }