-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
287 lines (241 loc) · 6.99 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package elio
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
// LogDump dump
LogDump = "dump"
// LogName name
LogName = "name"
// LogProject project
LogProject = "project"
// LogService service
LogService = "service"
// LogStage stage
LogStage = "stage"
// LogSuid server unique id
LogSuid = "suid"
// LogHost host
LogHost = "host"
// LogObject object
LogObject = "object"
// LogSession session
LogSession = "session"
// LogUuid user unique id
LogUuid = "uuid"
// LogIP ip
LogIP = "ip"
// LogCode code
LogCode = "code"
// LogMatchKey match key
LogMatchKey = "matchkey"
// LogState state
LogState = "state"
// LogWalltime walltime
LogWalltime = "walltime"
// LogErrorCode errorcode
LogErrorCode = "errorcode"
// LogRoom room
LogRoom = "room"
// LogContext context
LogContext = "context"
// LogPayload payload
LogPayload = "payload"
// LogGitHash git last commit hash
LogGitHash = "githash"
// LogRecover recover
LogRecover = "recover"
)
// InitLog init log
func InitLog(level string, outs []string, json bool) {
zerolog.TimestampFieldName = "logtime"
zerolog.TimeFieldFormat = time.RFC3339Nano
if l, e := zerolog.ParseLevel(strings.ToLower(level)); nil == e {
zerolog.SetGlobalLevel(l)
}
var writers []io.Writer
if 0 < len(outs) {
for _, o := range outs {
if "stdout" == o {
if true == json {
writers = append(writers, os.Stdout)
} else {
writers = append(writers, zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
}
} else if "stderr" == o {
writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
} else {
if f, err := os.OpenFile(o, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); nil == err {
// Add file and line number to log
writers = append(writers, f)
}
}
}
} else {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
o := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339, NoColor: false}
//o.FormatLevel = func(i interface{}) string {
// return strings.ToUpper(fmt.Sprintf("%-6s", i))
//}
//o.FormatMessage = func(i interface{}) string {
// return fmt.Sprintf("* %s *", i)
//}
//o.FormatFieldName = func(i interface{}) string {
// return fmt.Sprintf("%s:", i)
//}
o.FormatFieldValue = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("%s", i))
}
o.FormatCaller = func(i interface{}) string {
t := fmt.Sprintf("%s", i)
s := strings.Split(t, ":")
if 2 != len(s) {
return t
}
f := filepath.Base(s[0])
return f + ":" + s[1]
}
writers = append(writers, o)
}
log.Logger = log.With().Caller().Logger()
if 0 < len(writers) {
log.Logger = log.Output(io.MultiWriter(writers...))
}
}
// WithLevel with level
func WithLevel(level zerolog.Level) *zerolog.Event {
return log.WithLevel(level)
}
// PanicEnabled panic enabled
func PanicEnabled() bool {
return log.Panic().Enabled()
}
// FatalEnabled fatal enabled
func FatalEnabled() bool {
return log.Fatal().Enabled()
}
// ErrorEnabled error enabled
func ErrorEnabled() bool {
return log.Error().Enabled()
}
// WarnEnabled debug enabled
func WarnEnabled() bool {
return log.Warn().Enabled()
}
// InfoEnabled info enabled
func InfoEnabled() bool {
return log.Info().Enabled()
}
// DebugEnabled debug enabled
func DebugEnabled() bool {
return log.Debug().Enabled()
}
// TraceEnabled debug enabled
func TraceEnabled() bool {
return log.Trace().Enabled()
}
// LogPanic app log panic
func LogPanic(name string) *zerolog.Event {
return log.Panic().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// LogFatal app log fatal
func LogFatal(name string) *zerolog.Event {
return log.Fatal().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// LogError app log error
func LogError(name string) *zerolog.Event {
return log.Error().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// LogWarn app log warn
func LogWarn(name string) *zerolog.Event {
return log.Warn().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// LogInfo app log info
func LogInfo(name string) *zerolog.Event {
return log.Info().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// LogDebug app log debug
func LogDebug(name string) *zerolog.Event {
return log.Debug().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// LogTrace app log trace
func LogTrace(name string) *zerolog.Event {
return log.Trace().
Str(LogName, name).
Str(LogProject, LogParams().GetProject()).
Str(LogService, LogParams().GetService()).
Str(LogHost, LogParams().GetHostname())
}
// AppPanic app log panic
func AppPanic() *zerolog.Event { return LogPanic("app") }
// AppFatal app log fatal
func AppFatal() *zerolog.Event { return LogFatal("app") }
// AppError app log error
func AppError() *zerolog.Event { return LogError("app") }
// AppWarn app log warn
func AppWarn() *zerolog.Event { return LogWarn("app") }
// AppInfo app log info
func AppInfo() *zerolog.Event { return LogInfo("app") }
// AppDebug app log debug
func AppDebug() *zerolog.Event { return LogDebug("app") }
// AppTrace app log trace
func AppTrace() *zerolog.Event { return LogTrace("app") }
// PacketPanic packet log panic
func PacketPanic() *zerolog.Event { return LogPanic("packet") }
// PacketFatal packet log fatal
func PacketFatal() *zerolog.Event { return LogFatal("packet") }
// PacketError packet log error
func PacketError() *zerolog.Event { return LogError("packet") }
// PacketWarn packet log warn
func PacketWarn() *zerolog.Event { return LogWarn("packet") }
// PacketInfo packet log info
func PacketInfo() *zerolog.Event { return LogInfo("packet") }
// PacketDebug packet log debug
func PacketDebug() *zerolog.Event { return LogDebug("packet") }
// PacketTrace packet log trace
func PacketTrace() *zerolog.Event { return LogTrace("packet") }
// DumpPanic packet log panic
func DumpPanic() *zerolog.Event { return LogPanic("dump") }
// DumpFatal packet log fatal
func DumpFatal() *zerolog.Event { return LogFatal("dump") }
// DumpError packet log error
func DumpError() *zerolog.Event { return LogError("dump") }
// DumpWarn packet log warn
func DumpWarn() *zerolog.Event { return LogWarn("dump") }
// DumpInfo packet log info
func DumpInfo() *zerolog.Event { return LogInfo("dump") }
// DumpDebug packet log debug
func DumpDebug() *zerolog.Event { return LogDebug("dump") }
// DumpTrace packet log trace
func DumpTrace() *zerolog.Event { return LogTrace("dump") }