forked from skoowoo/log4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_writer.go
59 lines (46 loc) · 1.27 KB
/
console_writer.go
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
package log4go
import (
"fmt"
"os"
)
type colorRecord Record
func (r *colorRecord) String() string {
switch r.level {
case DEBUG:
return fmt.Sprintf("\033[36m%s\033[0m [\033[34m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LEVEL_FLAGS[r.level], r.code, r.info)
case INFO:
return fmt.Sprintf("\033[36m%s\033[0m [\033[32m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LEVEL_FLAGS[r.level], r.code, r.info)
case WARNING:
return fmt.Sprintf("\033[36m%s\033[0m [\033[33m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LEVEL_FLAGS[r.level], r.code, r.info)
case ERROR:
return fmt.Sprintf("\033[36m%s\033[0m [\033[31m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LEVEL_FLAGS[r.level], r.code, r.info)
case FATAL:
return fmt.Sprintf("\033[36m%s\033[0m [\033[35m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LEVEL_FLAGS[r.level], r.code, r.info)
}
return ""
}
type ConsoleWriter struct {
color bool
}
func NewConsoleWriter() *ConsoleWriter {
return &ConsoleWriter{}
}
func (w *ConsoleWriter) Write(r *Record) error {
if w.color {
fmt.Fprint(os.Stdout, ((*colorRecord)(r)).String())
} else {
fmt.Fprint(os.Stdout, r.String())
}
return nil
}
func (w *ConsoleWriter) Init() error {
return nil
}
func (w *ConsoleWriter) SetColor(c bool) {
w.color = c
}