整合zap日志

To Get zap

go get go.uber.org/zap

Fast Use

全局loggger

logger, _ := zap.NewProduction()
defer logger.Sync() // 将 buffer 中的日志写到文件中
logger.Info("this is a test log")

Zap.S 和 Zap.L

zap.SugarLogger

  • zap 默认的 logger 不支持格式化输出,要打印指定值要用 zap.Stringzap.Int 等封装
  • SugaredLogger提供了debug、info、warn、error、panic、dpanic、fatal这几种方法(使用fmt.Sprint的默认格式),另外还有带f的支持format,带w的方法则支持with键值对
  • 使用zap.SugarLogger
    • sugar.Info("this will not be logged")
    • sugar.DPanic("test dpanic")
    • ……
  • SugaredLogger是在Logger的基础上,提供了额外参数的格式化方式,提供更大的便利性以格式化具体的msg,但是增加了一小部分内存

zap.Logger

  • 高性能日志输出

zap 常用配置项

type LogConfig struct {
Level string `mapstructure:"level"` // 日志级别
Filename string `mapstructure:"filename"` // 日志文件路径
MaxSize int `mapstructure:"max_size"` // 每个日志文件保存的最大尺寸 单位:M
MaxAge int `mapstructure:"max_age"`// 文件最多保存多少天
MaxBackups int `mapstructure:"max_backups"` // 日志文件最多保存多少个备份
}

配置 自定义 zap logger

package logger

import (
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var lg *zap.Logger

// Init 初始化lg
func Init(cfg *setting.LogConfig, mode string) (err error) {
writeSyncer := getLogWriter(cfg.Filename, cfg.MaxSize, cfg.MaxBackups, cfg.MaxAge)
encoder := getEncoder()
var l = new(zapcore.Level)
err = l.UnmarshalText([]byte(cfg.Level))
if err != nil {
return
}
var core zapcore.Core
if mode == "dev" {
// 进入开发模式,日志输出到终端
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core = zapcore.NewTee(
zapcore.NewCore(encoder, writeSyncer, l),
zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel),
)
} else {
core = zapcore.NewCore(encoder, writeSyncer, l)
}

lg = zap.New(core, zap.AddCaller())

zap.ReplaceGlobals(lg)
zap.L().Info("init logger success")
return
}

func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.TimeKey = "time"
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeDuration = zapcore.SecondsDurationEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}

func getLogWriter(filename string, maxSize, maxBackup, maxAge int) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: filename,
MaxSize: maxSize,
MaxBackups: maxBackup,
MaxAge: maxAge,
}
return zapcore.AddSync(lumberJackLogger)
}

// GinLogger 接收gin框架默认的日志
func GinLogger() 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)
lg.Info(path,
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(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 {
lg.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 {
lg.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
zap.String("stack", string(debug.Stack())),
)
} else {
lg.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
}
c.AbortWithStatus(http.StatusInternalServerError)
}
}()
c.Next()
}
}

Use zap logger

  • zap.L().Error("Login with valid param",zap.Error(err))
  • zap.L().Error("logic.Login failed",zap.String("username",p.Username),zap.Error(err))
    • zap.Bool(key string, val bool) Field:bool字段
    • zap.Boolp(key string, val *bool) Field:bool指针字段
    • zap.Bools(key string, val []bool) Field:bool切片字段
    • zap.Any(key string, value interface{}) Field:任意类型的字段
    • zap.Binary(key string, val []byte) Field:二进制串的字段
    • …….