add StdoutColorDisabled configuration for package glog

This commit is contained in:
John Guo
2022-01-13 15:22:27 +08:00
parent 6ded700f92
commit 8731123030
2 changed files with 15 additions and 6 deletions

View File

@ -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
}

View File

@ -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).
}