-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
144 lines (127 loc) · 2.78 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
package verbose
import (
"bytes"
"fmt"
"strings"
"time"
)
type Formatter interface {
Format(*Entry) string
FormatByte(*Entry) []byte
SetTimeFormat(string)
}
type JSONFormatter struct {
timeFormat string
}
func NewJSONFormatter() *JSONFormatter {
return &JSONFormatter{
timeFormat: time.RFC3339,
}
}
func (j *JSONFormatter) Format(e *Entry) string {
return string(j.FormatByte(e))
}
func (j *JSONFormatter) FormatByte(e *Entry) []byte {
buf := bytes.Buffer{}
buf.WriteByte('{')
buf.WriteString(fmt.Sprintf(`"timestamp":"%s",`, e.Timestamp.Format(j.timeFormat)))
buf.WriteString(fmt.Sprintf(`"level":"%s",`, strings.ToUpper(e.Level.String())))
buf.WriteString(fmt.Sprintf(`"logger":"%s",`, e.Logger.Name()))
buf.WriteString(fmt.Sprintf(`"message":"%s",`, e.Message))
buf.WriteString(`"data":{`)
dataLen := len(e.Data)
for k, v := range e.Data {
buf.WriteString(fmt.Sprintf(`"%s":"%v"`, k, v))
if dataLen > 1 {
buf.WriteByte(',')
}
dataLen--
}
buf.WriteByte('}') // End data key
buf.WriteByte('}') // End complete object
buf.WriteByte('\n')
return buf.Bytes()
}
func (j *JSONFormatter) SetTimeFormat(f string) {
j.timeFormat = f
}
type LineFormatter struct {
timeFormat string
}
func NewLineFormatter() *LineFormatter {
return &LineFormatter{
timeFormat: time.RFC3339,
}
}
func (l *LineFormatter) Format(e *Entry) string {
return string(l.FormatByte(e))
}
func (l *LineFormatter) FormatByte(e *Entry) []byte {
buf := &bytes.Buffer{}
fmt.Fprintf(
buf,
"%s: %s: %s: %s",
e.Timestamp.Format(l.timeFormat),
strings.ToUpper(e.Level.String()),
e.Logger.Name(),
e.Message,
)
dataLen := len(e.Data)
if dataLen > 0 {
buf.WriteString(" |")
for k, v := range e.Data {
fmt.Fprintf(buf, ` "%s": "%v"`, k, v)
if dataLen > 1 {
buf.WriteByte(',')
}
dataLen--
}
}
buf.WriteByte('\n')
return buf.Bytes()
}
func (l *LineFormatter) SetTimeFormat(f string) {
l.timeFormat = f
}
type ColoredLineFormatter struct {
timeFormat string
}
func NewColoredLineFormatter() *ColoredLineFormatter {
return &ColoredLineFormatter{
timeFormat: time.RFC3339,
}
}
func (l *ColoredLineFormatter) Format(e *Entry) string {
return string(l.FormatByte(e))
}
func (l *ColoredLineFormatter) FormatByte(e *Entry) []byte {
buf := &bytes.Buffer{}
fmt.Fprintf(
buf,
"%s%s: %s%s: %s%s: %s%s",
ColorGrey,
e.Timestamp.Format(l.timeFormat),
colors[e.Level],
strings.ToUpper(e.Level.String()),
ColorGreen,
e.Logger.Name(),
ColorReset,
e.Message,
)
dataLen := len(e.Data)
if dataLen > 0 {
buf.WriteString(" |")
for k, v := range e.Data {
fmt.Fprintf(buf, ` "%s": "%v"`, k, v)
if dataLen > 1 {
buf.WriteByte(',')
}
dataLen--
}
}
buf.WriteByte('\n')
return buf.Bytes()
}
func (l *ColoredLineFormatter) SetTimeFormat(f string) {
l.timeFormat = f
}