1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 20:40:54 +00:00

colorful console only the terminal supports

This commit is contained in:
JessonChan 2016-02-02 16:37:09 +08:00
parent 16b01c362a
commit 5dec3d127c

View File

@ -49,7 +49,7 @@ var colors = []brush{
type consoleWriter struct { type consoleWriter struct {
lg *log.Logger lg *log.Logger
Level int `json:"level"` Level int `json:"level"`
Color bool `json:"color"` Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
} }
// NewConsole create ConsoleWriter returning as LoggerInterface. // NewConsole create ConsoleWriter returning as LoggerInterface.
@ -57,7 +57,7 @@ func NewConsole() Logger {
cw := &consoleWriter{ cw := &consoleWriter{
lg: log.New(os.Stdout, "", 0), lg: log.New(os.Stdout, "", 0),
Level: LevelDebug, Level: LevelDebug,
Color: true, Colorful: true,
} }
return cw return cw
} }
@ -68,7 +68,11 @@ func (c *consoleWriter) Init(jsonConfig string) error {
if len(jsonConfig) == 0 { if len(jsonConfig) == 0 {
return nil 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. // WriteMsg write message in console.
@ -77,12 +81,11 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
return nil return nil
} }
msg = formatLogTime(when) + msg msg = formatLogTime(when) + msg
if runtime.GOOS == "windows" || !c.Color { if c.Colorful {
c.lg.Println(msg)
return nil
}
c.lg.Println(colors[level](msg)) c.lg.Println(colors[level](msg))
} else {
c.lg.Println(msg)
}
return nil return nil
} }