blendle/zapdriver

Duplicate output

Opened this issue · 3 comments

tchap commented

The following demo program prints two matching log entries:

package main

import (
	"os"

	"github.com/blendle/zapdriver"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())

	core := zapcore.NewTee(
		zapcore.NewCore(consoleEncoder, os.Stderr, zapcore.ErrorLevel),
		zapcore.NewCore(consoleEncoder, os.Stdout, zapcore.InfoLevel),
	)

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

	logger = logger.WithOptions(zapdriver.WrapCore())
	defer logger.Sync()

	logger.Info("Starting...")
}

I am actually not sure this can be fixed or simply this is a limitation of Zap, but it seems to me that Check simply cannot work properly in this case without calling Check on the underlying core. Will investigate the issue further...

tchap commented

In other words, using return c.Core.Check(ent, ce) in Check fixes the issue, but that is not what you want, you want to register the wrapping core, right?

tchap commented

What obviously works is to add

func Wrap(c zapcore.Core) zapcore.Core {
	return &core{c, newLabels(), newLabels()}
}

and do

core := zapcore.NewTee(
	zapdriver.Wrap(zapcore.NewCore(consoleEncoder, os.Stderr, zapcore.ErrorLevel)),
	zapdriver.Wrap(zapcore.NewCore(consoleEncoder, os.Stdout, zapcore.InfoLevel)),
)
tchap commented

Ok, can be done already

core := zapcore.NewTee(
	zap.New(zapcore.NewCore(consoleEncoder, os.Stderr, zapcore.ErrorLevel)).
                WithOptions(zapdriver.WrapCore()).Core(),
	zap.New(zapcore.NewCore(consoleEncoder, os.Stdout, zapcore.InfoLevel)).
                WithOptions(zapdriver.WrapCore()).Core(),
)

Still wondering whether the library can be fixed so that the original code works. Not sure...