-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
80 lines (68 loc) · 2.23 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
package decision
import (
"github.com/sirupsen/logrus"
)
var logger Logger = NewDefaultLogger()
// Level type is an iota that represents log level
type Level uint32
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
)
// Logger interface represent an object that can log a string and specify the minimal level for the output
type Logger interface {
Logf(level Level, format string, args ...interface{})
SetLevel(level Level)
}
// SetLogger sets the current logger
func SetLogger(l Logger) {
logger = l
}
// SetLevel sets the level of the current logger
func SetLevel(level Level) {
logger.SetLevel(level)
}
var lvlLogrusMap = map[Level]logrus.Level{
PanicLevel: logrus.PanicLevel,
FatalLevel: logrus.FatalLevel,
ErrorLevel: logrus.ErrorLevel,
WarnLevel: logrus.WarnLevel,
InfoLevel: logrus.InfoLevel,
DebugLevel: logrus.DebugLevel,
TraceLevel: logrus.TraceLevel,
}
// DefaultLogger is a logrus based logger
type DefaultLogger struct {
*logrus.Entry
}
// Logf logs a formatted string and specify its level
func (l *DefaultLogger) Logf(level Level, format string, args ...interface{}) {
l.Entry.Logf(lvlLogrusMap[level], format, args...)
}
// SetLevel specifies the minimum log level to log to the logging output
func (l *DefaultLogger) SetLevel(level Level) {
l.Logger.SetLevel(lvlLogrusMap[level])
}
func NewDefaultLogger() *DefaultLogger {
entry := logrus.New().WithField("component", "common")
entry.Logger.SetLevel(logrus.ErrorLevel)
return &DefaultLogger{
Entry: entry,
}
}