-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog.go
192 lines (166 loc) · 4.62 KB
/
log.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package log
import (
"fmt"
"os"
"sync"
"github.com/sirupsen/logrus"
)
// Level type
type Level uint32
type Fields map[string]interface{}
// These are the different logging levels. You can set the logging level to log
// on your instance of logger, obtained with `logrus.New()`.
const (
// PanicLevel level, highest level of severity. Logs and then calls panic with the
// message passed to Debug, Info, ...
PanicLevel Level = iota
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
// logging level is set to Panic.
FatalLevel
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service.
ErrorLevel
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel
// InfoLevel level. General operational entries about what's going on inside the
// application.
InfoLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel
// TraceLevel level. Designates finer-grained informational events than the Debug.
TraceLevel
)
const (
maximumCallerDepth int = 25
knownLogrusFrames int = 4
timeFormat = "2006-01-02 15:04:05.000"
)
var (
// Used for caller information initialisation
callerInitOnce sync.Once
logrusPackage string
minimumCallerDepth = 1
loggers = make(map[string]*MyLogger)
loggersLock sync.RWMutex
defaultLogger = NewLogger(DebugLevel, "default")
)
// Infof logs a formatted info level log to the console
func Infof(format string, v ...interface{}) { defaultLogger.Infof(format, v...) }
// Tracef logs a formatted debug level log to the console
func Tracef(format string, v ...interface{}) { defaultLogger.Tracef(format, v...) }
// Debugf logs a formatted debug level log to the console
func Debugf(format string, v ...interface{}) { defaultLogger.Debugf(format, v...) }
// Warnf logs a formatted warn level log to the console
func Warnf(format string, v ...interface{}) { defaultLogger.Warnf(format, v...) }
// Errorf logs a formatted error level log to the console
func Errorf(format string, v ...interface{}) { defaultLogger.Errorf(format, v...) }
// Panicf logs a formatted panic level log to the console.
// The panic() function is called, which stops the ordinary flow of a goroutine.
func Panicf(format string, v ...interface{}) { defaultLogger.Panicf(format, v...) }
func Init(level string) {
defaultLogger.SetLevel(logrus.Level(StringToLevel(level)))
}
func StringToLevel(level string) Level {
l := logrus.DebugLevel
switch level {
case "trace":
l = logrus.TraceLevel
case "debug":
l = logrus.DebugLevel
case "info":
l = logrus.InfoLevel
case "warn":
l = logrus.WarnLevel
case "error":
l = logrus.ErrorLevel
}
return Level(l)
}
type MyLogger struct {
logger *logrus.Logger
level Level
prefix string
}
func (ml *MyLogger) Level() string {
switch ml.level {
case PanicLevel:
return "Panic"
case FatalLevel:
return "Fatal"
case ErrorLevel:
return "Error"
case WarnLevel:
return "Warn"
case InfoLevel:
return "Info"
case DebugLevel:
return "Debug"
case TraceLevel:
return "Trace"
}
return "Unkown"
}
func (ml *MyLogger) Prefix() string {
return ml.prefix
}
func (ml *MyLogger) SetLevel(level Level) {
ml.logger.SetLevel(logrus.Level(level))
}
func NewLogger(level Level, prefix string) *logrus.Logger {
loggersLock.RLock()
if logger, found := loggers[prefix]; found {
loggersLock.RUnlock()
return logger.logger
}
loggersLock.RUnlock()
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetReportCaller(true)
l.SetLevel(logrus.Level(level))
l.SetFormatter(&TextFormatter{
Prefix: prefix,
FullTimestamp: true,
TimestampFormat: timeFormat,
ForceFormatting: true,
})
loggersLock.Lock()
loggers[prefix] = &MyLogger{
logger: l,
level: level,
prefix: prefix,
}
loggersLock.Unlock()
return l
}
func NewLoggerWithFields(level Level, prefix string, fields Fields) *logrus.Logger {
if logger, found := loggers[prefix]; found {
return logger.logger
}
l := logrus.New()
l.SetOutput(os.Stdout)
l.SetReportCaller(true)
l.SetLevel(logrus.Level(level))
l.SetFormatter(&TextFormatter{
Prefix: prefix,
Fields: fields,
FullTimestamp: true,
TimestampFormat: timeFormat,
})
loggers[prefix] = &MyLogger{
logger: l,
level: level,
prefix: prefix,
}
return l
}
func SetLogLevel(prefix string, level Level) error {
if l, found := loggers[prefix]; found {
l.level = level
l.logger.SetLevel(logrus.Level(level))
return nil
}
return fmt.Errorf("logger [%v] not found", prefix)
}
func GetLoggers() map[string]*MyLogger {
return loggers
}