github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/logger.go (about) 1 package pinpoint 2 3 import ( 4 "os" 5 "strings" 6 7 "github.com/sirupsen/logrus" 8 prefixed "github.com/x-cray/logrus-prefixed-formatter" 9 "gopkg.in/natefinch/lumberjack.v2" 10 ) 11 12 var logger *logrusLogger 13 14 func initLogger() { 15 logger = newLogger() 16 } 17 18 func Log(src string) *logEntry { 19 return logger.newEntry(src) 20 } 21 22 func IsLogLevelEnabled(level logrus.Level) bool { 23 return logger.defaultLogger.GetLevel() >= level 24 } 25 26 func SetExtraLogger(lgr *logrus.Logger) { 27 logger.extraLogger = lgr 28 } 29 30 type logrusLogger struct { 31 defaultLogger *logrus.Logger 32 extraLogger *logrus.Logger 33 fileLogger *lumberjack.Logger 34 config *Config 35 } 36 37 func newLogger() *logrusLogger { 38 l := logrus.New() 39 formatter := new(prefixed.TextFormatter) 40 formatter.TimestampFormat = "2006-01-02 15:04:05.000000" 41 formatter.FullTimestamp = true 42 formatter.ForceFormatting = true 43 formatter.ForceColors = true 44 l.Formatter = formatter 45 return &logrusLogger{defaultLogger: l} 46 } 47 48 func (l *logrusLogger) setLevel(level string) { 49 lvl, err := logrus.ParseLevel(level) 50 if err != nil { 51 Log("config").Errorf("invalid log level: %s", level) 52 lvl = logrus.InfoLevel 53 } 54 55 l.defaultLogger.SetLevel(lvl) 56 reportCaller := false 57 if lvl > logrus.InfoLevel { 58 reportCaller = true 59 } 60 l.defaultLogger.SetReportCaller(reportCaller) 61 } 62 63 func (l *logrusLogger) setOutput(out string, maxSize int) { 64 Log("config").Infof("log output: %s", out) 65 66 if strings.EqualFold(out, "stdout") { 67 l.defaultLogger.SetOutput(os.Stdout) 68 } else if strings.EqualFold(out, "stderr") { 69 l.defaultLogger.SetOutput(os.Stderr) 70 } else { 71 l.fileLogger = &lumberjack.Logger{ 72 Filename: out, 73 MaxSize: maxSize, 74 MaxBackups: 1, 75 MaxAge: 30, 76 Compress: false, 77 } 78 l.defaultLogger.SetOutput(l.fileLogger) 79 } 80 } 81 82 func (l *logrusLogger) setup(config *Config) { 83 l.setOutput(config.String(CfgLogOutput), config.Int(CfgLogMaxSize)) 84 l.setLevel(config.String(CfgLogLevel)) 85 l.config = config 86 } 87 88 func (l *logrusLogger) reloadLevel() { 89 l.setLevel(l.config.String(CfgLogLevel)) 90 } 91 92 func (l *logrusLogger) reloadOutput() { 93 if l.fileLogger != nil { 94 defer func(f *lumberjack.Logger) { 95 f.Close() 96 }(l.fileLogger) 97 } 98 l.setOutput(l.config.String(CfgLogOutput), l.config.Int(CfgLogMaxSize)) 99 } 100 101 func (l *logrusLogger) newEntry(src string) *logEntry { 102 return &logEntry{ 103 entry: logrus.NewEntry(l.defaultLogger).WithFields(logrus.Fields{"module": "pinpoint", "src": src}), 104 extraLogger: l.extraLogger, 105 } 106 } 107 108 type logEntry struct { 109 entry *logrus.Entry 110 extraLogger *logrus.Logger 111 } 112 113 func (l *logEntry) log(logFunc func(string, ...interface{}), format string, args ...interface{}) { 114 logFunc(format, args...) 115 if l.extraLogger != nil { 116 l.entry.Logger = l.extraLogger 117 logFunc(format, args...) 118 } 119 } 120 121 func (l *logEntry) Errorf(format string, args ...interface{}) { 122 l.log(l.entry.Errorf, format, args...) 123 } 124 125 func (l *logEntry) Warnf(format string, args ...interface{}) { 126 l.log(l.entry.Warnf, format, args...) 127 } 128 129 func (l *logEntry) Infof(format string, args ...interface{}) { 130 l.log(l.entry.Infof, format, args...) 131 } 132 133 func (l *logEntry) Debugf(format string, args ...interface{}) { 134 l.log(l.entry.Debugf, format, args...) 135 } 136 137 func (l *logEntry) Tracef(format string, args ...interface{}) { 138 l.log(l.entry.Tracef, format, args...) 139 }