zap
zap copied to clipboard
how can I make a logger with different color for each level
I wrote a logging library that wraps zap. It includes a colorful, multi line, dev friendly formatter which can be used with zap. Import “github.com/gemalto/flume”, and the format will register itself as “term-color”.
I believe you are looking for https://godoc.org/go.uber.org/zap/internal/color.
It seems like this is the shortest example:
package main
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, _ := config.Build()
logger.Info("aaaaa bbbbb")
}
that disappointing as other projects are more compact:
package main
import "github.com/labstack/gommon/log"
func main() {
log.EnableColor()
log.Info("aaaaa bbbbb")
}
@pedgeio can you comment?
https://github.com/uber-go/zap/pull/307#issuecomment-504794011 also works on windows terminals
package main
import (
"github.com/mattn/go-colorable"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
aa := zap.NewDevelopmentEncoderConfig()
aa.EncodeLevel = zapcore.CapitalColorLevelEncoder
bb := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(aa),
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.DebugLevel,
))
bb.Warn("cc")
}
I believe you are looking for https://godoc.org/go.uber.org/zap/internal/color.
Any plans to have this working with zap.NewProductionConfig()
Right now its only working with zap.NewDevelopmentConfig()