Repository navigation
Expand file tree
/
Copy pathlog.go
More file actions
57 lines (44 loc) · 1.58 KB
/
Copy pathlog.go
File metadata and controls
57 lines (44 loc) · 1.58 KB
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
package logger
import (
"fmt"
io2 "github.com/stackrox/rox/roxctl/common/io"
"github.com/stackrox/rox/roxctl/common/printer"
)
// Logger is a struct responsible for printing messages. It should be preferred over fmt functions.
type Logger interface {
// ErrfLn prints a formatted string with a newline, prefixed with ERROR and colorized
ErrfLn(format string, a ...interface{})
// WarnfLn prints a formatted string with a newline, prefixed with WARN and colorized
WarnfLn(format string, a ...interface{})
// InfofLn prints a formatted string with a newline, prefixed with INFO and colorized
InfofLn(format string, a ...interface{})
// PrintfLn prints a formatted string with newline at the end
PrintfLn(format string, a ...interface{})
}
type logger struct {
io io2.IO
printer printer.ColorfulPrinter
}
// NewLogger returns new instance of Logger
func NewLogger(io io2.IO, colorfulPrinter printer.ColorfulPrinter) Logger {
return &logger{
io: io,
printer: colorfulPrinter,
}
}
func (l *logger) ErrfLn(format string, a ...interface{}) {
l.printer.Err(l.io.ErrOut(), format+"\n", a...)
}
func (l *logger) WarnfLn(format string, a ...interface{}) {
l.printer.Warn(l.io.ErrOut(), format+"\n", a...)
}
func (l *logger) InfofLn(format string, a ...interface{}) {
l.printer.Info(l.io.ErrOut(), format+"\n", a...)
}
func (l *logger) PrintfLn(format string, a ...interface{}) {
_, _ = fmt.Fprint(l.io.Out(), l.colorWords(format+"\n", a...))
}
func (l *logger) colorWords(format string, a ...interface{}) string {
str := fmt.Sprintf(format, a...)
return l.printer.ColorWords(str)
}