forked from fairyhunter13/logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
146 lines (121 loc) · 3.14 KB
/
logger.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
package logger
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
logger *Logger
sugaredLogger *zap.SugaredLogger
)
// Logger specifies the logger instance for this package.
type Logger struct {
*zap.Logger
Core zapcore.Core
Options []zap.Option
}
// Validate validate the logger parameters that is required.
func (l *Logger) Validate() (err error) {
if l.Core == nil || len(l.Options) == 0 {
err = ErrEmptyOpts
}
return
}
// Init initialize the logger local instance of this package.
func Init(opts ...Options) (err error) {
logger = new(Logger)
logger.Core = zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
os.Stdout,
zap.NewAtomicLevelAt(zapcore.DebugLevel),
)
logger.Options = []zap.Option{zap.AddCaller(), zap.AddCallerSkip(1)}
for _, opt := range opts {
opt(logger)
}
err = logger.Validate()
if err != nil {
return
}
logger.Logger = zap.New(logger.Core, logger.Options...)
return
}
func initLogger() {
if logger == nil {
Init()
}
}
// Get returns the underlying logger
func Get() *Logger {
initLogger()
return logger
}
// Check returns a CheckedEntry if logging a message at the specified level is enabled.
func Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
initLogger()
return logger.Check(lvl, msg)
}
// DPanic logs message at DPanic level.
// If logger is in the development mode, the logger will be panic.
func DPanic(msg string, fields ...zap.Field) {
initLogger()
logger.DPanic(msg, fields...)
}
// Debug logs message at Debug level.
func Debug(msg string, fields ...zap.Field) {
initLogger()
logger.Debug(msg, fields...)
}
// Error logs message at Error level.
func Error(msg string, fields ...zap.Field) {
initLogger()
logger.Error(msg, fields...)
}
// Fatal logs message at Fatal level.
func Fatal(msg string, fields ...zap.Field) {
initLogger()
logger.Fatal(msg, fields...)
}
// Info logs message at Info level.
func Info(msg string, fields ...zap.Field) {
initLogger()
logger.Info(msg, fields...)
}
// Named adds a new path segment to the logger's name.
func Named(s string) *zap.Logger {
initLogger()
return logger.Named(s)
}
// Panic logs message at Panic level.
func Panic(msg string, fields ...zap.Field) {
initLogger()
logger.Panic(msg, fields...)
}
// Sugar returns the sugared logger.
func Sugar() *zap.SugaredLogger {
initLogger()
if sugaredLogger == nil {
sugaredLogger = logger.Sugar()
}
return sugaredLogger
}
// Sync flushes the buffer/resources inside the logger.
func Sync() error {
initLogger()
return logger.Sync()
}
// Warn logs message at Warn level.
func Warn(msg string, fields ...zap.Field) {
initLogger()
logger.Warn(msg, fields...)
}
// With creates a child logger and adds structured context to it.
func With(fields ...zap.Field) *zap.Logger {
initLogger()
return logger.With(fields...)
}
// WithOptions clones the current logger and supplies it with options.
func WithOptions(options ...zap.Option) *zap.Logger {
initLogger()
return logger.WithOptions(options...)
}