From a7479302fc0ce40e265fa02130d6f251e6b9de8c Mon Sep 17 00:00:00 2001 From: Aziz Rmadi <46684200+armadi1809@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:12:42 -0600 Subject: [PATCH] core: Support NO_COLOR env var to disable log coloring (#6078) --- logging.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/logging.go b/logging.go index e1ee0150..a076f062 100644 --- a/logging.go +++ b/logging.go @@ -717,7 +717,10 @@ func newDefaultProductionLogEncoder(wo WriterOpener) zapcore.Encoder { 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 coloringEnabled { + encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder + } + return zapcore.NewConsoleEncoder(encCfg) } return zapcore.NewJSONEncoder(encCfg) @@ -758,6 +761,7 @@ func Log() *zap.Logger { } var ( + coloringEnabled = os.Getenv("NO_COLOR") == "" && os.Getenv("TERM") != "xterm-mono" defaultLogger, _ = newDefaultProductionLog() defaultLoggerMu sync.RWMutex )