/
logger.go
150 lines (137 loc) · 4.19 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
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
package log
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strconv"
"strings"
"time"
)
var Logger, _ = zap.NewProduction()
// InitLogger 初始化Logger
func InitLogger() (err error) {
writeSyncer := getLogWriter()
encoder := getEncoder()
var l = new(zapcore.Level)
err = l.UnmarshalText([]byte(os.Getenv("LOG_LEVEL")))
if err != nil {
return
}
core := zapcore.NewCore(encoder, writeSyncer, l)
Logger = zap.New(core, zap.AddCaller())
return
}
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "time"
encoderConfig.LevelKey = "level"
encoderConfig.NameKey = "log"
encoderConfig.MessageKey = "msg"
encoderConfig.StacktraceKey = "stacktrace"
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.LineEnding = zapcore.DefaultLineEnding
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
encoderConfig.EncodeName = zapcore.FullNameEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}
func getLogWriter() zapcore.WriteSyncer {
maxSize, _ := strconv.Atoi(os.Getenv("LOG_MAX_SIZE"))
maxBackups, _ := strconv.Atoi(os.Getenv("LOG_MAX_BACKUPS"))
maxAge, _ := strconv.Atoi(os.Getenv("LOG_MAX_AGE"))
compress, _ := strconv.ParseBool(os.Getenv("LOG_COMPRESS"))
lumberJackLogger := &lumberjack.Logger{
Filename: os.Getenv("LOG_FILENAME"),
MaxSize: maxSize,
MaxBackups: maxBackups,
MaxAge: maxAge,
Compress: compress,
}
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(lumberJackLogger), zapcore.AddSync(os.Stdout))
}
// GinLogger 接收gin框架默认的日志
func GinLogger(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
cost := time.Since(start)
logger.Info("gin log",
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
zap.Duration("cost", cost),
)
}
}
// GinRecovery recover掉项目可能出现的panic,并使用zap记录相关日志
func GinRecovery(logger *zap.Logger, stack bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace.
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe {
logger.Error(c.Request.URL.Path,
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
// If the connection is dead, we can't write a status to it.
_ = c.Error(err.(error)) // nolint: errcheck
c.Abort()
return
}
if stack {
logger.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
zap.String("stack", string(debug.Stack())),
)
} else {
logger.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
}
_ = c.AbortWithError(http.StatusInternalServerError, err.(error))
}
}()
c.Next()
}
}
func Info(template string, arguments interface{}) {
defer Logger.Sync()
sugar := Logger.Sugar()
sugar.Infof(template, arguments)
}
func Error(template string, arguments interface{}) {
defer Logger.Sync()
sugar := Logger.Sugar()
sugar.Errorf(template, arguments)
}
func Panic(template string, arguments interface{}) {
defer Logger.Sync()
sugar := Logger.Sugar()
sugar.Panicf(template, arguments)
}