/
init.go
199 lines (166 loc) · 4.31 KB
/
init.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
package log
import (
"os"
"sync"
"github.com/lkcloud/log/options"
"github.com/lkcloud/log/zaplog"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
//默认
var l Loger = newLogger(nil)
var once sync.Once
var lock = &sync.RWMutex{}
//设置
func SetLogger(opts ...options.Option) {
once.Do(func() {
l = newLogger(nil, opts...)
})
}
func GetLogger() Loger {
return l
}
func SetLoggerWithOptions(o *options.Options, opts ...options.Option) {
once.Do(func() {
l = newLogger(o, opts...)
})
}
func Reset() {
lock.Lock()
defer lock.Unlock()
l = newLogger(nil)
}
func newLogger(o *options.Options, opts ...options.Option) *zaplog.Log {
encoderConfig := zapcore.EncoderConfig{
// Keys can be anything except the empty string.
TimeKey: "time",
LevelKey: "level",
NameKey: "flag",
CallerKey: "file",
MessageKey: "msg",
StacktraceKey: "stack",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: timeEncoder,
EncodeDuration: milliSecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if o == nil {
o = options.NewOptions()
}
for _, opt := range opts {
opt(o)
}
// when output to local path, with color is forbidden
if o.Color && (o.Filename == "") && (o.LogFormat == options.PlainFormat) {
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
var writers = []zapcore.WriteSyncer{}
osfileout := zapcore.AddSync(&lumberjack.Logger{
Filename: o.Filename,
MaxSize: o.MaxSize, // megabytes
MaxBackups: 3,
MaxAge: o.MaxAge, // days
LocalTime: true,
Compress: false,
})
if o.IsStdOut {
writers = append(writers, os.Stdout)
}
writers = append(writers, osfileout)
w := zapcore.NewMultiWriteSyncer(writers...)
atom := zap.NewAtomicLevel()
atom.SetLevel(options.ParseLevel(o.LogLevel)) //改变日志级别
var enc zapcore.Encoder
if o.LogFormat == options.PlainFormat {
enc = zapcore.NewConsoleEncoder(encoderConfig)
} else {
enc = zapcore.NewJSONEncoder(encoderConfig)
}
core := zapcore.NewCore(
//这里控制json 或者不是json 类型
enc,
w,
atom,
)
logger := zap.New(
core,
zap.AddStacktrace(options.ParseLevel(o.Stacktrace)),
zap.AddCaller(),
zap.AddCallerSkip(2))
if o.ProjectName != "" {
logger = logger.With(zap.String(options.ProjectKey, o.ProjectName))
}
loggerSugar := logger.Sugar()
return zaplog.NewLogger(loggerSugar, atom, o)
}
//快捷使用,开发使用
func NewDevelopment(projectName, filePath string) {
SetLogger(options.WithProjectName(projectName),
options.WithFilename(filePath),
options.WithLogFormat(options.JsonFormat),
options.WithIsStdOut(true))
}
//快捷使用,生产使用
func NewProduction(projectName, filePath string) {
SetLogger(options.WithProjectName(projectName),
options.WithLogFormat(options.JsonFormat),
options.WithFilename(filePath),
options.WithLogLevel(options.ErrorLevel),
options.WithIsStdOut(false))
}
//目前只有zap生效
func SetLogLevel(level options.Level) {
l.SetLogLevel(level)
}
func GetOptions() string {
return l.Options()
}
//目前只有zap生效
func Sync() {
l.Sync()
}
//key value
func Debug(msg string, keysAndValues ...interface{}) {
l.Debug(msg, keysAndValues...)
}
func Info(msg string, keysAndValues ...interface{}) {
l.Info(msg, keysAndValues...)
}
func Warn(msg string, keysAndValues ...interface{}) {
l.Warn(msg, keysAndValues...)
}
func Error(msg string, keysAndValues ...interface{}) {
l.Error(msg, keysAndValues...)
}
func Panic(msg string, keysAndValues ...interface{}) {
l.Panic(msg, keysAndValues...)
}
func Fatal(msg string, keysAndValues ...interface{}) {
l.Fatal(msg, keysAndValues...)
}
func Dump(msg string, keysAndValues ...interface{}) {
l.Dump(msg, keysAndValues...)
}
func Debugf(format string, args ...interface{}) {
l.Debugf(format, args...)
}
func Infof(format string, args ...interface{}) {
l.Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
l.Warnf(format, args...)
}
func Errorf(format string, args ...interface{}) {
l.Errorf(format, args...)
}
func Panicf(format string, args ...interface{}) {
l.Panicf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
l.Fatalf(format, args...)
}
func Dumpf(format string, args ...interface{}) {
l.Dumpf(format, args...)
}