-
Notifications
You must be signed in to change notification settings - Fork 135
/
logger.go
112 lines (92 loc) · 2.77 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
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
package main
import (
"os"
"path"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var logger *zap.SugaredLogger
const (
logFileMaxSize = 50 // 日志文件最大大小(MB)
logFileMaxBackups = 10 // 最多保留的备份文件数量
logFileMaxAge = 7 // 日志文件最长保留天数
logFileCompress = false // 是否压缩备份文件
)
func InitLogger() {
// 创建 logs 目录
createLogPath()
// 初始化 zap logger
initZapLogger()
}
// createLogPath 创建 logs 目录
func createLogPath() error {
if dir, err := os.Getwd(); err == nil {
logFilePath := dir + "/logs/"
if err := os.MkdirAll(logFilePath, 0777); err != nil {
panic("create log path failed: " + err.Error())
}
}
return nil
}
// getLogPath 获取 logs 目录
func getLogPath() string {
if dir, err := os.Getwd(); err == nil {
return dir + "/logs/"
}
return ""
}
// 定义 zap logger
func initZapLogger() {
encoder := getEncoder()
core := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
logger = zap.New(core).Sugar()
}
// getEncoder 获取 zap encoder
func getEncoder() zapcore.Encoder {
return zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
}
// initGinLogger 初始化 gin logger
func initGinLogger() *zap.Logger {
logPath := getLogPath()
logFileName := "access.log"
// 日志文件
logFile := path.Join(logPath, logFileName)
lumberJackLogger := &lumberjack.Logger{
Filename: logFile,
MaxSize: logFileMaxSize, // 日志文件最大大小(MB)
MaxBackups: logFileMaxBackups, // 最多保留的备份文件数量
MaxAge: logFileMaxAge, // 日志文件最长保留天数
Compress: logFileCompress, // 是否压缩备份文件
}
writeSyncer := zapcore.AddSync(lumberJackLogger)
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeCaller = nil
encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
encoder := zapcore.NewConsoleEncoder(encoderConfig)
core := zapcore.NewCore(encoder, writeSyncer, zapcore.InfoLevel)
return zap.New(core, zap.AddCaller())
}
// initServiceLogger 初始化服务日志
func initServiceLogger() gin.HandlerFunc {
_logger := initGinLogger()
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
c.Next()
_logger.Info(
"request",
zap.String("time", start.Format(time.RFC3339)),
zap.String("method", c.Request.Method),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
zap.String("path", path),
zap.Int("status", c.Writer.Status()),
zap.Duration("latency", time.Since(start)),
)
}
}