zap icon indicating copy to clipboard operation
zap copied to clipboard

how can I make a logger with different color for each level

Open overtalk opened this issue 6 years ago • 6 comments

overtalk avatar Nov 09 '18 02:11 overtalk

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

ansel1 avatar Nov 09 '18 02:11 ansel1

I believe you are looking for https://godoc.org/go.uber.org/zap/internal/color.

NathanZook avatar Jan 31 '19 22:01 NathanZook

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?

ghost avatar May 15 '19 02:05 ghost

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")
}

phanirithvij avatar Jan 31 '21 16:01 phanirithvij

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()

sc-atompower avatar Apr 07 '23 13:04 sc-atompower