-
Notifications
You must be signed in to change notification settings - Fork 72
/
std_logger.go
156 lines (133 loc) · 4.24 KB
/
std_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
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
// This file contains a logger that uses the standard output and error streams, or custom writers.
package logging
import (
"fmt"
"io"
"os"
)
// StdLoggerBuilder contains the configuration and logic needed to build a logger that uses the
// standard output and error streams, or custom writers.
type StdLoggerBuilder struct {
debugEnabled bool
infoEnabled bool
errorEnabled bool
outStream io.Writer
errStream io.Writer
}
// StdLogger is a logger that uses the standard output and error streams, or custom writers.
type StdLogger struct {
debugEnabled bool
infoEnabled bool
errorEnabled bool
outStream io.Writer
errStream io.Writer
}
// NewStdLoggerBuilder creates a builder that knows how to build a logger that uses the standard
// output and error streams, or custom writers. By default these loggers will have enabled the
// information, warning and error levels
func NewStdLoggerBuilder() *StdLoggerBuilder {
// Allocate the object:
builder := new(StdLoggerBuilder)
// Set default values:
builder.debugEnabled = false
builder.infoEnabled = true
builder.errorEnabled = true
return builder
}
// Streams sets the standard output and error streams to use. If not used then the logger will use
// os.Stdout and os.Stderr.
func (b *StdLoggerBuilder) Streams(out io.Writer, err io.Writer) *StdLoggerBuilder {
b.outStream = out
b.errStream = err
return b
}
// Debug enables or disables the debug level.
func (b *StdLoggerBuilder) Debug(flag bool) *StdLoggerBuilder {
b.debugEnabled = flag
return b
}
// Info enables or disables the information level.
func (b *StdLoggerBuilder) Info(flag bool) *StdLoggerBuilder {
b.infoEnabled = flag
return b
}
// Error enables or disables the error level.
func (b *StdLoggerBuilder) Error(flag bool) *StdLoggerBuilder {
b.errorEnabled = flag
return b
}
// Build creates a new logger using the configuration stored in the builder.
func (b *StdLoggerBuilder) Build() (logger *StdLogger, err error) {
// Allocate and populate the object:
logger = new(StdLogger)
logger.debugEnabled = b.debugEnabled
logger.infoEnabled = b.infoEnabled
logger.errorEnabled = b.errorEnabled
logger.outStream = b.outStream
logger.errStream = b.errStream
if logger.outStream == nil {
logger.outStream = os.Stdout
}
if logger.errStream == nil {
logger.errStream = os.Stderr
}
return
}
// SetDebug sets/unsets the debug level for the logger
func (l *StdLogger) SetDebug(b bool) {
l.debugEnabled = b
}
// DebugEnabled returns true iff the debug level is enabled.
func (l *StdLogger) DebugEnabled() bool {
return l.debugEnabled
}
// InfoEnabled returns true iff the information level is enabled.
func (l *StdLogger) InfoEnabled() bool {
return l.infoEnabled
}
// ErrorEnabled returns true iff the error level is enabled.
func (l *StdLogger) ErrorEnabled() bool {
return l.errorEnabled
}
// Info sends to the log an information message formatted using the fmt.Fprintln function and the
// given arguments.
func (l *StdLogger) Info(args ...interface{}) {
if l.infoEnabled {
fmt.Fprintln(l.errStream, args...)
}
}
// Infof sends to the log an information message formatted using the fmt.Fprintf function and the
// given format and arguments.
func (l *StdLogger) Infof(format string, args ...interface{}) {
if l.infoEnabled {
fmt.Fprintf(l.errStream, format+"\n", args...)
}
}
// Debug sends to the log a debug message formatted using the fmt.Fprintln function and the given
// arguments.
func (l *StdLogger) Debug(args ...interface{}) {
if l.debugEnabled {
fmt.Fprintln(l.errStream, args...)
}
}
// Debugf sends to the log a debug message formatted using the fmt.Fprintf function and the given
// format and arguments.
func (l *StdLogger) Debugf(format string, args ...interface{}) {
if l.debugEnabled {
fmt.Fprintf(l.errStream, format+"\n", args...)
}
}
// Error sends to the log an error message formatted using the fmt.Fprintln function and the given
// arguments.
func (l *StdLogger) Error(args ...interface{}) {
if l.infoEnabled {
fmt.Fprintln(l.errStream, args...)
}
}
// Errorf sends to the log an error message formatted using the fmt.Fprintf function and the given
// format and arguments.
func (l *StdLogger) Errorf(format string, args ...interface{}) {
if l.infoEnabled {
fmt.Fprintf(l.errStream, format+"\n", args...)
}
}