-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
216 lines (201 loc) · 5.22 KB
/
console.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package logs
import (
"fmt"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/valyala/bytebufferpool"
"io"
"os"
"time"
)
const (
TextFormatter ConsoleWriterFormatter = iota
ColorTextFormatter
JsonFormatter
)
type ConsoleWriterFormatter int
const (
StdOut ConsoleWriterOutType = iota
StdErr
StdMix
)
type ConsoleWriterOutType int
func NewConsoleWriter(formatter ConsoleWriterFormatter, out ConsoleWriterOutType) (w Writer) {
var outWriter io.Writer
var errWriter io.Writer
switch out {
case StdErr:
outWriter = os.Stderr
errWriter = os.Stderr
break
case StdMix:
outWriter = os.Stdout
errWriter = os.Stderr
break
default:
outWriter = os.Stdout
errWriter = os.Stdout
break
}
var encoder EntryEncoder
switch formatter {
case ColorTextFormatter:
timeColor := color.New(color.FgHiBlack, color.Bold)
timeColor.EnableColor()
callerColor := color.New(color.FgHiBlue, color.Underline)
callerColor.EnableColor()
callerFnColor := color.New(color.FgHiBlack)
callerFnColor.EnableColor()
fieldKeyColor := color.New(color.FgHiCyan, color.Bold)
fieldKeyColor.EnableColor()
fieldValueColor := color.New(color.FgHiBlack)
fieldValueColor.EnableColor()
causeColor := color.New(color.BgHiRed, color.Bold)
causeColor.EnableColor()
encoder = &TextEntryEncoder{
colorable: true,
debugWriterTo: DebugLevel.ColorableLevelWriterTo(),
infoWriterTo: InfoLevel.ColorableLevelWriterTo(),
warnWriterTo: WarnLevel.ColorableLevelWriterTo(),
errorWriterTo: ErrorLevel.ColorableLevelWriterTo(),
timeColor: timeColor,
callerColor: callerColor,
callerFnColor: callerFnColor,
fieldKeyColor: fieldKeyColor,
fieldValueColor: fieldValueColor,
causeColor: causeColor,
}
outWriter = colorable.NewColorable(outWriter.(*os.File))
errWriter = colorable.NewColorable(errWriter.(*os.File))
break
case JsonFormatter:
encoder = &JsonEntryEncoder{}
break
default:
encoder = &TextEntryEncoder{}
break
}
w = &ConsoleWriter{
encoder: encoder,
out: outWriter,
err: errWriter,
}
return
}
type ConsoleWriter struct {
encoder EntryEncoder
out io.Writer
err io.Writer
}
func (writer *ConsoleWriter) Write(entry Entry) {
p := writer.encoder.Encode(entry)
if entry.Level == ErrorLevel {
_, _ = writer.err.Write(p)
} else {
_, _ = writer.out.Write(p)
}
return
}
func (writer *ConsoleWriter) Close() (err error) {
return
}
type EntryEncoder interface {
Encode(entry Entry) (p []byte)
}
type TextEntryEncoder struct {
colorable bool
debugWriterTo io.WriterTo
infoWriterTo io.WriterTo
warnWriterTo io.WriterTo
errorWriterTo io.WriterTo
timeColor *color.Color
callerColor *color.Color
callerFnColor *color.Color
fieldKeyColor *color.Color
fieldValueColor *color.Color
causeColor *color.Color
}
func (encoder *TextEntryEncoder) Encode(entry Entry) (p []byte) {
buf := bytebufferpool.Get()
_, _ = buf.Write(lqb)
if encoder.colorable {
switch entry.Level {
case DebugLevel:
_, _ = encoder.debugWriterTo.WriteTo(buf)
break
case InfoLevel:
_, _ = encoder.infoWriterTo.WriteTo(buf)
break
case WarnLevel:
_, _ = encoder.warnWriterTo.WriteTo(buf)
break
case ErrorLevel:
_, _ = encoder.errorWriterTo.WriteTo(buf)
break
default:
_, _ = buf.Write(entry.Level.Bytes())
break
}
} else {
_, _ = buf.Write(entry.Level.Bytes())
}
_, _ = buf.Write(rqb)
_, _ = buf.Write(space)
_, _ = buf.Write(lqb)
if encoder.colorable {
_, _ = buf.WriteString(encoder.timeColor.Sprintf("%s", entry.Occur.Format("15:04:05.000")))
} else {
_, _ = buf.WriteString(entry.Occur.Format(time.DateTime))
}
_, _ = buf.Write(rqb)
_, _ = buf.Write(space)
if entry.Caller.File != "" {
if encoder.colorable {
_, _ = buf.WriteString(encoder.timeColor.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line))
_, _ = buf.Write(space)
_, _ = buf.Write(lab)
_, _ = buf.Write(space)
_, _ = buf.WriteString(encoder.callerFnColor.Sprintf("%s", entry.Caller.Fn))
} else {
_, _ = buf.WriteString(fmt.Sprintf("%s:%d > %s", entry.Caller.File, entry.Caller.Line, entry.Caller.Fn))
}
}
_, _ = buf.Write(newline)
_, _ = buf.WriteString(entry.Message)
if entry.Cause != nil {
_, _ = buf.Write(newline)
if encoder.colorable {
_, _ = buf.WriteString(encoder.causeColor.Sprintf("%s", ">>>>>>>>>>>>> ERROR <<<<<<<<<<<<<"))
} else {
_, _ = buf.WriteString(">>>>>>>>>>>>> ERROR <<<<<<<<<<<<<")
}
_, _ = buf.Write(newline)
_, _ = buf.WriteString(fmt.Sprintf("%+v", entry.Cause))
}
if len(entry.Fields) > 0 {
_, _ = buf.Write(newline)
for i, field := range entry.Fields {
if i > 0 {
_, _ = buf.Write(space)
}
if encoder.colorable {
_, _ = buf.WriteString(encoder.fieldKeyColor.Sprintf("%s", field.Key))
_, _ = buf.Write(equal)
_, _ = buf.WriteString(encoder.fieldValueColor.Sprintf("%s", string(field.ValueBytes())))
} else {
_, _ = buf.WriteString(fmt.Sprintf("%s=%s", field.Key, string(field.ValueBytes())))
}
}
}
_, _ = buf.Write(newline)
p = buf.Bytes()
bytebufferpool.Put(buf)
return
}
type JsonEntryEncoder struct {
}
func (encoder *JsonEntryEncoder) Encode(entry Entry) (p []byte) {
p, _ = entry.MarshalJSON()
p = append(p, newline...)
return
}