/
logger.go
66 lines (56 loc) · 1.81 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
package log
// Logger provide common logging methods
// the logger will call `ILogAdapter.Log` to write actual log item
type Logger struct {
adapter ILogAdapter
}
// NewLogger create logger instance
func NewLogger(adapter ILogAdapter) *Logger {
return &Logger{adapter}
}
// Log a generic method that write log with level and msg
// you can pass-in the list of logging arguments to add more information into the log item
// Use some sugar method like: WithField, WithError, WithFormatArgs
// the level and the msg cannot be override by `Arg` function
func (t *Logger) Log(level Level, msg string, args ...Arg) {
fields := Fields{}
for _, arg := range args {
arg(fields)
}
fields[FieldKeyLevel] = level
fields[FieldKeyMessage] = msg
t.adapter.Log(fields)
}
// Trace log msg with "trace" level
func (t *Logger) Trace(msg string, args ...Arg) {
t.Log(LevelTrace, msg, args...)
}
// Debug log msg with "debug" level
func (t *Logger) Debug(msg string, args ...Arg) {
t.Log(LevelDebug, msg, args...)
}
// Info log msg with "info" level
func (t *Logger) Info(msg string, args ...Arg) {
t.Log(LevelInfo, msg, args...)
}
// Warn log msg with "warn" level
func (t *Logger) Warn(msg string, args ...Arg) {
t.Log(LevelWarn, msg, args...)
}
// Error log msg with "error" level
// use WithError to attach the error
func (t *Logger) Error(msg string, args ...Arg) {
t.Log(LevelError, msg, args...)
}
// Panic log msg with "panic" level
// use WithError to attach the error
// some log engine will call `panic` when logging this level
func (t *Logger) Panic(msg string, args ...Arg) {
t.Log(LevelPanic, msg, args...)
}
// Fatal log msg with "fatal" level
// use WithError to attach the error
// some log engine will call `os.Exit` when logging this level
func (t *Logger) Fatal(msg string, args ...Arg) {
t.Log(LevelFatal, msg, args...)
}