具有自定义消息编码器的 Uber zap 日志记录

Posted

技术标签:

【中文标题】具有自定义消息编码器的 Uber zap 日志记录【英文标题】:Uber zap logging having custom message encoder 【发布时间】:2019-05-04 20:56:10 【问题描述】:

我正在使用以下代码在控制台上转储日志,并使用 Uber zap logger 记录文件。如何拥有自定义消息编码器,以便消息的输出格式如下?

"severity":"DEBUG","message":"Dec 12, 2018 19:52:39 [log.go:77] Sample debug for log file and console"

下面是我用来在控​​制台上转储日志的代码。

package main

import (
    "os"
    "time"

    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    "gopkg.in/natefinch/lumberjack.v2"
    "path/filepath"
)

var logLevelSeverity = map[zapcore.Level]string
    zapcore.DebugLevel:  "DEBUG",
    zapcore.InfoLevel:   "INFO",
    zapcore.WarnLevel:   "WARNING",
    zapcore.ErrorLevel:  "ERROR",
    zapcore.DPanicLevel: "CRITICAL",
    zapcore.PanicLevel:  "ALERT",
    zapcore.FatalLevel:  "EMERGENCY",


func SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) 
    enc.AppendString(t.Format("Jan 01, 2006  15:04:05"))


func CustomEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) 
    enc.AppendString(logLevelSeverity[level])


func CustomLevelFileEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) 
    enc.AppendString("[" + logLevelSeverity[level] + "]")

func funcCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) 
    enc.AppendString(filepath.Base(caller.FullPath()))


func main() 

    w := zapcore.AddSync(&lumberjack.Logger
        Filename:   "temp1.log",
        MaxSize:    1024,
        MaxBackups: 20,
        MaxAge:     28,
        Compress:   true,
    )

    //Define config for the console output
    cfgConsole := zapcore.EncoderConfig
        MessageKey:   "message",
        LevelKey:     "severity",
        EncodeLevel:  CustomEncodeLevel,
        TimeKey:      "time",
        EncodeTime:   SyslogTimeEncoder,
        CallerKey:    "caller",
        EncodeCaller: funcCaller,
    
    cfgFile := zapcore.EncoderConfig
        MessageKey:   "message",
        LevelKey:     "severity",
        EncodeLevel:  CustomLevelFileEncoder,
        TimeKey:      "time",
        EncodeTime:   SyslogTimeEncoder,
        CallerKey:    "caller",
        EncodeCaller: funcCaller,
    

    consoleDebugging := zapcore.Lock(os.Stdout)
    //consoleError := zapcore.Lock(os.Stderr)
    core := zapcore.NewTee(
        zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel),
        zapcore.NewCore(zapcore.NewJSONEncoder(cfgConsole), consoleDebugging, zap.DebugLevel),
        //zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), consoleError, zap.ErrorLevel),
    )
    //core := zapcore.NewCore(zapcore.NewConsoleEncoder(encConsole), w, zap.DebugLevel)
    wlogger := zap.New(core, zap.AddCaller())
    wlogger.Debug("Sample debug for log file and console")
    wlogger.Warn("An warning message example")
    wlogger.Info("An info level message")
    coreFile := zapcore.NewCore(zapcore.NewConsoleEncoder(cfgFile), w, zap.DebugLevel)
    flogger := zap.New(coreFile, zap.AddCaller())
    flogger.Debug("An exclusive message for file")
    //output
    //"severity":"DEBUG","time":"Dec 12, 2018  19:52:39","caller":"log.go:77","message":"Sample debug for log file and console"

任何想法如何实现预期的输出?我们需要将消息以上述格式放在控制台上。

【问题讨论】:

【参考方案1】:

根据您的代码,以下配置对我有用:

func logInit(d bool, f *os.File) *zap.SugaredLogger 

    pe := zap.NewProductionEncoderConfig()

    fileEncoder := zapcore.NewJSONEncoder(pe)

    pe.EncodeTime = zapcore.ISO8601TimeEncoder
    consoleEncoder := zapcore.NewConsoleEncoder(pe)

    level := zap.InfoLevel
    if d 
        level = zap.DebugLevel
    

    core := zapcore.NewTee(
        zapcore.NewCore(fileEncoder, zapcore.AddSync(f), level),
        zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level),
    )

    l := zap.New(core)

    return l.Sugar()

【讨论】:

以上是关于具有自定义消息编码器的 Uber zap 日志记录的主要内容,如果未能解决你的问题,请参考以下文章

Zap简单使用

如何使用 uber-go/zap 根据日志级别记录到标准输出或标准错误?

如何为滚动文件系统日志配置 uber-go/zap 记录器?

是否可以在运行时更新 zap 记录器的日志级别?

如何配置仅在错误级别写入输出的自定义 zap 记录器?

Go语言项目中使用zap日志库(翻译)