/
formatter.go
160 lines (123 loc) · 2.97 KB
/
formatter.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
149
150
151
152
153
154
155
156
157
158
159
160
package log
import (
"fmt"
"path"
"strings"
)
type Format interface {
Format(entry Entry) []byte
}
type FormatFull interface {
Format
// ParsingAndEscaping Default should be on
ParsingAndEscaping(disable bool)
// DisableCaller Default should be on
Caller(disable bool)
Clone() Format
}
type Formatter struct {
Module string
DisableParsingAndEscaping bool
DisableCaller bool
}
func (p *Formatter) format(entry Entry) []byte {
b := GetBuffer()
defer PutBuffer(b)
if entry.PrefixMsg != "" {
b.WriteString(entry.PrefixMsg)
b.WriteString(" ")
}
b.WriteString(fmt.Sprintf("(%d.%d) ", entry.Pid, entry.Gid))
b.WriteString(entry.Time.Format("2006-01-02 15:04:05.999Z07:00"))
color := getColorByLevel(entry.Level)
b.WriteString(color)
b.WriteString(" [")
b.WriteString(entry.Level.String())
b.WriteString("] ")
b.WriteString(colorEnd)
b.WriteString(strings.TrimSpace(entry.Message))
if !p.DisableCaller || entry.TraceId != "" {
b.WriteString(colorCyan)
b.WriteString(" [ ")
if !p.DisableCaller {
b.WriteString(path.Join(entry.CallerDir, path.Base(entry.File)))
b.WriteString(":")
b.WriteString(fmt.Sprintf("%d", entry.CallerLine))
b.WriteString(" ")
b.WriteString(entry.CallerFunc)
b.WriteString(" ")
}
if entry.TraceId != "" {
b.WriteString(entry.TraceId)
b.WriteString(" ")
}
b.WriteString("]")
b.WriteString(colorEnd)
}
if entry.SuffixMsg != "" {
b.WriteString(entry.SuffixMsg)
}
b.WriteByte('\n')
return b.Bytes()
}
func (p *Formatter) Format(entry Entry) []byte {
if p.DisableParsingAndEscaping {
return p.format(entry)
}
b := GetBuffer()
defer PutBuffer(b)
for _, msg := range strings.Split(entry.Message, "\n") {
entry.Message = msg
b.Write(p.format(entry))
}
return b.Bytes()
}
func (p *Formatter) ParsingAndEscaping(disable bool) {
p.DisableParsingAndEscaping = disable
}
func (p *Formatter) Caller(disable bool) {
p.DisableCaller = disable
}
func (p *Formatter) Clone() Format {
return &Formatter{
Module: p.Module,
DisableParsingAndEscaping: p.DisableParsingAndEscaping,
DisableCaller: p.DisableCaller,
}
}
const (
colorBlack = "\u001B[30m"
colorRed = "\u001B[31m"
colorGreen = "\u001B[32m"
colorYellow = "\u001B[33m"
colorBlue = "\u001B[34m"
colorMagenta = "\u001B[35m"
colorCyan = "\u001B[36m"
colorGray = "\u001B[37m"
colorWhite = "\u001B[38m"
colorEnd = "\u001B[0m"
)
func getColorByLevel(level Level) string {
switch level {
case DebugLevel, TraceLevel:
return colorGreen
case WarnLevel:
return colorYellow
case ErrorLevel, FatalLevel, PanicLevel:
return colorRed
default:
return colorGreen
}
}
func SplitPackageName(f string) (string, string) {
slashIndex := strings.LastIndex(f, "/")
if slashIndex > 0 {
idx := strings.Index(f[slashIndex:], ".") + slashIndex
return f[:idx], f[idx+1:]
}
slashIndex = strings.Index(f, ".")
if slashIndex > 0 {
return f[:slashIndex], f[slashIndex+1:]
}
return f, ""
}