Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate output #23

Open
tchap opened this issue Jul 11, 2019 · 3 comments
Open

Duplicate output #23

tchap opened this issue Jul 11, 2019 · 3 comments

Comments

@tchap
Copy link

tchap commented Jul 11, 2019

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
Copy link
Author

tchap commented Jul 11, 2019

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
Copy link
Author

tchap commented Jul 11, 2019

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
Copy link
Author

tchap commented Jul 11, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant