go-rest-api icon indicating copy to clipboard operation
go-rest-api copied to clipboard

Zap logging with colors on Windows

Open kolkov opened this issue 5 years ago • 0 comments

If we need colored output on windows console we need to replace New() function:

// New creates a new logger using the default configuration.
func New() Logger {
	l, _ := zap.NewProduction()
	return NewWithZap(l)
}

with:

import (
    "github.com/mattn/go-colorable"
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    "go.uber.org/zap/zaptest/observer"
)

// New creates a new logger using the default configuration.
func New() Logger {
	c := zap.NewDevelopmentEncoderConfig()
	c.EncodeLevel = zapcore.CapitalColorLevelEncoder
	l := zap.New(zapcore.NewCore(
		zapcore.NewConsoleEncoder(c),
		zapcore.AddSync(colorable.NewColorableStdout()),
		zapcore.DebugLevel,
	))

	return NewWithZap(l)
}

kolkov avatar Mar 17 '20 07:03 kolkov