简体   繁体   中英

Zap logger does not print on console rather print in the log file

I have integrated Zap with my go application, we have logs getting printed in two log files and i am also using Lumberjack for log rotation. But i am trying to display the logs in console as well, but no luck for this case. Following is my code in logger.go

var (
    Logger *zap.Logger
    N2n    *zap.Logger
)

type WriteSyncer struct {
    io.Writer
}

func (ws WriteSyncer) Sync() error {
    return nil
}

func InitLogging(mode string) {
    var cfg zap.Config
    var logName = "abc.log"
    var slogName = "n2n.log"

    if mode == "production" {
        cfg = zap.NewProductionConfig()
        cfg.DisableCaller = true
    } else {
        cfg = zap.NewDevelopmentConfig()
        cfg.EncoderConfig.LevelKey = "level"
        cfg.EncoderConfig.NameKey = "name"
        cfg.EncoderConfig.MessageKey = "msg"
        cfg.EncoderConfig.CallerKey = "caller"
        cfg.EncoderConfig.StacktraceKey = "stacktrace"
    }

    cfg.Encoding = "json"
    cfg.EncoderConfig.TimeKey = "timestamp"
    cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
    cfg.OutputPaths = []string{logName}
    sw := getWriteSyncer(logName)
    swSugar := getWriteSyncer(slogName)

    l, err := cfg.Build(SetOutput(sw, cfg))
    if err != nil {
        panic(err)
    }
    defer l.Sync()

    ls, err := cfg.Build(SetOutput(swSugar, cfg))
    if err != nil {
        panic(err)
    }
    defer ls.Sync()

    Logger = l
    N2n = ls
}

// SetOutput replaces existing Core with new, that writes to passed WriteSyncer.
func SetOutput(ws zapcore.WriteSyncer, conf zap.Config) zap.Option {
    var enc zapcore.Encoder
    switch conf.Encoding {
    case "json":
        enc = zapcore.NewJSONEncoder(conf.EncoderConfig)
    case "console":
        enc = zapcore.NewConsoleEncoder(conf.EncoderConfig)
    default:
        panic("unknown encoding")
    }

    return zap.WrapCore(func(core zapcore.Core) zapcore.Core {
        return zapcore.NewCore(enc, ws, conf.Level)
    })
}

func getWriteSyncer(logName string) zapcore.WriteSyncer {
    var ioWriter = &lumberjack.Logger{
        Filename:   logName,
        MaxSize:    10, // MB
        MaxBackups: 3,  // number of backups
        MaxAge:     28, //days
        LocalTime:  true,
        Compress:   false, // disabled by default
    }
    var sw = WriteSyncer{
        ioWriter,
    }
    return sw
}

I have tried appending the output paths but it is not working.

Figured out that zapcore has NewMultiWriteSyncer which can write the logs in file and also on console using zapcore.addSync(os.stdout). For ex:

swSugar := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), 
           getWriteSyncer(logfileName))

Since this is one of the first things that appears after looking for this on Google, here's a simple example of how to make logs show on the Console and a log file, which can be any io.Writer as the one used by lumberjack:

func logInit(d bool, f *os.File) *zap.SugaredLogger {

    pe := zap.NewProductionEncoderConfig()

    fileEncoder := zapcore.NewJSONEncoder(pe)

    pe.EncodeTime = zapcore.ISO8601TimeEncoder # The encoder can be customized for each output
    consoleEncoder := zapcore.NewConsoleEncoder(pe)

    level := zap.InfoLevel
    if d {
        level = zap.DebugLevel
    }

    core := zapcore.NewTee(
        zapcore.NewCore(fileEncoder, zapcore.AddSync(f), level),
        zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level),
    )

    l := zap.New(core) # Creating the logger

    return l.Sugar()
}

I used the default ProductionEncoderConfig but it could be a custom one like the one on OP's code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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