douqi0090 2018-11-11 15:39
浏览 142
已采纳

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

I am trying to put the same message to console and the log files at the same time with custom encoder for config. In the process I want to display the caller information but the same is not being displayed even if I have used caller key as suggested in the documentation. Below is the sample code for the same

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

Any thoughts why the caller information is not being displayed.

  • 写回答

1条回答 默认 最新

  • dongxiong2000 2018-11-11 16:12
    关注

    according to documentation https://godoc.org/go.uber.org/zap#AddCaller you can do something like this on logger creation:

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

    update answer to comment
    also you can define your implementation of caller encode:

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

    and pass it to cfgConsole and cfgFile

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题