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

fix log rotation #56

merged 3 commits into from May 3, 2019
Changes from 1 commit
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.


Just for now

@@ -131,21 +131,20 @@ func NewLogger(serviceName string, level string, format string, file string, fil
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)

logger, err := config.Build()
if w != nil {
logger, err = config.Build(SetOutput(w, config))
logger := InitLogger(w, config)

if err != nil {

return logger, config

// SetOutput returns the zap option with the new sync writer
func SetOutput(w zapcore.WriteSyncer, conf zap.Config) zap.Option {
// InitLogger constructs the logger from the options
func InitLogger(w zapcore.WriteSyncer, conf zap.Config) *zap.Logger {
This conversation was marked as resolved by 0xmchadha

This comment has been minimized.

Copy link

primalmotion May 3, 2019


does this need to be exported?

This comment has been minimized.

Copy link

0xmchadha May 3, 2019

Author Contributor

No. It doesn't need to be. Fixed it.

var enc zapcore.Encoder
// Copy paste from zap.Config.buildEncoder.
var coreFile zapcore.Core

switch conf.Encoding {
case "json":
enc = zapcore.NewJSONEncoder(conf.EncoderConfig)
@@ -154,14 +153,28 @@ func SetOutput(w zapcore.WriteSyncer, conf zap.Config) zap.Option {
panic("unknown encoding")
return zap.WrapCore(func(zapcore.Core) zapcore.Core {
return zapcore.NewCore(enc, w, conf.Level)

console := zapcore.Lock(os.Stdout)
coreConsole := zapcore.NewCore(enc, console, conf.Level)

if w != nil {
coreFile = zapcore.NewCore(enc, w, conf.Level)

core := zapcore.NewTee(

logger := zap.New(core)
return logger

// handleOutputFile handles options in log configs to redirect to file
func handleOutputFile(config *zap.Config, file string, fileOnly bool) (zapcore.WriteSyncer, error) {

var w zapcore.WriteSyncer

if file == "" {
return nil, nil
@@ -172,17 +185,20 @@ func handleOutputFile(config *zap.Config, file string, fileOnly bool) (zapcore.W

if fileOnly {
w := zapcore.AddSync(&lumberjack.Logger{
if file != "" {
w = zapcore.AddSync(&lumberjack.Logger{
Filename: file,
MaxSize: logFileSizeDefault,
MaxBackups: logFileNumBackups,
MaxAge: logFileAge,

if fileOnly {
config.OutputPaths = []string{file}
return w, nil
} else {
config.OutputPaths = append(config.OutputPaths, file)

config.OutputPaths = append(config.OutputPaths, file)
return nil, nil
return w, nil
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.