summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-04-08 14:37:37 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-04-08 14:37:37 -0600
commit6e4c688ea7b9354fabad07c6bc5d7afa9c446691 (patch)
treec3ac28fd891f6ed997e065d27c04fc18f484c74c
parent5110643201d6c82f0859f22d86be60c6493f69eb (diff)
logging: Only colorize console output
-rw-r--r--logging.go36
1 files changed, 22 insertions, 14 deletions
diff --git a/logging.go b/logging.go
index df999f2..bd291ff 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)