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