logrus.go
3.5 KB
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
package log
import (
"github.com/linmadan/egglib-go/log"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"io"
"os"
)
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.WarnLevel)
}
type LogrusLogger struct {
serviceName string
logrus *logrus.Logger
}
func (logger *LogrusLogger) SetServiceName(serviceName string) {
logger.serviceName = serviceName
}
func (logger *LogrusLogger) SetLevel(level string) {
switch level {
case "trace":
logger.logrus.Level = logrus.TraceLevel
case "debug":
logger.logrus.Level = logrus.DebugLevel
case "info":
logger.logrus.Level = logrus.InfoLevel
case "warn":
logger.logrus.Level = logrus.WarnLevel
case "error":
logger.logrus.Level = logrus.ErrorLevel
case "fatal":
logger.logrus.Level = logrus.FatalLevel
case "panic":
logger.logrus.Level = logrus.PanicLevel
default:
logger.logrus.Level = logrus.DebugLevel
}
}
func (logger *LogrusLogger) AddHook(w io.Writer) {
level := logger.logrus.Level
var levels []logrus.Level
// 默认已经添加了一个当前log level的hook,所以此处 level+1
for i := 0; i <= (int(level)); i++ {
levels = append(levels, logrus.Level(i))
}
logger.logrus.AddHook(&writer.Hook{
Writer: w,
LogLevels: levels,
})
}
func (logger *LogrusLogger) Trace(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Trace(msg)
}
func (logger *LogrusLogger) Debug(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Debug(msg)
}
func (logger *LogrusLogger) Info(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Info(msg)
}
func (logger *LogrusLogger) Warn(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Warn(msg)
}
func (logger *LogrusLogger) Error(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Error(msg)
}
func (logger *LogrusLogger) Fatal(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Fatal(msg)
}
func (logger *LogrusLogger) Panic(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Panic(msg)
}
func NewLogrusLogger() *LogrusLogger {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
logger.Out = os.Stdout
return &LogrusLogger{
logrus: logger,
}
}
var _ log.Logger = (*LogrusLogger)(nil)