-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
58 lines (49 loc) · 1.61 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
package lib
import (
"fmt"
"os"
"path"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func NewRotateLog(name string, option ...rotatelogs.Option) *rotatelogs.RotateLogs {
wd, _ := os.Getwd()
filename := fmt.Sprintf("%s/log/%s.log", wd, name)
base := path.Dir(filename) // 支持多级目录,建议将调试日志迁入子文件夹
if !FileExist(base) {
_ = os.MkdirAll(base, 0755)
}
option = append(option, rotatelogs.WithLinkName(filename))
rotator, err := rotatelogs.New(filename+"-%Y-%m-%d", option...)
if err != nil {
panic(err)
}
return rotator
}
func GetLogger(name string) *zap.SugaredLogger {
w := zapcore.AddSync(NewRotateLog(name, rotatelogs.WithRotationCount(30)))
var enc = zap.NewProductionEncoderConfig()
var level = zap.DebugLevel
core := zapcore.NewCore(zapcore.NewJSONEncoder(enc), w, level)
// 创建带有动态时间字段的 logger
logger := zap.New(core, zap.AddCaller()).With(
zap.String("formatted_time", time.Now().Format("2006-01-02 15:04:05")),
)
// 添加动态时间字段
logger = logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
logger.Core().With([]zapcore.Field{
zap.String("formatted_time", entry.Time.Format("2006-01-02 15:04:05")),
})
return nil
}))
return logger.Sugar()
}
// 适用于可读日志
func GetConsoleLogger(name string) *zap.SugaredLogger {
w := zapcore.AddSync(NewRotateLog(name, rotatelogs.WithRotationCount(90)))
var enc = zap.NewDevelopmentEncoderConfig()
core := zapcore.NewCore(zapcore.NewConsoleEncoder(enc), w, zap.InfoLevel)
return zap.New(core).Sugar()
}