-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzerologger.go
179 lines (157 loc) · 4.85 KB
/
zerologger.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package log
import (
"io"
"os"
"time"
"encoding/json"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
)
// ZeroLogger implemented logger using zerolog
type ZeroLogger struct {
logger zerolog.Logger
logFile *os.File
callerLevel zerolog.Level
}
// GetZeroLogger returns the zero logger instance for advanced usage
func (l *ZeroLogger) GetZeroLogger() zerolog.Logger {
return l.logger
}
const defaultCallSkip = 3
// Rawf implements raw logger interface
func (l *ZeroLogger) Rawf(rawMessage []byte, format string, v ...interface{}) {
l.rawf(defaultCallSkip, rawMessage, format, v...)
}
func (l *ZeroLogger) rawf(callSkip int, rawMessage []byte, format string, v ...interface{}) {
event := l.logger.WithLevel(zerolog.NoLevel).Caller(callSkip)
if json.Valid(rawMessage) {
rawMessageInJSON := json.RawMessage(rawMessage)
event.Interface("raw", rawMessageInJSON)
} else {
event.Bytes("raw", rawMessage)
}
if len(v) == 0 {
event.Msg(format)
} else {
event.Msgf(format, v...)
}
}
// Debugf implements debug logger interface
func (l *ZeroLogger) Debugf(format string, v ...interface{}) {
l.debugf(defaultCallSkip, format, v...)
}
func (l *ZeroLogger) debugf(callSkip int, format string, v ...interface{}) {
eventf(l.logger.Debug(), zerolog.DebugLevel >= l.callerLevel, false, callSkip, format, v...)
}
// Infof implements info logger interface
func (l *ZeroLogger) Infof(format string, v ...interface{}) {
l.infof(defaultCallSkip, format, v...)
}
func (l *ZeroLogger) infof(callSkip int, format string, v ...interface{}) {
eventf(l.logger.Info(), zerolog.InfoLevel >= l.callerLevel, false, callSkip, format, v...)
}
// Errorf implements error logger interface
func (l *ZeroLogger) Errorf(err error, format string, v ...interface{}) {
l.errorf(defaultCallSkip, err, format, v...)
}
func (l *ZeroLogger) errorf(callSkip int, err error, format string, v ...interface{}) {
withCaller := zerolog.ErrorLevel >= l.callerLevel
eventf(l.logger.Error().Err(err), withCaller, withCaller, callSkip, format, v...)
}
// Fatalf make a fatal return
func (l *ZeroLogger) Fatalf(err error, format string, v ...interface{}) {
l.fatalf(defaultCallSkip, err, format, v...)
}
func (l *ZeroLogger) fatalf(callSkip int, err error, format string, v ...interface{}) {
eventf(l.logger.Panic().Err(err), true, true, callSkip, format, v...)
}
func eventf(event *zerolog.Event, withCaller, withStack bool, callSkip int, format string, v ...interface{}) {
if withCaller {
event = event.Caller(callSkip)
}
if withStack {
event = event.Stack()
}
if len(v) == 0 {
event.Msg(format)
} else {
event.Msgf(format, v...)
}
}
// LoggingConfig helper for a logging destination
type LoggingConfig struct {
// Service name
Service string
// Name running instance name
Name string
// Level logging level
Level zerolog.Level
// CallerLevel caller setting level
CallerLevel zerolog.Level
// Disable console color in debug mode
DisableConsoleColor bool
// Stdout log json in stdout
Stdout bool
// FileDir write log to dir
FileDir string
// TimeFormat log time format
TimeFormat string
}
// MakeSimpleZeroLogger create a new simple logger using zero logger
func MakeSimpleZeroLogger() *ZeroLogger {
return &ZeroLogger{logger: log.Logger}
}
// MakeZeroLogger create a new logger using zero logger
func MakeZeroLogger(c LoggingConfig) (*ZeroLogger, error) {
l := ZeroLogger{}
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.DisableSampling(true)
if len(c.TimeFormat) == 0 || c.TimeFormat == "timestamp" || c.TimeFormat == "unix" || c.TimeFormat == "cls" {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
} else if c.TimeFormat == "sls" {
zerolog.TimeFieldFormat = "2006-01-02T15:04:05.999Z07:00"
} else {
zerolog.TimeFieldFormat = c.TimeFormat
}
zerolog.TimestampFunc = func() time.Time { return time.Now().UTC() }
var err error
logWriters := make([]io.Writer, 0, 3)
if len(c.FileDir) > 0 {
logName := c.Name
if len(logName) == 0 {
logName = c.Service
}
l.logFile, err = OpenLogFile(c.FileDir, logName)
if err != nil {
return nil, err
}
logWriters = append(logWriters, l.logFile)
}
zerolog.SetGlobalLevel(c.Level)
if c.Stdout {
logWriters = append(logWriters, os.Stdout)
} else if c.Level == zerolog.DebugLevel {
logWriters = append(logWriters, zerolog.ConsoleWriter{Out: os.Stderr, NoColor: c.DisableConsoleColor})
}
if len(logWriters) == 0 {
return nil, errors.New("no log writer available")
}
logContext := zerolog.
New(zerolog.MultiLevelWriter(logWriters...)).
With().Timestamp().Str("service", c.Service)
if len(c.Name) > 0 {
logContext = logContext.Str("process", c.Name)
}
l.logger = logContext.Logger()
l.callerLevel = c.CallerLevel
return &l, nil
}
// CloseLogger close the logger
func (l *ZeroLogger) CloseLogger() error {
if l.logFile != nil {
return l.logFile.Close()
}
return nil
}