duan01203 2018-10-10 07:06
浏览 161
已采纳

测试从自定义配置构建的Logger的zap日志记录

I have a Zap logger that is generated from a custom Config (i.e. config.Build()). I would like to test the logger by calling, for example, logger.Info() in the test method and assert the result to see if it is according to the config set. How can I achieve this?

EDIT: Adding a code example in the following

func GetLogger() *zap.Logger{
 config := &zap.Config{
  Encoding: "json",
  Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
  OutputPaths: []string{"stdout"},
  ErrorOutputPaths: []string{"stdout"},
  EncoderConfig: zapcore.EncoderConfig{
   MessageKey: "@m",
   LevelKey:    "@l",
   EncodeLevel: zapcore.CapitalLevelEncoder,
   TimeKey:    "@t",
   EncodeTime: zapcore.EpochMillisTimeEncoder,
   CallerKey:     "@c",
   EncodeCaller:  zapcore.ShortCallerEncoder,
   StacktraceKey: "@x",
  },
 }
 return config.Build()
}
  • 写回答

1条回答 默认 最新

  • donglin9717 2018-10-10 10:18
    关注

    Zap has a concept of sinks, destinations for log messages. For testing, implement a sink that simply remembers messages (for instance in a bytes.Buffer):

    package main
    
    import (
        "bytes"
        "net/url"
        "strings"
        "testing"
        "time"
    
        "go.uber.org/zap"
    )
    
    // MemorySink implements zap.Sink by writing all messages to a buffer.
    type MemorySink struct {
        *bytes.Buffer
    }
    
    // Implement Close and Sync as no-ops to satisfy the interface. The Write 
    // method is provided by the embedded buffer.
    
    func (s *MemorySink) Close() error { return nil }
    func (s *MemorySink) Sync() error  { return nil }
    
    
    func TestLogger(t *testing.T) {
        // Create a sink instance, and register it with zap for the "memory" 
        // protocol.
        sink := &MemorySink{new(bytes.Buffer)}
        zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) {
            return sink, nil
        })
    
        conf := zap.NewProductionConfig() // TODO: replace with real config
    
        // Redirect all messages to the MemorySink.    
        conf.OutputPaths = []string{"memory://"}
    
        l, err := conf.Build()
        if err != nil {
            t.Fatal(err)
        }
    
        l.Info("failed to fetch URL",
            zap.String("url", "http://example.com"),
            zap.Int("attempt", 3),
            zap.Duration("backoff", time.Second),
        )
    
        // Assert sink contents
    
        output := sink.String()
        t.Logf("output = %s", output)
    
        if !strings.Contains(output, `"url":"http://example.com"`) {
            t.Error("output missing: url=http://example.com")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析