From 87311230307452383856447cd944c743d142cdf2 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 13 Jan 2022 15:22:27 +0800 Subject: [PATCH] add StdoutColorDisabled configuration for package glog --- os/glog/glog_logger.go | 18 +++++++++++++----- os/glog/glog_logger_config.go | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 501c0523b..213973c67 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -275,14 +275,22 @@ func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) *bytes. func (l *Logger) printToStdout(ctx context.Context, input *HandlerInput) *bytes.Buffer { if l.config.StdoutPrint { var ( + err error buffer = input.getRealBuffer(true) ) - // This will lose color in Windows os system. - // if _, err := os.Stdout.Write(input.getRealBuffer(true).Bytes()); err != nil { + if l.config.StdoutColorDisabled { + // Output to stdout without color. + if _, err = os.Stdout.Write(buffer.Bytes()); err != nil { + intlog.Error(ctx, err) + } + } else { + // This will lose color in Windows os system. + // if _, err := os.Stdout.Write(input.getRealBuffer(true).Bytes()); err != nil { - // This will print color in Windows os system. - if _, err := fmt.Fprint(color.Output, buffer.String()); err != nil { - intlog.Error(ctx, err) + // This will print color in Windows os system. + if _, err = fmt.Fprint(color.Output, buffer.String()); err != nil { + intlog.Error(ctx, err) + } } return buffer } diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index d3aa8d970..1d971a44e 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -39,9 +39,10 @@ type Config struct { RotateSize int64 `json:"rotateSize"` // Rotate the logging file if its size > 0 in bytes. RotateExpire time.Duration `json:"rotateExpire"` // Rotate the logging file if its mtime exceeds this duration. RotateBackupLimit int `json:"rotateBackupLimit"` // Max backup for rotated files, default is 0, means no backups. - RotateBackupExpire time.Duration `json:"rotateBackupExpire"` // Max expire for rotated files, which is 0 in default, means no expiration. + RotateBackupExpire time.Duration `json:"rotateBackupExpire"` // Max expires for rotated files, which is 0 in default, means no expiration. RotateBackupCompress int `json:"rotateBackupCompress"` // Compress level for rotated files using gzip algorithm. It's 0 in default, means no compression. RotateCheckInterval time.Duration `json:"rotateCheckInterval"` // Asynchronously checks the backups and expiration at intervals. It's 1 hour in default. + StdoutColorDisabled bool `json:"stdoutColorDisabled"` // Logging level prefix with color to writer or not (false in default). WriterColorEnable bool `json:"writerColorEnable"` // Logging level prefix with color to writer or not (false in default). }