diff --git a/logs/console.go b/logs/console.go index d7ed8d8e..3781e6cd 100644 --- a/logs/console.go +++ b/logs/console.go @@ -47,17 +47,17 @@ var colors = []brush{ // consoleWriter implements LoggerInterface and writes messages to terminal. type consoleWriter struct { - lg *log.Logger - Level int `json:"level"` - Color bool `json:"color"` + lg *log.Logger + Level int `json:"level"` + Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color } // NewConsole create ConsoleWriter returning as LoggerInterface. func NewConsole() Logger { cw := &consoleWriter{ - lg: log.New(os.Stdout, "", 0), - Level: LevelDebug, - Color: true, + lg: log.New(os.Stdout, "", 0), + Level: LevelDebug, + Colorful: true, } return cw } @@ -68,7 +68,11 @@ func (c *consoleWriter) Init(jsonConfig string) error { if len(jsonConfig) == 0 { return nil } - return json.Unmarshal([]byte(jsonConfig), c) + err := json.Unmarshal([]byte(jsonConfig), c) + if runtime.GOOS == "windows" { + c.Colorful = false + } + return err } // WriteMsg write message in console. @@ -77,12 +81,11 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error { return nil } msg = formatLogTime(when) + msg - if runtime.GOOS == "windows" || !c.Color { + if c.Colorful { + c.lg.Println(colors[level](msg)) + } else { c.lg.Println(msg) - return nil } - c.lg.Println(colors[level](msg)) - return nil }