-
Notifications
You must be signed in to change notification settings - Fork 0
/
pplogger.go
179 lines (153 loc) · 4.25 KB
/
pplogger.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 pplogger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"time"
)
type Config struct {
StdoutWriter bool // 是否打印到控制台
FileWriter bool // 是否写到文件中
LogPath string // 日志文件路径
Filename string // 日志文件名称
LogLevel string // 日志输出等级
MaxSize int // 单个文件最大限制,单位 M
MaxBackups int // 最多保留备份数
MaxAge int // 最多保留天数
Compress bool // 是否压缩
}
const (
DebugLevel = "Debug"
InfoLevel = "Info"
WarnLevel = "Warn"
ErrorLevel = "Error"
DPanicLevel = "DPanic"
PanicLevel = "Panic"
FatalLevel = "Fatal"
)
func NewEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
// Keys can be anything except the empty string.
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
func getFileLogger(config Config) lumberjack.Logger {
return lumberjack.Logger{
Filename: filepath.Join(config.LogPath, config.Filename),
MaxSize: config.MaxSize,
MaxBackups: config.MaxBackups,
MaxAge: config.MaxAge,
Compress: config.Compress,
}
}
func getLogLevel(str string) zapcore.Level {
var level zapcore.Level
switch str {
case DebugLevel:
level = zap.DebugLevel
case InfoLevel:
level = zap.InfoLevel
case WarnLevel:
level = zap.WarnLevel
case ErrorLevel:
level = zap.ErrorLevel
case DPanicLevel:
level = zap.DPanicLevel
case PanicLevel:
level = zap.PanicLevel
case FatalLevel:
level = zap.FatalLevel
default:
level = zap.InfoLevel
}
return level
}
func NewPPLogger(config Config) (*zap.Logger, *zap.SugaredLogger) {
// 设置默认值
if config.MaxSize == 0 {
config.MaxSize = 500
}
if config.MaxBackups == 0 {
config.MaxBackups = 3
}
if config.MaxAge == 0 {
config.MaxAge = 30
}
logPath := config.LogPath
if logPath == "" || logPath == "./" {
logPath = "./logs"
}
if _, err := os.Stat(logPath); os.IsNotExist(err) {
absRegexp, _ := regexp.Compile(`^(/|([a-zA-Z]:\\)).*`)
if !absRegexp.MatchString(logPath) {
_, currentFilePath, _, _ := runtime.Caller(1)
workPath := filepath.Join(filepath.Dir(currentFilePath), "../")
logPath = filepath.Join(workPath, logPath)
}
}
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
log.Fatal("foundation logger: ", err)
}
config.LogPath = logPath
var multiWriters zapcore.WriteSyncer
if config.StdoutWriter && config.FileWriter {
fileLogger := getFileLogger(config)
multiWriters = zapcore.NewMultiWriteSyncer(zapcore.AddSync(&fileLogger), zapcore.AddSync(os.Stdout))
} else if config.FileWriter {
fileLogger := getFileLogger(config)
multiWriters = zapcore.NewMultiWriteSyncer(zapcore.AddSync(&fileLogger))
} else if config.StdoutWriter {
multiWriters = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout))
} else {
log.Fatal("Logfile and Stdout must have one set to true")
}
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(NewEncoderConfig()),
multiWriters,
getLogLevel(config.LogLevel),
)
opts := []zap.Option{zap.AddCaller()}
opts = append(opts, zap.AddStacktrace(zap.ErrorLevel))
opts = append(opts, zap.AddCallerSkip(0))
logger := zap.New(core, opts...)
sugar := logger.Sugar()
return logger, sugar
}
func NewPPLoggerLite(fileName string, logLevel string) (*zap.Logger, *zap.SugaredLogger) {
if fileName == "" {
fileName = "./logs/pplogger.log"
}
w := zapcore.AddSync(&lumberjack.Logger{
Filename: fileName,
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 30, // days
})
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(NewEncoderConfig()),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout),
w),
getLogLevel(logLevel),
)
logger := zap.New(core, zap.AddCaller())
sugar := logger.Sugar()
return logger, sugar
}