console.go
1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package logger
import (
"encoding/json"
//"fmt"
"os"
"runtime"
"sync"
"time"
)
type brush func(string) string
func newBrush(color string) brush {
pre := "\033["
reset := "\033[0m"
return func(text string) string {
return pre + color + "m" + text + reset
}
}
//鉴于终端的通常使用习惯,一般白色和黑色字体是不可行的,所以30,37不可用,
var colors = []brush{
newBrush("1;41"), // Emergency 红色底
newBrush("1;35"), // Alert 紫色
newBrush("1;34"), // Critical 蓝色
newBrush("1;31"), // Error 红色
newBrush("1;33"), // Warn 黄色
newBrush("1;36"), // Informational 天蓝色
newBrush("1;32"), // Debug 绿色
newBrush("1;32"), // Trace 绿色
}
type consoleLogger struct {
sync.Mutex
Level string `json:"level"`
Colorful bool `json:"color"`
LogLevel int
}
func (c *consoleLogger) Init(jsonConfig string, appName string) error {
if len(jsonConfig) == 0 {
return nil
}
if jsonConfig != "{}" {
//fmt.Fprintf(os.Stdout, "consoleLogger Init:%s\n", jsonConfig)
}
err := json.Unmarshal([]byte(jsonConfig), c)
if runtime.GOOS == "windows" {
c.Colorful = false
}
if l, ok := LevelMap[c.Level]; ok {
c.LogLevel = l
return nil
}
return err
}
func (c *consoleLogger) LogWrite(when time.Time, msgText interface{}, level int) error {
if level > c.LogLevel {
return nil
}
msg, ok := msgText.(string)
if !ok {
return nil
}
if c.Colorful {
msg = colors[level](msg)
}
c.printlnConsole(when, msg)
return nil
}
func (c *consoleLogger) Destroy() {
}
func (c *consoleLogger) printlnConsole(when time.Time, msg string) {
c.Lock()
defer c.Unlock()
os.Stdout.Write(append([]byte(msg), '\n'))
}
func init() {
Register(AdapterConsole, &consoleLogger{
LogLevel: LevelDebug,
Colorful: runtime.GOOS != "windows",
})
}