go-rest-api
go-rest-api copied to clipboard
Zap logging with colors on Windows
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)
}