-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevelopment_log_sink.go
223 lines (186 loc) · 6.85 KB
/
development_log_sink.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
217
218
219
220
221
222
223
package simplelogr
import (
"bytes"
"encoding/json"
"fmt"
"io"
"time"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/pkg/errors"
)
// DevelopmentLogSink emits unstructured, optionally coloured, text representations of log Entry objects - intended
// for ease of reading in terminals during local development
type DevelopmentLogSink struct {
options DevelopmentLogSinkOptions
}
// NewDevelopmentLogSink creates a new DevelopmentLogSink with the provided options
func NewDevelopmentLogSink(opts DevelopmentLogSinkOptions) *DevelopmentLogSink {
sink := &DevelopmentLogSink{
options: opts,
}
allColours := []*color.Color{
sink.options.PrimaryColour,
sink.options.SecondaryColour,
}
for _, c := range sink.options.SeverityColours {
allColours = append(allColours, c)
}
switch sink.options.ColouredOutput {
case ColourModeAuto:
// do nothing, let the color package do its magic
case ColourModeForceOn:
for _, c := range allColours {
c.EnableColor()
}
case ColourModeForceOff:
for _, c := range allColours {
c.DisableColor()
}
}
return sink
}
// Log implements LogSink, encoding the given Entry as human-readable text before writing it to the configured io.Writer
func (d DevelopmentLogSink) Log(e Entry) error {
buffer := bytes.Buffer{}
severity := d.options.SeverityEncoder(e.Level, e.Error)
severityColour := d.options.SeverityColours[severity]
if severityColour == nil {
severityColour = d.options.PrimaryColour
}
if _, err := d.options.SecondaryColour.Fprint(&buffer, d.options.TimestampEncoder(e.Timestamp)); err != nil {
return err
}
if _, err := severityColour.Fprintf(&buffer, "%s%s", d.options.SpaceSeparator, severity); err != nil {
return err
}
if len(e.Names) > 0 {
if _, err := d.options.PrimaryColour.Fprintf(&buffer, "%s%s", d.options.SpaceSeparator, d.options.NameEncoder(e.Names)); err != nil {
return err
}
}
if _, err := d.options.PrimaryColour.Fprintf(&buffer, "%s%s", d.options.SpaceSeparator, e.Message); err != nil {
return err
}
var encodedErr EncodedError
if e.Error != nil {
encodedErr = d.options.ErrorEncoder(e.Error)
if _, err := severityColour.Fprintf(&buffer, "%s%s=%q", d.options.SpaceSeparator, d.options.ErrorKey, encodedErr.Message); err != nil {
return err
}
}
for i := 0; i < len(e.KVs); i += 2 {
k := e.KVs[i]
v := e.KVs[i+1]
kStr, ok := k.(string)
if !ok {
return errors.Errorf("logging keys must be strings, got %T: %v", k, k)
}
if _, err := d.options.SecondaryColour.Fprintf(&buffer, "%s%s=", d.options.SpaceSeparator, kStr); err != nil {
return err
}
b, err := json.Marshal(v)
if err != nil {
return err
}
if _, err := d.options.PrimaryColour.Fprintf(&buffer, "%s", b); err != nil {
return err
}
}
if encodedErr.StackTrace != "" {
if _, err := d.options.PrimaryColour.Fprintf(&buffer, "%s", encodedErr.StackTrace); err != nil {
return err
}
}
if _, err := fmt.Fprintf(d.options.Output, "%s%s", buffer.String(), d.options.EntrySuffix); err != nil {
return err
}
return nil
}
var _ LogSink = (*DevelopmentLogSink)(nil)
// ColourMode controls whether the DevelopmentLogSink emits coloured output or not
type ColourMode int
const (
// ColourModeAuto will use the color package's built-in auto-detection to guess whether to use coloured output
ColourModeAuto ColourMode = iota
// ColourModeForceOff forces coloured output to be disabled, use this if you're seeing garbled escape characters
// in the output
ColourModeForceOff
// ColourModeForceOn forces coloured output to be produced, use this if you're using an integrated terminal in
// your development IDE and aren't getting coloured output
ColourModeForceOn
)
// DevelopmentLogSinkOptions configures the behaviour of a DevelopmentLogSink
type DevelopmentLogSinkOptions struct {
// Output configures where to write logs to
Output io.Writer
// ColouredOutput determines whether coloured output will be used, if unspecified it will attempt to auto-detect
// from the environment. This is usually confused by integrated terminals in IDEs, so for coloured output in IDEs
// you may wish to use ColourModeForceOn
ColouredOutput ColourMode
// SeverityColours maps severity names (produced by SeverityEncoder) to colours, used when displaying severity names
// and when Entry objects contain an Entry.Error
SeverityColours map[string]*color.Color
// PrimaryColour is the colour of log messages, logger names, and the values of key-value pairs
PrimaryColour *color.Color
// SecondaryColour is the colour of timestamps, and the keys of key-value pairs
SecondaryColour *color.Color
// SeverityEncoder identifies the severity name based on the verbosity level and the presence of any errors
SeverityEncoder func(level int, err error) string
// NameEncoder collapses the series of Logger names down into one string for logging
NameEncoder func(names []string) string
// TimestampEncoder formats timestamps into string representations
TimestampEncoder func(t time.Time) string
// ErrorKey determines the key prefix on any error messages, displayed as though "just another key-value pair",
// but (if colours are enabled) printed using the relevant colour (see SeverityColours)
ErrorKey string
// ErrorEncoder extracts loggable EncodedError information from an error
ErrorEncoder func(err error) EncodedError
// EntrySuffix is appended to the end of log entries, typically to add a newline between them
EntrySuffix string
// SpaceSeparator is placed between all log elements: timestamp, severity, logger name, message, and key-value pairs
// It can be useful, for example, to change this to "\t" to increase spacing - which may improve readability
SpaceSeparator string
}
// AssertDefaults replaces all uninitialised options with reasonable defaults
func (d *DevelopmentLogSinkOptions) AssertDefaults() {
if d.Output == nil {
d.Output = colorable.NewColorableStdout()
}
if d.SeverityColours == nil {
d.SeverityColours = map[string]*color.Color{}
for severity, colour := range DefaultSeverityColours {
colourCopy := *colour
d.SeverityColours[severity] = &colourCopy
}
}
if d.PrimaryColour == nil {
colourCopy := *DefaultPrimaryColour
d.PrimaryColour = &colourCopy
}
if d.SecondaryColour == nil {
colourCopy := *DefaultSecondaryColour
d.SecondaryColour = &colourCopy
}
if d.SeverityEncoder == nil {
d.SeverityEncoder = DefaultSeverityEncoder(DefaultSeverity, DefaultErrorSeverity, DefaultSeverityThresholds)
}
if d.NameEncoder == nil {
d.NameEncoder = DefaultNameEncoder(DefaultNameSeparator)
}
if d.TimestampEncoder == nil {
d.TimestampEncoder = DefaultTimestampEncoder(DefaultTimestampFormat)
}
if d.ErrorKey == "" {
d.ErrorKey = DefaultErrorKey
}
if d.ErrorEncoder == nil {
d.ErrorEncoder = DefaultErrorEncoder
}
if d.EntrySuffix == "" {
d.EntrySuffix = DefaultEntrySuffix
}
if d.SpaceSeparator == "" {
d.SpaceSeparator = DefaultSpaceSeparator
}
}