-
Notifications
You must be signed in to change notification settings - Fork 13
/
formatter-line.go
106 lines (85 loc) · 2 KB
/
formatter-line.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
package log
import (
"bytes"
"fmt"
"runtime"
"strings"
"time"
)
// LineFormatter 格式化
type LineFormatter struct {
TimestampFormat string
CallerPrettyfier func(*runtime.Frame) (function string, file string)
}
// Format 格式化
func (f *LineFormatter) Format(entry *Entry) ([]byte, error) {
data := make(Fields)
for k, v := range entry.Data {
data[k] = v
}
b := &bytes.Buffer{}
b.Reset()
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = time.RFC3339
}
levelText := strings.ToUpper(entry.Level.String())
levelText = levelText[0:4]
b.WriteString(fmt.Sprint("[", entry.Time.Format(timestampFormat), "] "))
b.WriteString(fmt.Sprint("[", levelText, "] "))
if entry.HasCaller() {
var funcVal, fileVal string
if f.CallerPrettyfier != nil {
funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
} else {
funcVal = entry.Caller.Function
fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
}
b.WriteString(funcVal)
b.WriteString(" ")
b.WriteString(fileVal)
b.WriteString(" ")
}
b.WriteString(strings.TrimSuffix(entry.Message, "\n"))
for k, v := range data {
appendKeyValue(b, k, v)
}
b.WriteByte('\n')
return b.Bytes(), nil
}
func needsQuoting(text string) bool {
if len(text) == 0 {
return true
}
if text[0] == '"' {
return false
}
for _, ch := range text {
if !((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
return true
}
}
return false
}
func appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
if b.Len() > 0 {
b.WriteByte(' ')
}
b.WriteString(key)
b.WriteByte('=')
appendValue(b, value)
}
func appendValue(b *bytes.Buffer, value interface{}) {
stringVal, ok := value.(string)
if !ok {
stringVal = fmt.Sprint(value)
}
if !needsQuoting(stringVal) {
b.WriteString(stringVal)
} else {
b.WriteString(fmt.Sprintf("%q", stringVal))
}
}