-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackbox.go
190 lines (164 loc) · 5.32 KB
/
blackbox.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
package blackbox
import (
"fmt"
"os"
)
// Logger will take log messages and write them to the targets provided
type Logger struct {
level Level
targetSet *targetSet
context Ctx
}
// New creates a new blackbox logger
func New() *Logger {
return &Logger{
level: Trace,
targetSet: &targetSet{},
context: make(Ctx, 0),
}
}
// NewWithCtx creates a new blackbox logger with a given context
func NewWithCtx(contextData Ctx) *Logger {
logger := New()
return logger.WithCtx(contextData)
}
// Log logs values to the loggers targets at the given log level. Any values
// that have a String method, it's return value will be used instead.
func (l *Logger) Log(level Level, values ...interface{}) *Logger {
if level < l.level {
return l
}
l.targetSet.log(level, values, l.context)
return l
}
// Logf works the same way as fmt.Printf. Provide a format string and any
// values you wish.
func (l *Logger) Logf(level Level, format string, values ...interface{}) *Logger {
l.Log(level, fmt.Sprintf(format, values...))
return l
}
// Trace is a convenience method for logging values at the trace log level. It
// behaves the same as Log.
func (l *Logger) Trace(values ...interface{}) *Logger {
l.Log(Trace, values...)
return l
}
// Tracef is a convenience method for logging values at the trace log level. It
// behaves the same as Logf.
func (l *Logger) Tracef(format string, values ...interface{}) *Logger {
l.Logf(Trace, format, values...)
return l
}
// Debug is a convenience method for logging values at the debug log level. It
// behaves the same as Log.
func (l *Logger) Debug(values ...interface{}) *Logger {
l.Log(Debug, values...)
return l
}
// Debugf is a convenience method for logging values at the debug log level. It
// behaves the same as Logf.
func (l *Logger) Debugf(format string, values ...interface{}) *Logger {
l.Logf(Debug, format, values...)
return l
}
// Verbose is a convenience method for logging values at the verbose log level. It
// behaves the same as Log.
func (l *Logger) Verbose(values ...interface{}) *Logger {
l.Log(Verbose, values...)
return l
}
// Verbosef is a convenience method for logging values at the verbose log level. It
// behaves the same as Logf.
func (l *Logger) Verbosef(format string, values ...interface{}) *Logger {
l.Logf(Verbose, format, values...)
return l
}
// Info is a convenience method for logging values at the info log level. It
// behaves the same as Log.
func (l *Logger) Info(values ...interface{}) *Logger {
l.Log(Info, values...)
return l
}
// Infof is a convenience method for logging values at the info log level. It
// behaves the same as Logf.
func (l *Logger) Infof(format string, values ...interface{}) *Logger {
l.Logf(Info, format, values...)
return l
}
// Warn is a convenience method for logging values at the warn log level. It
// behaves the same as Log.
func (l *Logger) Warn(values ...interface{}) *Logger {
l.Log(Warn, values...)
return l
}
// Warnf is a convenience method for logging values at the warn log level. It
// behaves the same as Logf.
func (l *Logger) Warnf(format string, values ...interface{}) *Logger {
l.Logf(Warn, format, values...)
return l
}
// Error is a convenience method for logging values at the error log level. It
// behaves the same as Log.
func (l *Logger) Error(values ...interface{}) *Logger {
l.Log(Error, values...)
return l
}
// Errorf is a convenience method for logging values at the error log level. It
// behaves the same as Logf.
func (l *Logger) Errorf(format string, values ...interface{}) *Logger {
l.Logf(Error, format, values...)
return l
}
// Fatal is a convenience method for logging values at the fatal log level. It
// behaves the same as Log with the exception that it exits the program with
// code 1.
func (l *Logger) Fatal(values ...interface{}) {
l.Log(Fatal, values...)
os.Exit(1)
}
// Fatalf is a convenience method for logging values at the fatal log level. It
// behaves the same as Logf with the exception that it exits the program with
// code 1.
func (l *Logger) Fatalf(format string, values ...interface{}) {
l.Logf(Fatal, format, values...)
os.Exit(1)
}
// Panic is a convenience method for logging values at the panic log level. It
// behaves the same as Log.
func (l *Logger) Panic(values ...interface{}) {
l.Log(Panic, values...)
panic(fmt.Sprint(values...))
}
// Panicf is a convenience method for logging values at the panic log level. It
// behaves the same as Logf.
func (l *Logger) Panicf(format string, values ...interface{}) {
l.Logf(Panic, format, values...)
panic(fmt.Sprint(values...))
}
// SetLevel sets the log level across all targets at once.
func (l *Logger) SetLevel(level Level) {
l.level = level
}
// AddTarget adds a io.Writer to be written to
func (l *Logger) AddTarget(target Target) {
l.targetSet.addTarget(target)
}
// WithCtx takes a context, merging it with the current one, and creates a new
// sub logger from the merged context. This new logger will have the same
// target set as the one WithCtx is called upon.
func (l *Logger) WithCtx(context Ctx) *Logger {
return &Logger{
level: l.level,
context: l.context.Extend(context),
targetSet: l.targetSet,
}
}
// GetCtx returns a ctx instance containing a copy of the logger's internal
// context data.
func (l *Logger) GetCtx() Ctx {
ctx := make(Ctx, 0)
for key, value := range l.context {
ctx[key] = value
}
return ctx
}