From 968e1db94db6d832fbdb018873abd004d926a464 Mon Sep 17 00:00:00 2001 From: wanna Date: Mon, 28 Jun 2021 00:00:44 +0800 Subject: [PATCH 1/7] add log level prefix color --- os/glog/glog_logger.go | 8 ++++---- os/glog/glog_logger_chaining.go | 11 +++++++++++ os/glog/glog_logger_config.go | 5 +++++ os/glog/glog_logger_handler.go | 19 +++++++++++++++++-- os/glog/glog_logger_level.go | 25 +++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 7f4dcbc91..7a50d6b2c 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -43,6 +43,7 @@ const ( defaultFilePerm = os.FileMode(0666) defaultFileExpire = time.Minute pathFilterKey = "/os/glog/glog" + bufferStdOut = "stdOut" ) const ( @@ -220,20 +221,19 @@ func (l *Logger) print(ctx context.Context, level int, values ...interface{}) { // printToWriter writes buffer to writer. func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) { - buffer := input.Buffer() if l.config.Writer == nil { // Output content to disk file. if l.config.Path != "" { - l.printToFile(input.Time, buffer) + l.printToFile(input.Time, input.Buffer()) } // Allow output to stdout? if l.config.StdoutPrint { - if _, err := os.Stdout.Write(buffer.Bytes()); err != nil { + if _, err := os.Stdout.Write(input.Buffer(bufferStdOut).Bytes()); err != nil { intlog.Error(err) } } } else { - if _, err := l.config.Writer.Write(buffer.Bytes()); err != nil { + if _, err := l.config.Writer.Write(input.Buffer().Bytes()); err != nil { // panic(err) intlog.Error(err) } diff --git a/os/glog/glog_logger_chaining.go b/os/glog/glog_logger_chaining.go index 3a5080b62..e96c55ec7 100644 --- a/os/glog/glog_logger_chaining.go +++ b/os/glog/glog_logger_chaining.go @@ -245,3 +245,14 @@ func (l *Logger) Async(enabled ...bool) *Logger { } return logger } + +func (l *Logger) Color(color logColor) *Logger { + logger := (*Logger)(nil) + if l.parent == nil { + logger = l.Clone() + } else { + logger = l + } + logger.SetColor(color) + return logger +} \ No newline at end of file diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index 6b00a6afd..00c974705 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -42,6 +42,7 @@ type Config struct { RotateBackupExpire time.Duration `json:"rotateBackupExpire"` // Max expire 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"` // Asynchronizely checks the backups and expiration at intervals. It's 1 hour in default. + color logColor `json:"-"` } // DefaultConfig returns the default configuration for logger. @@ -252,3 +253,7 @@ func (l *Logger) SetPrefix(prefix string) { func (l *Logger) SetHandlers(handlers ...Handler) { l.config.Handlers = append(handlers, defaultHandler) } + +func (l *Logger) SetColor(color logColor) { + l.config.color = color +} diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index 69a703992..37c318643 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -9,6 +9,7 @@ package glog import ( "bytes" "context" + "fmt" "time" ) @@ -42,11 +43,15 @@ func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, s string) { buffer.WriteString(s) } -func (i *HandlerInput) Buffer() *bytes.Buffer { +func (i *HandlerInput) Buffer(bufferType ...string) *bytes.Buffer { buffer := bytes.NewBuffer(nil) buffer.WriteString(i.TimeFormat) if i.LevelFormat != "" { - i.addStringToBuffer(buffer, i.LevelFormat) + if len(bufferType) > 0 && bufferType[0] == bufferStdOut { + i.addStringToBuffer(buffer, i.getLevelFormatWithColor()) + } else { + i.addStringToBuffer(buffer, i.LevelFormat) + } } if i.CallerFunc != "" { i.addStringToBuffer(buffer, i.CallerFunc) @@ -67,6 +72,16 @@ func (i *HandlerInput) Buffer() *bytes.Buffer { return buffer } +// getLevelFormatWithColor returns the prefix string with color. +func (i *HandlerInput) getLevelFormatWithColor() string { + s := i.LevelFormat + color := defaultLevelColor[i.Level] + if i.logger.config.color != 0 { + color = i.logger.config.color + } + return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, s) +} + func (i *HandlerInput) String() string { return i.Buffer().String() } diff --git a/os/glog/glog_logger_level.go b/os/glog/glog_logger_level.go index a756bc6f8..49a4c1f94 100644 --- a/os/glog/glog_logger_level.go +++ b/os/glog/glog_logger_level.go @@ -29,6 +29,19 @@ const ( LEVEL_FATA // 1024 ) +type logColor int + +const ( + COLOR_BLACK logColor = 30 + iota + COLOR_RED + COLOR_GREEN + COLOR_YELLOW + COLOR_BLUE + COLOR_MAGENTA + COLOR_CYAN + COLOR_WHITE +) + // defaultLevelPrefixes defines the default level and its mapping prefix string. var defaultLevelPrefixes = map[int]string{ LEVEL_DEBU: "DEBU", @@ -41,6 +54,18 @@ var defaultLevelPrefixes = map[int]string{ LEVEL_FATA: "FATA", } +// defaultLevelColor defines the default level and its mapping prefix string. +var defaultLevelColor = map[int]logColor{ + LEVEL_DEBU: COLOR_YELLOW, + LEVEL_INFO: COLOR_GREEN, + LEVEL_NOTI: COLOR_CYAN, + LEVEL_WARN: COLOR_YELLOW, + LEVEL_ERRO: COLOR_RED, + LEVEL_CRIT: COLOR_RED, + LEVEL_PANI: COLOR_RED, + LEVEL_FATA: COLOR_RED, +} + // levelStringMap defines level string name to its level mapping. var levelStringMap = map[string]int{ "ALL": LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT, From a2771c7558f4a0e5462c7d51a734382378b3332e Mon Sep 17 00:00:00 2001 From: wanna Date: Mon, 28 Jun 2021 00:23:25 +0800 Subject: [PATCH 2/7] format code --- os/glog/glog_logger.go | 2 +- os/glog/glog_logger_chaining.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 7a50d6b2c..88475c0e0 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -43,7 +43,7 @@ const ( defaultFilePerm = os.FileMode(0666) defaultFileExpire = time.Minute pathFilterKey = "/os/glog/glog" - bufferStdOut = "stdOut" + bufferStdOut = "stdOut" ) const ( diff --git a/os/glog/glog_logger_chaining.go b/os/glog/glog_logger_chaining.go index e96c55ec7..0ba7565a6 100644 --- a/os/glog/glog_logger_chaining.go +++ b/os/glog/glog_logger_chaining.go @@ -255,4 +255,4 @@ func (l *Logger) Color(color logColor) *Logger { } logger.SetColor(color) return logger -} \ No newline at end of file +} From 03928f19775b6ef8c3977de64aaa2f8288b23270 Mon Sep 17 00:00:00 2001 From: wanna Date: Mon, 28 Jun 2021 00:58:35 +0800 Subject: [PATCH 3/7] add logging level prefix with color or not config --- os/glog/glog_logger.go | 4 ++-- os/glog/glog_logger_config.go | 1 + os/glog/glog_logger_handler.go | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 88475c0e0..5fdc14fed 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -43,7 +43,7 @@ const ( defaultFilePerm = os.FileMode(0666) defaultFileExpire = time.Minute pathFilterKey = "/os/glog/glog" - bufferStdOut = "stdOut" + mustWithColor = true ) const ( @@ -228,7 +228,7 @@ func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) { } // Allow output to stdout? if l.config.StdoutPrint { - if _, err := os.Stdout.Write(input.Buffer(bufferStdOut).Bytes()); err != nil { + if _, err := os.Stdout.Write(input.Buffer(mustWithColor).Bytes()); err != nil { intlog.Error(err) } } diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index 00c974705..ec970897c 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -42,6 +42,7 @@ type Config struct { RotateBackupExpire time.Duration `json:"rotateBackupExpire"` // Max expire 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"` // Asynchronizely checks the backups and expiration at intervals. It's 1 hour in default. + FileColor bool `json:"fileColor"` // Logging level prefix with color or not (false in default). color logColor `json:"-"` } diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index 37c318643..bfb15d94b 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -43,11 +43,11 @@ func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, s string) { buffer.WriteString(s) } -func (i *HandlerInput) Buffer(bufferType ...string) *bytes.Buffer { +func (i *HandlerInput) Buffer(withColor ...bool) *bytes.Buffer { buffer := bytes.NewBuffer(nil) buffer.WriteString(i.TimeFormat) if i.LevelFormat != "" { - if len(bufferType) > 0 && bufferType[0] == bufferStdOut { + if i.logger.config.FileColor || (len(withColor) > 0 && withColor[0] == mustWithColor) { i.addStringToBuffer(buffer, i.getLevelFormatWithColor()) } else { i.addStringToBuffer(buffer, i.LevelFormat) From bfdeb6c4f511455b66d957c3882e29fa76dd3454 Mon Sep 17 00:00:00 2001 From: wanna Date: Tue, 29 Jun 2021 21:05:46 +0800 Subject: [PATCH 4/7] =?UTF-8?q?log=20color=E5=85=BC=E5=AE=B9win=E7=8E=AF?= =?UTF-8?q?=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + os/glog/glog_logger_chaining.go | 7 +++-- os/glog/glog_logger_config.go | 49 ++++++++++++++++----------------- os/glog/glog_logger_handler.go | 12 ++++---- os/glog/glog_logger_level.go | 7 ++--- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 5022b6160..ba8172629 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( github.com/BurntSushi/toml v0.3.1 github.com/clbanning/mxj v1.8.5-0.20200714211355-ff02cfb8ea28 + github.com/fatih/color v1.12.0 // indirect github.com/fsnotify/fsnotify v1.4.9 github.com/gogf/mysql v1.6.1-0.20210603073548-16164ae25579 github.com/gomodule/redigo v2.0.0+incompatible diff --git a/os/glog/glog_logger_chaining.go b/os/glog/glog_logger_chaining.go index 0ba7565a6..f4ed24201 100644 --- a/os/glog/glog_logger_chaining.go +++ b/os/glog/glog_logger_chaining.go @@ -8,6 +8,7 @@ package glog import ( "context" + "github.com/fatih/color" "github.com/gogf/gf/internal/intlog" "io" @@ -246,13 +247,15 @@ func (l *Logger) Async(enabled ...bool) *Logger { return logger } -func (l *Logger) Color(color logColor) *Logger { +// Color is a chaining function, +// which set level prefix color logging output feature. +func (l *Logger) Color(color color.Attribute) *Logger { logger := (*Logger)(nil) if l.parent == nil { logger = l.Clone() } else { logger = l } - logger.SetColor(color) + logger.config.currentColor = color return logger } diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index ec970897c..620351fd5 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -9,6 +9,7 @@ package glog import ( "errors" "fmt" + "github.com/fatih/color" "io" "strings" "time" @@ -22,28 +23,28 @@ import ( // Config is the configuration object for logger. type Config struct { - Handlers []Handler `json:"-"` // Logger handlers which implement feature similar as middleware. - Writer io.Writer `json:"-"` // Customized io.Writer. - Flags int `json:"flags"` // Extra flags for logging output features. - Path string `json:"path"` // Logging directory path. - File string `json:"file"` // Format for logging file. - Level int `json:"level"` // Output level. - Prefix string `json:"prefix"` // Prefix string for every logging content. - StSkip int `json:"stSkip"` // Skip count for stack. - StStatus int `json:"stStatus"` // Stack status(1: enabled - default; 0: disabled) - StFilter string `json:"stFilter"` // Stack string filter. - CtxKeys []interface{} `json:"ctxKeys"` // Context keys for logging, which is used for value retrieving from context. - HeaderPrint bool `json:"header"` // Print header or not(true in default). - StdoutPrint bool `json:"stdout"` // Output to stdout or not(true in default). - LevelPrefixes map[int]string `json:"levelPrefixes"` // Logging level to its prefix string mapping. - 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. - 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"` // Asynchronizely checks the backups and expiration at intervals. It's 1 hour in default. - FileColor bool `json:"fileColor"` // Logging level prefix with color or not (false in default). - color logColor `json:"-"` + Handlers []Handler `json:"-"` // Logger handlers which implement feature similar as middleware. + Writer io.Writer `json:"-"` // Customized io.Writer. + Flags int `json:"flags"` // Extra flags for logging output features. + Path string `json:"path"` // Logging directory path. + File string `json:"file"` // Format for logging file. + Level int `json:"level"` // Output level. + Prefix string `json:"prefix"` // Prefix string for every logging content. + StSkip int `json:"stSkip"` // Skip count for stack. + StStatus int `json:"stStatus"` // Stack status(1: enabled - default; 0: disabled) + StFilter string `json:"stFilter"` // Stack string filter. + CtxKeys []interface{} `json:"ctxKeys"` // Context keys for logging, which is used for value retrieving from context. + HeaderPrint bool `json:"header"` // Print header or not(true in default). + StdoutPrint bool `json:"stdout"` // Output to stdout or not(true in default). + LevelPrefixes map[int]string `json:"levelPrefixes"` // Logging level to its prefix string mapping. + 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. + 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"` // Asynchronizely checks the backups and expiration at intervals. It's 1 hour in default. + FileColorEnable bool `json:"fileColorEnable"` // Logging level prefix with color or not (false in default). + currentColor color.Attribute `json:"-"` } // DefaultConfig returns the default configuration for logger. @@ -254,7 +255,3 @@ func (l *Logger) SetPrefix(prefix string) { func (l *Logger) SetHandlers(handlers ...Handler) { l.config.Handlers = append(handlers, defaultHandler) } - -func (l *Logger) SetColor(color logColor) { - l.config.color = color -} diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index bfb15d94b..6e1b501ca 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -9,7 +9,7 @@ package glog import ( "bytes" "context" - "fmt" + "github.com/fatih/color" "time" ) @@ -47,7 +47,7 @@ func (i *HandlerInput) Buffer(withColor ...bool) *bytes.Buffer { buffer := bytes.NewBuffer(nil) buffer.WriteString(i.TimeFormat) if i.LevelFormat != "" { - if i.logger.config.FileColor || (len(withColor) > 0 && withColor[0] == mustWithColor) { + if i.logger.config.FileColorEnable || (len(withColor) > 0 && withColor[0] == mustWithColor) { i.addStringToBuffer(buffer, i.getLevelFormatWithColor()) } else { i.addStringToBuffer(buffer, i.LevelFormat) @@ -75,11 +75,11 @@ func (i *HandlerInput) Buffer(withColor ...bool) *bytes.Buffer { // getLevelFormatWithColor returns the prefix string with color. func (i *HandlerInput) getLevelFormatWithColor() string { s := i.LevelFormat - color := defaultLevelColor[i.Level] - if i.logger.config.color != 0 { - color = i.logger.config.color + fg := defaultLevelColor[i.Level] + if i.logger.config.currentColor != 0 { + fg = i.logger.config.currentColor } - return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, s) + return color.New(fg).Sprint(s) } func (i *HandlerInput) String() string { diff --git a/os/glog/glog_logger_level.go b/os/glog/glog_logger_level.go index 49a4c1f94..4cdad5624 100644 --- a/os/glog/glog_logger_level.go +++ b/os/glog/glog_logger_level.go @@ -9,6 +9,7 @@ package glog import ( "errors" "fmt" + "github.com/fatih/color" "strings" ) @@ -29,10 +30,8 @@ const ( LEVEL_FATA // 1024 ) -type logColor int - const ( - COLOR_BLACK logColor = 30 + iota + COLOR_BLACK = 30 + iota COLOR_RED COLOR_GREEN COLOR_YELLOW @@ -55,7 +54,7 @@ var defaultLevelPrefixes = map[int]string{ } // defaultLevelColor defines the default level and its mapping prefix string. -var defaultLevelColor = map[int]logColor{ +var defaultLevelColor = map[int]color.Attribute{ LEVEL_DEBU: COLOR_YELLOW, LEVEL_INFO: COLOR_GREEN, LEVEL_NOTI: COLOR_CYAN, From bc724deb5e7795c9c1861c51b3ccd87ebd30d4d6 Mon Sep 17 00:00:00 2001 From: wanna Date: Tue, 13 Jul 2021 21:43:07 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E5=8C=BA=E5=88=86=E6=96=87=E4=BB=B6=E5=92=8C=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- os/glog/glog_logger.go | 4 +--- os/glog/glog_logger_handler.go | 35 ++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 5fdc14fed..9bb943fc9 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -228,9 +228,7 @@ func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) { } // Allow output to stdout? if l.config.StdoutPrint { - if _, err := os.Stdout.Write(input.Buffer(mustWithColor).Bytes()); err != nil { - intlog.Error(err) - } + input.Stdout() } } else { if _, err := l.config.Writer.Write(input.Buffer().Bytes()); err != nil { diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index 6e1b501ca..bb603ef8e 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -10,6 +10,7 @@ import ( "bytes" "context" "github.com/fatih/color" + "os" "time" ) @@ -43,16 +44,27 @@ func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, s string) { buffer.WriteString(s) } -func (i *HandlerInput) Buffer(withColor ...bool) *bytes.Buffer { +func (i *HandlerInput) Buffer() *bytes.Buffer { buffer := bytes.NewBuffer(nil) buffer.WriteString(i.TimeFormat) - if i.LevelFormat != "" { - if i.logger.config.FileColorEnable || (len(withColor) > 0 && withColor[0] == mustWithColor) { - i.addStringToBuffer(buffer, i.getLevelFormatWithColor()) - } else { - i.addStringToBuffer(buffer, i.LevelFormat) - } - } + i.addStringToBuffer(buffer, i.LevelFormat) + msg := i.GetContent() + i.addStringToBuffer(buffer, msg.String()) + return buffer +} + +// Stdout print log to console +func (i *HandlerInput) Stdout() { + _, _ = os.Stdout.Write([]byte(i.TimeFormat)) + fg := i.getLevelFormatColor() + _, _ = color.New(fg).Print(" " + i.LevelFormat + " ") + msg := i.GetContent() + _, _ = os.Stdout.Write(msg.Bytes()) +} + +// GetContent returns the primary content. +func (i *HandlerInput) GetContent() *bytes.Buffer { + buffer := bytes.NewBuffer(nil) if i.CallerFunc != "" { i.addStringToBuffer(buffer, i.CallerFunc) } @@ -72,14 +84,13 @@ func (i *HandlerInput) Buffer(withColor ...bool) *bytes.Buffer { return buffer } -// getLevelFormatWithColor returns the prefix string with color. -func (i *HandlerInput) getLevelFormatWithColor() string { - s := i.LevelFormat +// getLevelFormatColor returns the prefix string color. +func (i *HandlerInput) getLevelFormatColor() color.Attribute { fg := defaultLevelColor[i.Level] if i.logger.config.currentColor != 0 { fg = i.logger.config.currentColor } - return color.New(fg).Sprint(s) + return fg } func (i *HandlerInput) String() string { From 84aa30d9c29540c5d6be7c4e0e23b8e09d73af1f Mon Sep 17 00:00:00 2001 From: wanna Date: Tue, 13 Jul 2021 22:45:35 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=AD=E6=B7=BB=E5=8A=A0=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- os/glog/glog_logger_handler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index bb603ef8e..b632e46bb 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -47,7 +47,12 @@ func (i *HandlerInput) addStringToBuffer(buffer *bytes.Buffer, s string) { func (i *HandlerInput) Buffer() *bytes.Buffer { buffer := bytes.NewBuffer(nil) buffer.WriteString(i.TimeFormat) - i.addStringToBuffer(buffer, i.LevelFormat) + levelString := i.LevelFormat + if i.logger.config.FileColorEnable { + fg := i.getLevelFormatColor() + levelString = color.New(fg).Sprintf(i.LevelFormat) + } + i.addStringToBuffer(buffer, levelString) msg := i.GetContent() i.addStringToBuffer(buffer, msg.String()) return buffer From 30dbccf99ea2969685b00da820249e7ab115d56b Mon Sep 17 00:00:00 2001 From: wanna Date: Wed, 14 Jul 2021 21:28:23 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=AF=B9=E5=BA=94err?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- os/glog/glog_logger.go | 4 +++- os/glog/glog_logger_handler.go | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 8e5f11ed3..891bd2ab3 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -228,7 +228,9 @@ func (l *Logger) printToWriter(ctx context.Context, input *HandlerInput) { } // Allow output to stdout? if l.config.StdoutPrint { - input.Stdout() + if err := input.Stdout(); err != nil { + intlog.Error(ctx, err) + } } } else { if _, err := l.config.Writer.Write(input.Buffer().Bytes()); err != nil { diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index 53e090ca8..152629e90 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -10,6 +10,7 @@ import ( "bytes" "context" "github.com/fatih/color" + "github.com/gogf/gf/internal/intlog" "os" "time" ) @@ -59,12 +60,22 @@ func (i *HandlerInput) Buffer() *bytes.Buffer { } // Stdout print log to console -func (i *HandlerInput) Stdout() { - _, _ = os.Stdout.Write([]byte(i.TimeFormat)) +func (i *HandlerInput) Stdout() error { + if _, err := os.Stdout.Write([]byte(i.TimeFormat)); err != nil { + intlog.Error(i.Ctx, err) + return err + } fg := i.getLevelFormatColor() - _, _ = color.New(fg).Print(" " + i.LevelFormat + " ") + if _, err := color.New(fg).Print(" " + i.LevelFormat + " "); err != nil { + intlog.Error(i.Ctx, err) + return err + } msg := i.GetContent() - _, _ = os.Stdout.Write(msg.Bytes()) + if _, err := os.Stdout.Write(msg.Bytes()); err != nil { + intlog.Error(i.Ctx, err) + return err + } + return nil } // GetContent returns the primary content.