Uber Zap 记录器未在日志语句中打印调用者信息

Posted

技术标签:

【中文标题】Uber Zap 记录器未在日志语句中打印调用者信息【英文标题】:Uber Zap logger not printing caller information in the log statement 【发布时间】:2019-04-14 11:24:15 【问题描述】:

我正在尝试使用自定义编码器将相同的消息同时发送到控制台和日志文件以进行配置。在此过程中,我想显示调用者信息,但即使我按照文档中的建议使用了caller 键,也不会显示相同的信息。

下面是相同的示例代码

package main

import (
    "os"
    "time"

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

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 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: zapcore.FullCallerEncoder,
    
    cfgFile := zapcore.EncoderConfig
        MessageKey:   "message",
        LevelKey:     "severity",
        EncodeLevel:  CustomLevelFileEncoder,
        TimeKey:      "time",
        EncodeTime:   SyslogTimeEncoder,
        CallerKey:    "caller",
        EncodeCaller: zapcore.FullCallerEncoder,
    

    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)
    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)
    flogger.Debug("An exclusive message for file")
    //output
    //"severity":"DEBUG","time":"Nov 11, 2018  20:24:11","message":"Sample debug for log file and console"
    //"severity":"WARNING","time":"Nov 11, 2018  20:24:11","message":"An warning message example"
    //"severity":"INFO","time":"Nov 11, 2018  20:24:11","message":"An info level message"

有什么想法为什么不显示来电者信息?

【问题讨论】:

【参考方案1】:

AddCallerSkip 可以帮助你跳过调用者的调用堆栈深度。

// before
zap.New(core, zap.AddCaller())
// output: "level":"panic","time":"2020-06-09T16:08:25.677+0800","line":"log/log.go:118","msg":"before")

// add caller skip
zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
// output: "level":"panic","time":"2020-06-09T16:47:39.559+0800","line":"client/client.go:29","msg":"after"

【讨论】:

【参考方案2】:

根据文档https://godoc.org/go.uber.org/zap#AddCaller 您可以在创建记录器时执行以下操作:

wlogger := zap.New(core, zap.AddCaller())

更新评论答案 您也可以定义调用者编码的实现:

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

并将其传递给 cfgConsolecfgFile

【讨论】:

它工作,但如何将文件名而不是 go-play/log.go:75log.go:75 你可以定义你的CallerEncoder的实现func MyCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) enc.AppendString(filepath.Base(caller.FullPath()))

以上是关于Uber Zap 记录器未在日志语句中打印调用者信息的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

Zap简单使用