forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
115 lines (101 loc) · 3.47 KB
/
logger.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
package scribe
import (
"fmt"
"io"
"strings"
)
// A Logger provides a standard logging interface for doing basic low level
// logging tasks as well as debug logging.
type Logger struct {
writer io.Writer
LeveledLogger
Debug LeveledLogger
}
// NewLogger takes a writer and returns a Logger that writes to the given
// writer. The default writter sends all debug logging to io.Discard.
func NewLogger(writer io.Writer) Logger {
return Logger{
writer: writer,
LeveledLogger: NewLeveledLogger(writer),
Debug: NewLeveledLogger(io.Discard),
}
}
// WithLevel takes in a log level string and configures the log level of the
// logger. To enable debug logging the log level must be set to "DEBUG".
func (l Logger) WithLevel(level string) Logger {
switch strings.ToUpper(level) {
case "DEBUG":
return Logger{
writer: l.writer,
LeveledLogger: NewLeveledLogger(l.writer),
Debug: NewLeveledLogger(l.writer),
}
default:
return Logger{
writer: l.writer,
LeveledLogger: NewLeveledLogger(l.writer),
Debug: NewLeveledLogger(io.Discard),
}
}
}
// A LeveledLogger provides a standard interface for basic formatted logging.
type LeveledLogger struct {
TitleWriter io.Writer
ProcessWriter io.Writer
SubprocessWriter io.Writer
ActionWriter io.Writer
DetailWriter io.Writer
SubdetailWriter io.Writer
}
// NewLeveledLogger takes a writer and returns a LeveledLogger that writes to the given
// writer.
func NewLeveledLogger(writer io.Writer) LeveledLogger {
return LeveledLogger{
TitleWriter: NewWriter(writer),
ProcessWriter: NewWriter(writer, WithIndent(1)),
SubprocessWriter: NewWriter(writer, WithIndent(2)),
ActionWriter: NewWriter(writer, WithIndent(3)),
DetailWriter: NewWriter(writer, WithIndent(4)),
SubdetailWriter: NewWriter(writer, WithIndent(5)),
}
}
// Title takes a string and optional formatting, and prints a formatted string
// with zero levels of indentation.
func (l LeveledLogger) Title(format string, v ...interface{}) {
l.printf(l.TitleWriter, format, v...)
}
// Process takes a string and optional formatting, and prints a formatted string
// with one level of indentation.
func (l LeveledLogger) Process(format string, v ...interface{}) {
l.printf(l.ProcessWriter, format, v...)
}
// Subprocess takes a string and optional formatting, and prints a formatted string
// with two levels of indentation.
func (l LeveledLogger) Subprocess(format string, v ...interface{}) {
l.printf(l.SubprocessWriter, format, v...)
}
// Action takes a string and optional formatting, and prints a formatted string
// with three levels of indentation.
func (l LeveledLogger) Action(format string, v ...interface{}) {
l.printf(l.ActionWriter, format, v...)
}
// Detail takes a string and optional formatting, and prints a formatted string
// with four levels of indentation.
func (l LeveledLogger) Detail(format string, v ...interface{}) {
l.printf(l.DetailWriter, format, v...)
}
// Subdetail takes a string and optional formatting, and prints a formatted string
// with five levels of indentation.
func (l LeveledLogger) Subdetail(format string, v ...interface{}) {
l.printf(l.SubdetailWriter, format, v...)
}
// Break inserts a line break in the log output
func (l LeveledLogger) Break() {
l.printf(l.TitleWriter, "\n")
}
func (l LeveledLogger) printf(writer io.Writer, format string, v ...interface{}) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
fmt.Fprintf(writer, format, v...)
}