-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
398 lines (339 loc) · 11.8 KB
/
log.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Package log provides logging mechanisms. It offers
// logging functionality that can include stylized logs, updating progress dots (...), and emojis.
// It respects a TTY parameter. When set to false, all stylization is removed.
package gopherlogs
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/jpmcb/gopherlogs/pkg/colors"
"golang.org/x/term"
)
// DefaultLogLevel controls the default verbosity of log messages.
var DefaultLogLevel = 0
// CMDLogger is the logger implementation used for high-level command line logging.
type CMDLogger struct {
// whether to support stylizing logging output
tty bool
// logging level to respect for this logger
verbosity int
// log level set by a logging event
logLevel int
// instances of indentation (" ") to prepend to a long message
indent int
// termFd maps to the file descriptor of the attached terminal when the logger is initilized
termFd int
// logColor defines the color to log the message as define by fatih/color Attributes
logColor colors.Attribute
// output controls where log messages are sent
output io.Writer
// defaultWriter is used to track the original io.Writer setup for logger. Helps to generate
// the io.MultiWriter when AddLogFile is invoked
defaultWriter io.Writer
}
// Logger provides the logging interaction for the application.
type Logger interface {
// Event takes an emoji codepoint (e.g. "\U0001F609") and presents a log message on it's own line.
// This package provides several standard emoji codepoints as string constants. I.e: logger.HammerEmoji
// Warning: Emojis may have variable width and this method assumes 2 width emojis, adding a space between the emoji and message.
// Emojis provided in this package as string consts have 2 width and work with this method.
// If you wish for additional space, add it at the beginning of the message (string) argument.
Event(emoji, message string)
// Eventf takes an emoji codepoint (e.g. "\U0001F609"), a format string, arguments for the format string.
// This package provides several standard emoji codepoints as string constants. I.e: logger.HammerEmoji
// Warning: Emojis may have variable width and this method assumes 2 width emojis, adding a space between the emoji and message.
// Emojis provided in this package as string consts have 2 width and work with this method.
// If you wish for additional space, add it at the beginning of the message (string) argument.
Eventf(emoji, message string, args ...interface{})
// Info prints a standard log message.
// Line breaks are automatically added to the end.
Info(message string)
// Infof takes a format string, arguments, and prints it as a standard log message.
// Line breaks are not automatically added to the end.
Infof(message string, args ...interface{})
// Warn prints a warning message. When TTY is enabled (default), it will be stylized as yellow.
// Line breaks are automatically added to the end.
Warn(message string)
// Warnf takes a format string, arguments, and prints it as a warning message.
// When TTY is enabled (default), it will be stylized as yellow.
// Line breaks are not automatically added to the end.
Warnf(message string, args ...interface{})
// Error prints an error message. When TTY is enabled (default), it will be stylized as red.
// Line breaks are automatically added to the end.
Error(message string)
// Errorf takes a format string, arguments, and prints it as an error message.
// When TTY is enabled (default), it will be stylized as yellow.
// Line breaks are not automatically added to the end.
Errorf(message string, args ...interface{})
// ReplaceLinef takes a template string message
// and any optional format arguments
// and replaces the current line.
// This is useful after canceling AnimateProgressWithOptions and needing to print a final "success" message
// Ex: ReplaceLinef("Finished reconciling controller: %s", controllerStatus)
ReplaceLinef(message string, args ...interface{})
// AnimateProgressWithOptions takes any number of AnimatorOptions
// and is used to async animate a number of dots.
// See the AnimatorOptions for further documentation
// Ex: AnimateProgressWithOptions(AnimatorWithMaxLen(5))
AnimateProgressWithOptions(options ...AnimatorOption)
// V sets the level of the log message based on an integer. The logger implementation will hold a configured
// log level, which this V level is assessed against to determine whether the log message should be output.
V(level int) Logger
// Style provides indentation and colorization of log messages. The indent argument specifies the amount of " "
// characters to prepend to the message. The color should be specified using color constants in this package.
Style(indent int, c colors.Attribute) Logger
}
// NewLogger returns an instance of Logger, implemented via CMDLogger.
func NewLogger(options ...LoggerOptions) (Logger, error) {
fd := int(os.Stdout.Fd())
l := &CMDLogger{
tty: true,
verbosity: 0,
logLevel: DefaultLogLevel,
output: os.Stdout,
termFd: fd,
defaultWriter: os.Stdout,
}
// Apply given logger options
for _, o := range options {
if err := o.apply(l); err != nil {
return nil, err
}
}
return l, nil
}
func (l *CMDLogger) Event(emoji, message string) {
if l.logLevel > l.verbosity {
return
}
// when tty is off, remove emoji from output
if !l.tty {
emoji = ""
// space is sometimes added to the beginning so that text isn't up against the emoji
// this trims leading space in case that was one.
message = strings.TrimLeft(message, " ")
}
// Print a new line before the event is logged
// so that each event is within it's own "block"
fmt.Print("\n")
// process indentation and ensure a space after the emoji and a new line after message
message = "%s " + message + "\n"
message = processStyle(l, message)
fmt.Fprintf(l.output, message, emoji)
}
func (l *CMDLogger) Eventf(emoji, message string, args ...interface{}) {
if l.logLevel > l.verbosity {
return
}
// when tty is off, remove emoji from output
if !l.tty {
emoji = ""
// space is sometimes added to the beginning so that text isn't up against the emoji
// this trims leading space in case that was one.
message = strings.TrimLeft(message, " ")
}
// Print a new line before the event is logged
// so that each event is within it's own "block"
fmt.Print("\n")
// ensure a space between the emoji and the message
message = emoji + " " + message
message = processStyle(l, message)
fmt.Fprintf(l.output, message, args...)
}
func (l *CMDLogger) Warn(message string) {
if l.logLevel > l.verbosity {
return
}
message = processStyle(l, message)
fmt.Fprintln(l.output, message)
}
func (l *CMDLogger) Warnf(message string, args ...interface{}) {
if l.logLevel > l.verbosity {
return
}
message = processStyle(l, message)
fmt.Fprintf(l.output, message, args...)
}
func (l *CMDLogger) Error(message string) {
if l.logLevel > l.verbosity {
return
}
message = processStyle(l, message)
fmt.Fprintln(l.output, message)
}
func (l *CMDLogger) Errorf(message string, args ...interface{}) {
if l.logLevel > l.verbosity {
return
}
message = processStyle(l, message)
fmt.Fprintf(l.output, message, args...)
}
func (l *CMDLogger) Info(message string) {
if l.logLevel > l.verbosity {
return
}
message = processStyle(l, message)
fmt.Fprintln(l.output, message)
}
func (l *CMDLogger) Infof(message string, args ...interface{}) {
if l.logLevel > l.verbosity {
return
}
message = processStyle(l, message)
fmt.Fprintf(l.output, message, args...)
}
// progressf is an internal method used to log out a specified number of dots
// in addition to a provided message and any format string arguments
func (l *CMDLogger) progressf(count int, message string, args ...interface{}) {
if l.logLevel > l.verbosity {
return
}
if !l.tty {
count = 0
}
// Add dots to message
for i := 0; i < count; i++ {
message += "."
}
// Process message style and ensure we clear the line with \r in tty mode
message = processStyle(l, message)
if l.tty {
message = "\r\033[K" + message
}
// TODO(joshrosso): Is there a better way to do this?
// we pad with extra space to ensure the line we overwrite (\r) is cleaned
// nolint
//message += " "
// when count is 0, a line break should be added at the end
// this support non-tty use cases
if count == 0 {
message += "\n"
}
// Get a temporary string buffer to check it's length
buffer := fmt.Sprintf(message, args...)
// Get the terminal width
termWidth, _, _ := term.GetSize(l.termFd)
// If the length of the message buffer is greater than the width of the terminal,
// then rebuild the message string with a truncated message, leaving trailing space
// to re-add the dots, whitespace and newline
if len(buffer) > termWidth {
sb := strings.Builder{}
for i := 0; i < termWidth-count-15; i++ {
sb.WriteByte(buffer[i])
}
for i := 0; i < count; i++ {
sb.WriteString(".")
}
// TODO - anyway to make this look nicer?
//sb.WriteString(" ")
if count == 0 {
sb.WriteString("\n")
}
buffer = sb.String()
}
fmt.Print(buffer)
}
func (l *CMDLogger) ReplaceLinef(message string, args ...interface{}) {
if l.logLevel > l.verbosity {
return
}
// Process message style and Ensure we clear the line with \r in tty mode
message = processStyle(l, message)
if l.tty {
message = "\r\033[K" + message
}
// TODO(joshrosso): Is there a better way to do this?
// we pad with extra space to ensure the line we overwrite (\r) is cleaned
//message += " "
// add a line break
// this also supports non-tty use cases
message += "\n"
fmt.Fprintf(l.output, message, args...)
}
func (l *CMDLogger) AnimateProgressWithOptions(options ...AnimatorOption) {
opts := &progressAnimatorOptions{
maxLen: 5,
}
// Apply given animation options
for _, o := range options {
o.apply(opts)
}
currentLen := 1
status := ""
for {
select {
case <-opts.ctx.Done():
return
case status = <-opts.statChan:
// noop - this gets the newest status from the status channel
default:
// noop - this is used to fallthrough to the processing logic below
// when there is no status channel or there's no status update
}
// Build the format args that eventually get passed to fmt.Fprintf
// Always expect the status to be first
fArgs := make([]interface{}, 0)
if opts.statChan != nil {
fArgs = append(fArgs, status)
}
if len(opts.messagefArgs) != 0 {
for _, arg := range opts.messagefArgs {
fArgs = append(fArgs, arg)
}
}
if len(fArgs) == 0 {
l.progressf(currentLen, opts.messagef)
} else {
l.progressf(currentLen, opts.messagef, fArgs...)
}
currentLen++
time.Sleep(1 * time.Second)
if currentLen > opts.maxLen {
currentLen = 1
}
}
}
func (l *CMDLogger) V(level int) Logger {
return &CMDLogger{
tty: l.tty,
logLevel: level,
verbosity: l.verbosity,
output: l.output,
defaultWriter: l.defaultWriter,
}
}
func (l *CMDLogger) Style(indent int, c colors.Attribute) Logger {
// if tty is disable, don't return a style-capable logger
if !l.tty {
return l
}
return &CMDLogger{
tty: l.tty,
verbosity: l.verbosity,
logLevel: l.logLevel,
indent: indent,
logColor: c,
output: l.output,
defaultWriter: l.defaultWriter,
}
}
// processStyle adds indentation and color based on the configured CMDLogger.
// When tty is false, stylization arguments are ignored.
func processStyle(l *CMDLogger, message string) string {
// when tty is off, do no stylization
if !l.tty {
return message
}
// render indentation
for i := 0; i < l.indent; i++ {
message = " " + message
}
// apply color value to entire message
if l.logColor != 0 {
// Similar to log printing in fatih/color.Sprint
message = fmt.Sprintf("%s[%dm%s%s[%dm", colors.Escape, l.logColor, message, colors.Escape, colors.Reset)
}
return message
}