-
Notifications
You must be signed in to change notification settings - Fork 0
/
lumberjack.go
32 lines (30 loc) · 949 Bytes
/
lumberjack.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
package zip_rotate
import (
"github.com/spf13/viper"
"gopkg.in/natefinch/lumberjack.v2"
)
func NewLoggerByViper() *lumberjack.Logger {
fileName := viper.GetString("zap.rotate.Filename")
if fileName == "" {
fileName = "logs/app.log"
}
maxSize := viper.GetInt("zap.rotate.MaxSize")
if maxSize < 0 {
maxSize = 0
}
maxBackUps := viper.GetInt("zap.rotate.MaxBackups")
if maxBackUps < 0 {
maxBackUps = 0
}
maxAge := viper.GetInt("zap.rotate.MaxAge")
if maxAge < 0 {
maxAge = 0
}
return &lumberjack.Logger{
Filename: fileName, // Log file path
MaxSize: maxSize, // Maximum size of each log file Unit: M
MaxBackups: maxBackUps, // How many backups are saved in the log file
MaxAge: maxAge, // How many days can the file be keep
Compress: viper.GetBool("zap.rotate.Compress"), // need compress
}
}