NewZap returns a new log handler based on the zap library.
(encoding string)
| 25 | |
| 26 | // NewZap returns a new log handler based on the zap library. |
| 27 | func NewZap(encoding string) (Handler, error) { |
| 28 | config := zap.Config{ |
| 29 | Level: zap.NewAtomicLevelAt(zap.DebugLevel), // levels are currently still filtered by the Logger. |
| 30 | Encoding: encoding, |
| 31 | EncoderConfig: zap.NewProductionEncoderConfig(), |
| 32 | OutputPaths: []string{"stderr"}, |
| 33 | ErrorOutputPaths: []string{"stderr"}, |
| 34 | } |
| 35 | config.DisableCaller = true // omit caller from log message. |
| 36 | config.EncoderConfig.TimeKey = "" // omit timestamp from log message. |
| 37 | if encoding == "console" { |
| 38 | config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder |
| 39 | colorterm, _ := strconv.ParseBool(os.Getenv("COLORTERM")) |
| 40 | if !colorterm && os.Getenv("COLORTERM") == "truecolor" { |
| 41 | colorterm = true |
| 42 | } |
| 43 | if colorterm { |
| 44 | config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder |
| 45 | } |
| 46 | } |
| 47 | logger, err := config.Build() |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | return &zapHandler{Logger: logger}, nil |
| 52 | } |
| 53 | |
| 54 | type zapHandler struct { |
| 55 | *zap.Logger |