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

标签 go go-zap

我正在尝试使用配置的自定义编码器将相同的消息同时发送到控制台和日志文件。在此过程中,我想显示来电者信息,但即使我按照文档中的建议使用了 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"}
}

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

最佳答案

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"}

关于go - Uber Zap 记录器不在日志语句中打印调用者信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250323/

相关文章:

macos - Sublime Text : "MarGo: Missing required environment variables: GOPATH"

image - 在 Golang 中比较 base64 图像字符串

go - 为什么在 Uber Zap 中调用 logger.With 后自定义编码会丢失?

go - 如何在不使用字段的情况下在 Uber Zap 中记录键/值对

go - 如何访问 zap Hooks 中的字段?

go - 如何将钩子(Hook)添加到 zap 记录器中?

linux - 如何减少 gccgo 编译的可执行文件所需的虚拟内存?

使用 Go 在 Google Container/Compute Engine 中登录到 Google Cloud

function - 使用方法而不是函数有什么好处吗?

go - go-logr 和 uber 的 zap 详细级别之间的对应关系是什么?