diff options
author | Matthew Holt <[email protected]> | 2020-04-08 14:37:37 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2020-04-08 14:37:37 -0600 |
commit | 6e4c688ea7b9354fabad07c6bc5d7afa9c446691 (patch) | |
tree | c3ac28fd891f6ed997e065d27c04fc18f484c74c /logging.go | |
parent | 5110643201d6c82f0859f22d86be60c6493f69eb (diff) | |
download | caddy-6e4c688ea7b9354fabad07c6bc5d7afa9c446691.tar.gz caddy-6e4c688ea7b9354fabad07c6bc5d7afa9c446691.zip |
logging: Only colorize console output
Diffstat (limited to 'logging.go')
-rw-r--r-- | logging.go | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/logging.go b/logging.go index df999f2be..bd291ff60 100644 --- a/logging.go +++ b/logging.go @@ -396,17 +396,6 @@ func (cl *CustomLog) provision(ctx Context, logging *Logging) error { } } - if cl.EncoderRaw != nil { - mod, err := ctx.LoadModule(cl, "EncoderRaw") - if err != nil { - return fmt.Errorf("loading log encoder module: %v", err) - } - cl.encoder = mod.(zapcore.Encoder) - } - if cl.encoder == nil { - cl.encoder = newDefaultProductionLogEncoder() - } - if cl.WriterRaw != nil { mod, err := ctx.LoadModule(cl, "WriterRaw") if err != nil { @@ -423,6 +412,23 @@ func (cl *CustomLog) provision(ctx Context, logging *Logging) error { return fmt.Errorf("opening log writer using %#v: %v", cl.writerOpener, err) } + if cl.EncoderRaw != nil { + mod, err := ctx.LoadModule(cl, "EncoderRaw") + if err != nil { + return fmt.Errorf("loading log encoder module: %v", err) + } + cl.encoder = mod.(zapcore.Encoder) + } + if cl.encoder == nil { + // only allow colorized output if this log is going to stdout or stderr + var colorize bool + switch cl.writerOpener.(type) { + case *StdoutWriter, *StderrWriter: + colorize = true + } + cl.encoder = newDefaultProductionLogEncoder(colorize) + } + cl.buildCore() return nil @@ -650,7 +656,7 @@ func newDefaultProductionLog() (*defaultCustomLog, error) { if err != nil { return nil, err } - cl.encoder = newDefaultProductionLogEncoder() + cl.encoder = newDefaultProductionLogEncoder(true) cl.levelEnabler = zapcore.InfoLevel cl.buildCore() @@ -661,14 +667,16 @@ func newDefaultProductionLog() (*defaultCustomLog, error) { }, nil } -func newDefaultProductionLogEncoder() zapcore.Encoder { +func newDefaultProductionLogEncoder(colorize bool) zapcore.Encoder { encCfg := zap.NewProductionEncoderConfig() if terminal.IsTerminal(int(os.Stdout.Fd())) { // if interactive terminal, make output more human-readable by default encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) { encoder.AppendString(ts.UTC().Format("2006/01/02 15:04:05.000")) } - encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder + if colorize { + encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder + } return zapcore.NewConsoleEncoder(encCfg) } return zapcore.NewJSONEncoder(encCfg) |