-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_writer.go
148 lines (127 loc) · 4.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package log4go
import (
"fmt"
"os"
)
type colorRecord Record
// brush is a color join function
type brush func(string) string
// newBrush return a fix color Brush
func newBrush(color string) brush {
pre := "\033["
reset := "\033[0m"
return func(text string) string {
return fmt.Sprintf("%s%s%s%s%s", pre, color, "m", text, reset)
}
}
// effect: 0~8
// 0:no, 1: Highlight (deepen) display, 2: Low light (dimmed) display,
// 4: underline, 5: blink, 7: Reverse display (replace background color and font color)
// 8: blank
// font color: 30~39
// 30: black, 31: red, 32: green, 33: yellow, 34: blue, 35: purple, 36: dark green, 37: grey
// 38: Sets the underline on the default foreground color, 39: Turn off underlining on the default foreground color
// background color: 40~49
// 40: black, 41: red, 42: green, 43: yellow, 44: blue, 45: purple, 46: dark green, 47: grey
// (background;font;effect)
var colors = []brush{
newBrush("1;31"), // Emergency red
newBrush("1;36"), // Alert dark green
newBrush("1;35"), // Critical purple
newBrush("1;31"), // Error red
newBrush("1;33"), // Warning yellow
newBrush("1;32"), // Notice green
newBrush("1;34"), // Informational blue
newBrush("2;37"), // Debug grey
}
func (r *colorRecord) ColorString() string {
inf := fmt.Sprintf("%s %s %s %s\n", r.time, LevelFlags[r.level], r.file, r.msg)
return colors[r.level](inf)
}
func (r *colorRecord) String() string {
inf := ""
switch r.level {
case EMERGENCY:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[31m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case ALERT:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[36m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case CRITICAL:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[35m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case ERROR:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[31m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case WARNING:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[33m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case NOTICE:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[32m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case INFO:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[34m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
case DEBUG:
inf = fmt.Sprintf("\033[36m%s\033[0m [\033[44m%s\033[0m] \033[47;30m%s\033[0m %s\n",
r.time, LevelFlags[r.level], r.file, r.msg)
}
return inf
}
// ConsoleWriter console writer define
type ConsoleWriter struct {
level int
color bool
fullColor bool // line all with color
}
// ConsoleWriterOptions color field options
type ConsoleWriterOptions struct {
Enable bool `json:"enable" mapstructure:"enable"`
Color bool `json:"color" mapstructure:"color"`
FullColor bool `json:"full_color" mapstructure:"full_color"`
Level string `json:"level" mapstructure:"level"`
}
// NewConsoleWriter create new console writer
func NewConsoleWriter() *ConsoleWriter {
return &ConsoleWriter{}
}
// NewConsoleWriterWithOptions create new console writer with level
func NewConsoleWriterWithOptions(options ConsoleWriterOptions) *ConsoleWriter {
defaultLevel := DEBUG
if len(options.Level) > 0 {
defaultLevel = getLevelDefault(options.Level, defaultLevel, "")
}
return &ConsoleWriter{
level: defaultLevel,
color: options.Color,
fullColor: options.FullColor,
}
}
// Write console write
func (w *ConsoleWriter) Write(r *Record) error {
if r.level > w.level {
return nil
}
if w.color {
if w.fullColor {
_, _ = fmt.Fprint(os.Stdout, ((*colorRecord)(r)).ColorString())
} else {
_, _ = fmt.Fprint(os.Stdout, ((*colorRecord)(r)).String())
}
} else {
_, _ = fmt.Fprint(os.Stdout, r.String())
}
return nil
}
// Init console init without implement
func (w *ConsoleWriter) Init() error {
return nil
}
// SetColor console output color control
func (w *ConsoleWriter) SetColor(c bool) {
w.color = c
}
// SetFullColor console output full line color control
func (w *ConsoleWriter) SetFullColor(c bool) {
w.fullColor = c
}