-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoverlogger.go
134 lines (103 loc) · 2.85 KB
/
overlogger.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
package over
import (
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
)
var overLogger *Overlog
type Overlog struct {
log *zerolog.Logger
}
func New(logger zerolog.Logger) *Overlog {
log.Logger = logger.Hook(MDCHook{})
ResetGlobalMdcAdapter()
ClearGlobalFields()
overLogger = &Overlog{
log: &log.Logger,
}
return overLogger
}
func NewDefault() *Overlog {
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Caller().Logger().Hook(MDCHook{})
ResetGlobalMdcAdapter()
ClearGlobalFields()
zerolog.TimeFieldFormat = "2006-01-02 15:04:05,000"
zerolog.LevelFieldMarshalFunc = UppercaseLevelEncoder()
zerolog.CallerMarshalFunc = ShortCallerEncoder()
zerolog.CallerFieldName = "source"
zerolog.CallerSkipFrameCount = 3
overLogger = &Overlog{
log: &log.Logger,
}
return overLogger
}
func Log() *Overlog {
return overLogger
}
func Z() *zerolog.Logger {
return overLogger.log
}
func (o *Overlog) Trace(args ...interface{}) {
o.log.Trace().Msg(message("", args...))
}
func (o *Overlog) Tracef(format string, args ...interface{}) {
o.log.Trace().Msg(message(format, args...))
}
func (o *Overlog) Debug(args ...interface{}) {
o.log.Debug().Msg(message("", args...))
}
func (o *Overlog) Debugf(format string, args ...interface{}) {
o.log.Debug().Msg(message(format, args...))
}
func (o *Overlog) Info(args ...interface{}) {
o.log.Info().Msg(message("", args...))
}
func (o *Overlog) Infof(format string, args ...interface{}) {
o.log.Info().Msg(message(format, args...))
}
func (o *Overlog) Warn(args ...interface{}) {
o.log.Warn().Msg(message("", args...))
}
func (o *Overlog) Warnf(format string, args ...interface{}) {
o.log.Warn().Msg(message(format, args...))
}
func (o *Overlog) Error(args ...interface{}) {
o.log.Error().Msg(message("", args...))
}
func (o *Overlog) Errorf(format string, args ...interface{}) {
o.log.Error().Msg(message(format, args...))
}
func (o *Overlog) Fatal(args ...interface{}) {
o.log.Fatal().Msg(message("", args...))
}
func (o *Overlog) Fatalf(format string, args ...interface{}) {
o.log.Fatal().Msg(message(format, args...))
}
func (o *Overlog) Panic(args ...interface{}) {
o.log.Panic().Msg(message("", args...))
}
func (o *Overlog) Panicf(format string, args ...interface{}) {
o.log.Panic().Msg(message(format, args...))
}
func (o *Overlog) Log(args ...interface{}) {
o.log.Log().Msg(message("", args...))
}
func (o *Overlog) Logf(format string, args ...interface{}) {
o.log.Log().Msg(message(format, args...))
}
func (o *Overlog) Print(args ...interface{}) {
o.log.Print(args...)
}
func (o *Overlog) Printf(format string, args ...interface{}) {
o.log.Printf(format, args...)
}
func message(format string, args ...interface{}) string {
msg := format
if msg == "" && len(args) > 0 {
msg = fmt.Sprint(args...)
} else if msg != "" && len(args) > 0 {
msg = fmt.Sprintf(format, args...)
}
return msg
}