-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
52 lines (45 loc) · 933 Bytes
/
color.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
package logx
import (
"sync"
"time"
)
const (
endColor = "\033[0m"
)
var (
red = "\033[1;31m" //红色
green = "\033[1;32m" //绿色
yellow = "\033[1;33m" //黄色
purple = "\033[1;34m" //紫色
magenta = "\033[1;35m" //品红
teal = "\033[0;97m" //青色
white = "\033[1;37m" //白色
)
var (
logColor = []string{
info: white,
debug: purple,
notice: green,
warn: yellow,
logError: red,
logPanic: magenta,
fatal: magenta,
}
)
type colorWriter struct {
mu *sync.Mutex
b []byte
writer Writer
}
// WriteLog colorWrite WriteLog
func (w *colorWriter) WriteLog(now time.Time, level int, b []byte) (int, error) {
w.b = w.b[:0]
w.b = append(w.b, logColor[level]...)
w.b = append(w.b, b...)
w.b = append(w.b, endColor...)
return w.writer.WriteLog(now, level, w.b)
}
// WithColor use colorWriter
func WithColor(w Writer) Writer {
return &colorWriter{writer: w}
}