繁体   English   中英

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

[英]Uber Zap logger not printing caller information in the log statement

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

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

根据文档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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM