zapdriver icon indicating copy to clipboard operation
zapdriver copied to clipboard

Duplicate output

Open tchap opened this issue 6 years ago • 3 comments

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 avatar Jul 11 '19 22:07 tchap

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 avatar Jul 11 '19 22:07 tchap

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 avatar Jul 11 '19 23:07 tchap

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...

tchap avatar Jul 11 '19 23:07 tchap