美文网首页
gin & zap & logstash

gin & zap & logstash

作者: EasyNetCN | 来源:发表于2023-12-27 09:34 被阅读0次

    自定义zap logger,使用了https://github.com/elastic/ecs-logging-go-zap

    package log
    
    import (
        "os"
        "time"
    
        "github.com/natefinch/lumberjack"
        "github.com/spf13/viper"
        "go.elastic.co/ecszap"
        "go.uber.org/zap"
        "go.uber.org/zap/zapcore"
        "easymesh.net/search-service/util"
    )
    
    type Field = zap.Field
    
    var (
        Logger  *zap.Logger
        String  = zap.String
        Any     = zap.Any
        Int     = zap.Int
        Float32 = zap.Float32
    )
    
    func InitLogger(viper *viper.Viper) {
        hook := lumberjack.Logger{
            Filename:   viper.GetString("logging.file"),
            MaxSize:    10,
            MaxBackups: 30,
            MaxAge:     7,
            Compress:   true,
        }
        write := zapcore.AddSync(&hook)
    
        var level zapcore.Level
    
        switch viper.GetString("logging.level") {
        case "debug":
            level = zap.DebugLevel
        case "info":
            level = zap.InfoLevel
        case "error":
            level = zap.ErrorLevel
        case "warn":
            level = zap.WarnLevel
        default:
            level = zap.InfoLevel
        }
    
        encoderConfig := ecszap.NewDefaultEncoderConfig()
    
        var writes = []zapcore.WriteSyncer{write}
    
        if level == zap.DebugLevel {
            writes = append(writes, zapcore.AddSync(os.Stdout))
        }
    
        core := ecszap.NewCore(
            encoderConfig,
            zapcore.NewMultiWriteSyncer(writes...),
            level,
        )
    
        Logger = zap.New(
            core, zap.AddCaller(),
            zap.Fields(
                zap.String("application", viper.GetString("spring.application.name")),
                zap.String("serverIp", util.LocalIp()),
                zap.Int("port", viper.GetInt("server.port")),
                zap.String("profile", viper.GetString("spring.profiles.active")),
                zap.Any("logTime", time.Now().Format("2006-01-02 15:04:05.000")),
            ),
        )
    }
    

    设置gin使用自定义的logger

    package router
    
    import (
        "fmt"
    
        ginzap "github.com/gin-contrib/zap"
        "github.com/gin-gonic/gin"
        "go.uber.org/zap"
        "easymesh.net/search-service/log"
    )
    
    func NewRouter() *gin.Engine {
        gin.SetMode(gin.ReleaseMode)
    
        server := gin.Default()
    
        server.Use(Recovery)
        server.Use(ginzap.Ginzap(log.Logger, "2006-01-02 15:04:05.000", false))
    
        return server
    }
    
    func Recovery(c *gin.Context) {
        defer func() {
            if r := recover(); r != nil {
                log.Logger.Error("系统内部错误", zap.Error(fmt.Errorf("%v", r)))
            }
        }()
        c.Next()
    }
    

    相关文章

      网友评论

          本文标题:gin & zap & logstash

          本文链接:https://www.haomeiwen.com/subject/oasrndtx.html