From bc724deb5e7795c9c1861c51b3ccd87ebd30d4d6 Mon Sep 17 00:00:00 2001 From: wanna Date: Tue, 13 Jul 2021 21:43:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=86=85=E5=AE=B9=E5=8C=BA?= =?UTF-8?q?=E5=88=86=E6=96=87=E4=BB=B6=E5=92=8C=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=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 {