-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
72 lines (58 loc) · 1.49 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
package logrusx
import (
"github.com/illuminatingKong/kongming-kit/base/logx"
"github.com/illuminatingKong/kongming-kit/base/msg"
"github.com/sirupsen/logrus"
)
type Logger struct {
name string
*logrus.Entry
}
type Option interface {
get() interface{}
}
type setNameOption string
type JsonFormatter logrus.Formatter
type option struct{ value interface{} }
func (o *option) get() interface{} { return o.value }
type PathMap map[logrus.Level]string
func WithFormatter(l JsonFormatter) Option {
l = &logrus.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05.000",
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "@level",
},
}
return &option{l}
}
func (l Logger) AddHook(hook logrus.Hook) {
l.Entry.Logger.AddHook(hook)
}
func (l Logger) WithFieldsX(f func(args ...interface{}), m string, keyvals ...interface{}) {
f(msg.LogMessage(m, keyvals...))
}
func New(options ...Option) logx.Logger {
log := logrus.New()
logger := &Logger{"", logrus.NewEntry(log)}
for _, opt := range options {
processOptions(log, logger, opt.get())
}
return logger
}
func (l Logger) Sub(name string) logx.Logger {
if len(l.name) > 0 {
name = l.name + "." + name
}
return &Logger{name, l.Entry.WithField("module", name)}
}
func processOptions(logr *logrus.Logger, logger *Logger, opt interface{}) {
switch val := opt.(type) {
case setNameOption:
logger.name = string(val)
case logrus.Level:
logr.SetLevel(val)
case JsonFormatter:
logr.SetFormatter(val)
}
}