-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
73 lines (63 loc) · 1.61 KB
/
logger.go
File metadata and controls
73 lines (63 loc) · 1.61 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
package log
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var log *logger
type logger struct {
*zap.SugaredLogger
handlers map[string]*Handler
options []zap.Option
}
func (log *logger) build() {
var cores []zapcore.Core
for _, v := range log.handlers {
cores = append(cores, v.core)
}
log.SugaredLogger = zap.New(zapcore.NewTee(cores...), log.options...).Sugar()
}
// RegisterHandler allows to register new logging handler.
func RegisterHandler(name string, h *Handler) {
if h.core == nil {
return
}
if log == nil {
log = &logger{}
}
if log.handlers == nil {
log.handlers = map[string]*Handler{}
}
log.handlers[name] = h
log.build()
}
// UnregisterHandler allows to unregister existing logging handler.
func UnregisterHandler(name string) {
if log == nil {
return
}
delete(log.handlers, name)
log.build()
}
func UseOptions(opts ...zap.Option) {
if log == nil {
log = &logger{}
log.build()
}
log.options = append(log.options, opts...)
log.SugaredLogger = log.WithOptions(opts...)
}
// WithCaller configures the logger to annotate each message with the filename,
// line number, and function name (it needs to be enabled by setting function key).
func WithCaller() {
UseOptions(zap.AddCaller(), zap.AddCallerSkip(2))
}
// WithStacktrace configures the logger to record a stack trace for all messages at
// or above Error level.
func WithStacktrace() {
UseOptions(zap.AddStacktrace(zap.ErrorLevel))
}
// DevelopmentMode puts the logger in development mode, which makes DPanic-level
// logs panic instead of simply logging an error.
func DevelopmentMode() {
UseOptions(zap.Development())
}