1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 23:01:28 +00:00

colorful is the switch to level label

This commit is contained in:
JessonChan 2019-03-12 12:12:59 +08:00
parent 661dcbb6ca
commit 0ba77a0d87

View File

@ -17,8 +17,10 @@ package logs
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"runtime" "strings"
"time" "time"
"github.com/shiena/ansicolor"
) )
// brush is a color join function // brush is a color join function
@ -54,9 +56,9 @@ type consoleWriter struct {
// NewConsole create ConsoleWriter returning as LoggerInterface. // NewConsole create ConsoleWriter returning as LoggerInterface.
func NewConsole() Logger { func NewConsole() Logger {
cw := &consoleWriter{ cw := &consoleWriter{
lg: newLogWriter(os.Stdout), lg: newLogWriter(ansicolor.NewAnsiColorWriter(os.Stdout)),
Level: LevelDebug, Level: LevelDebug,
Colorful: false, Colorful: true,
} }
return cw return cw
} }
@ -67,11 +69,7 @@ func (c *consoleWriter) Init(jsonConfig string) error {
if len(jsonConfig) == 0 { if len(jsonConfig) == 0 {
return nil return nil
} }
err := json.Unmarshal([]byte(jsonConfig), c) return json.Unmarshal([]byte(jsonConfig), c)
if runtime.GOOS == "windows" {
c.Colorful = false
}
return err
} }
// WriteMsg write message in console. // WriteMsg write message in console.
@ -80,7 +78,7 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
return nil return nil
} }
if c.Colorful { if c.Colorful {
msg = colors[level](msg) msg = strings.Replace(msg, levelPrefix[level], colors[level](levelPrefix[level]), 1)
} }
c.lg.println(when, msg) c.lg.println(when, msg)
return nil return nil