forked from samwafgo/SamWaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zlog.go
131 lines (114 loc) · 3.98 KB
/
zlog.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
package zlog
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
)
// 简单封装一下对 zap 日志库的使用
// 使用方式:
// zlog.Debug("hello", zap.String("name", "Kevin"), zap.Any("arbitraryObj", dummyObject))
// zlog.Info("hello", zap.String("name", "Kevin"), zap.Any("arbitraryObj", dummyObject))
// zlog.Warn("hello", zap.String("name", "Kevin"), zap.Any("arbitraryObj", dummyObject))
var logger *zap.Logger
func InitZLog(releaseFlag string) {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoder := zapcore.NewJSONEncoder(encoderConfig)
//file, _ := os.OpenFile("/tmp/test.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 644)
//fileWriteSyncer = zapcore.AddSync(file)
fileWriteSyncer := getFileLogWriter()
if releaseFlag == "false" {
core := zapcore.NewTee(
// 同时向控制台和文件写日志, 生产环境记得把控制台写入去掉
zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel),
zapcore.NewCore(encoder, fileWriteSyncer, zapcore.DebugLevel),
)
logger = zap.New(core)
} else {
core := zapcore.NewTee(
zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.InfoLevel),
zapcore.NewCore(encoder, fileWriteSyncer, zapcore.InfoLevel),
zapcore.NewCore(encoder, fileWriteSyncer, zapcore.ErrorLevel),
zapcore.NewCore(encoder, fileWriteSyncer, zapcore.FatalLevel),
)
logger = zap.New(core)
}
}
func getFileLogWriter() (writeSyncer zapcore.WriteSyncer) {
exeDir := ""
// 检测环境变量是否存在
envVar := "SamWafIDE"
if value, exists := os.LookupEnv(envVar); exists {
fmt.Println("当前在IDE,环境变量" + value)
exeDir = "."
} else {
exePath, err := os.Executable()
if err != nil {
fmt.Errorf(err.Error())
exeDir = ""
} else {
exeDir = filepath.Dir(exePath)
}
}
// 使用 lumberjack 实现 log rotate
lumberJackLogger := &lumberjack.Logger{
Filename: exeDir + "/logs/log.log",
MaxSize: 100,
MaxBackups: 60,
MaxAge: 1,
Compress: false,
}
return zapcore.AddSync(lumberJackLogger)
}
func InfoCall(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
logger.Info(message, fields...)
}
func Info(message string, inter ...interface{}) {
fields := append([]zap.Field{zap.String("pid", strconv.Itoa(os.Getpid()))}, zap.Any("info", inter))
logger.Info(message, fields...)
}
func DebugCall(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
logger.Debug(message, fields...)
}
func Debug(message string, inter ...interface{}) {
fields := append([]zap.Field{zap.String("pid", strconv.Itoa(os.Getpid()))}, zap.Any("debug", inter))
logger.Debug(message, fields...)
}
func ErrorCall(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
logger.Error(message, fields...)
}
func Error(message string, inter ...interface{}) {
fields := append([]zap.Field{zap.String("pid", strconv.Itoa(os.Getpid()))}, zap.Any("err", inter))
logger.Error(message, fields...)
}
func WarnCall(message string, fields ...zap.Field) {
callerFields := getCallerInfoForLog()
fields = append(fields, callerFields...)
logger.Warn(message, fields...)
}
func Warn(message string, inter ...interface{}) {
fields := append([]zap.Field{zap.String("pid", strconv.Itoa(os.Getpid()))}, zap.Any("warn", inter))
logger.Warn(message, fields...)
}
func getCallerInfoForLog() (callerFields []zap.Field) {
pc, file, line, ok := runtime.Caller(2) // 回溯两层,拿到写日志的业务函数的信息
if !ok {
return
}
funcName := runtime.FuncForPC(pc).Name()
funcName = path.Base(funcName) //Base函数返回路径的最后一个元素,只保留函数名
callerFields = append(callerFields, zap.String("func", funcName), zap.String("file", file), zap.Int("line", line))
return
}