douyan6742 2019-01-28 04:06
浏览 38

具有1)定制配置和2)伐木工人的zap日志记录

I'm trying to build a customized zap logger with 1) customized *zap.Config and 2) lumberjack, but can't find proper example to apply both configurations.

Since config.Build does not accept WriteSync as an input. Do you know how to achieve this?

func genBaseLoggerZap() Logger {
    ex, err := os.Executable()
    if err != nil {
        Fatalf("Failed to get os.Executable, err: %v", err)
    }
    zlManager.outputPath = path.Join(filepath.Dir(ex), zlManager.outputPath)
        // Want to add sync here..
    zapcore.AddSync(&lumberjack.Logger{
        Filename:   zlManager.outputPath + "123",
        MaxSize:    500,
        MaxBackups: 10,
        MaxAge:     28,
    })
    return genLoggerZap(BaseLogger, genDefaultConfig())
}

// genLoggerZap creates a zapLogger with given ModuleID and Config.
func genLoggerZap(mi ModuleID, cfg *zap.Config) Logger {
    logger, err := cfg.Build()
    if err != nil {
        Fatalf("Failed to generate zap logger, err: %v", err)
    }
    newLogger := &zapLogger{mi, cfg, logger.Sugar()}
    newLogger.register()
    return newLogger
}
  • 写回答

1条回答 默认 最新

  • dpkajqd31574096 2019-01-28 18:32
    关注

    You can add custom log destinations using the zap.RegisterSink function and the Config.OutputPaths field. RegisterSink maps URL schemes to Sink constructors, and OutputPaths configures log destinations (encoded as URLs).

    Conveniently, *lumberjack.Logger implements almost all of the zap.Sink interface already. Only the Sync method is missing, which can be easily added with a thin wrapper type.

    package main
    
    import (
        "net/url"
    
        "go.uber.org/zap"
        lumberjack "gopkg.in/natefinch/lumberjack.v2"
    )
    
    type lumberjackSink struct {
        *lumberjack.Logger
    }
    
    // Sync implements zap.Sink. The remaining methods are implemented 
    // by the embedded *lumberjack.Logger.
    func (lumberjackSink) Sync() error { return nil }
    
    func main() {
        zap.RegisterSink("lumberjack", func(u *url.URL) (zap.Sink, error) {
            return lumberjackSink{
                Logger: &lumberjack.Logger{
                    Filename: u.Opaque,
    
                    // Use query parameters or hardcoded values for remaining
                    // fields.
                },
            }, nil
        })
    
        config := zap.NewProductionConfig()
    
        // Add a URL with the "lumberjack" scheme.
        config.OutputPaths = append(config.OutputPaths, "lumberjack:foo.log")
    
        log, _ := config.Build()
        log.Info("test", zap.String("foo", "bar"))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line