-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging.go
291 lines (243 loc) · 8.94 KB
/
logging.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
// Copyright 2024 Adam Chalkley
//
// https://github.com/atc0005/go-nagios
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package nagios
import (
"io"
"log"
"os"
"strings"
)
// Logger related values set as constants so that their values are exposed to
// internal tests.
const (
logMsgPrefix string = "[" + MyPackageName + "] "
logFlags int = log.Ldate | log.Ltime
// using log.Lshortfile reports the helper function use instead of the
// caller's location.
//
// logFlags int = log.Ldate | log.Ltime | log.Lshortfile
)
// debugLoggingOptions controls all debug logging behavior for this library.
type debugLoggingOptions struct {
// actions indicates whether actions taken by this library are logged.
// This covers enabling/disabling settings or other general plugin
// activity.
actions bool
// pluginOutputSize indicates whether all output to the configured plugin
// output sink should be measured and written to the log output sink.
pluginOutputSize bool
}
// defaultPluginDebugLoggingOutputTarget returns the default debug logging
// output target used when a user-specified value is not provided.
func defaultPluginDebugLoggingOutputTarget() io.Writer {
return os.Stderr
}
// defaultPluginAbortMessageOutputTarget returns the default abort message
// output target.
func defaultPluginAbortMessageOutputTarget() io.Writer {
return os.Stderr
}
// defaultPluginDebugLoggerTarget returns the default debug logger target used
// when a user-specified value is not provided for the debug output target.
func defaultPluginDebugLoggerTarget() io.Writer {
// The intended default behavior is to throw away debug log messages if a
// debug log message output target has not been specified.
return io.Discard
}
// allDebugLoggingOptionsEnabled is a helper function that provides a
// debugLoggingOptions value with all settings enabled.
func allDebugLoggingOptionsEnabled() debugLoggingOptions {
return debugLoggingOptions{
actions: true,
pluginOutputSize: true,
// Expand this for any new fields added in the future.
}
}
// allDebugLoggingOptionsDisabled is a helper function that provides a
// debugLoggingOptions value with all settings disabled.
func allDebugLoggingOptionsDisabled() debugLoggingOptions {
return debugLoggingOptions{
actions: false,
pluginOutputSize: false,
// Expand this for any new fields added in the future.
}
}
// enableAll enables all debug logging options. The user is able to optionally
// disable select portions of the debug logging output that they do not wish
// to see.
func (dlo *debugLoggingOptions) enableAll() {
*dlo = allDebugLoggingOptionsEnabled()
}
// disableAll disables all debug logging options.
func (dlo *debugLoggingOptions) disableAll() {
*dlo = allDebugLoggingOptionsDisabled()
}
// enableActions enables logging plugin actions.
func (dlo *debugLoggingOptions) enableActions() {
dlo.actions = true
}
// disableActions disables logging plugin actions.
func (dlo *debugLoggingOptions) disableActions() {
dlo.actions = false
}
// enablePluginOutputSize enables logging plugin output size.
func (dlo *debugLoggingOptions) enablePluginOutputSize() {
dlo.pluginOutputSize = true
}
// disablePluginOutputSize disables logging plugin output size.
func (dlo *debugLoggingOptions) disablePluginOutputSize() {
dlo.pluginOutputSize = false
}
// DebugLoggingEnableAll changes the default state of all debug logging
// options for this library from disabled to enabled.
//
// Once enabled, debug logging output is emitted to os.Stderr. This can be
// overridden by explicitly setting a custom debug output target.
func (p *Plugin) DebugLoggingEnableAll() {
// Enable all (granular) debug log options.
p.debugLogging.enableAll()
// Ensure we have a valid output target, but do not overwrite any custom
// target already set.
if p.logOutputSink == nil {
p.setFallbackDebugLogTarget()
}
// Connect logger to configured debug log target.
p.setupLogger()
}
// DebugLoggingDisableAll changes the default state of all debug logging
// options for this library from any custom state back to the default state of
// disabled.
//
// Any custom debug log output target remains as it was before calling this
// function.
func (p *Plugin) DebugLoggingDisableAll() {
p.debugLogging.disableAll()
}
// DebugLoggingDisableActions disables debug logging of general "actions" or
// plugin activity. This is the most verbose debug logging output generated by
// this library.
func (p *Plugin) DebugLoggingDisableActions() {
p.debugLogging.disableActions()
}
// DebugLoggingEnableActions enables debug logging of general "actions" or
// plugin activity. This is the most verbose debug logging output generated by
// this library.
//
// Once enabled, debug logging output is emitted to os.Stderr. This can be
// overridden by explicitly setting a custom debug output target.
func (p *Plugin) DebugLoggingEnableActions() {
p.debugLogging.enableActions()
// Ensure we have a valid output target, but do not overwrite any custom
// target already set.
if p.logOutputSink == nil {
p.setFallbackDebugLogTarget()
}
// Connect logger to configured debug log target.
p.setupLogger()
}
// DebugLoggingDisablePluginOutputSize disables debug logging of plugin output
// size calculations.
func (p *Plugin) DebugLoggingDisablePluginOutputSize() {
p.debugLogging.disablePluginOutputSize()
}
// DebugLoggingEnablePluginOutputSize enables debug logging of plugin output
// size calculations. This debug logging output produces minimal output.
//
// Once enabled, debug logging output is emitted to os.Stderr. This can be
// overridden by explicitly setting a custom debug output target.
func (p *Plugin) DebugLoggingEnablePluginOutputSize() {
p.debugLogging.enablePluginOutputSize()
// Ensure we have a valid output target, but do not overwrite any custom
// target already set.
if p.logOutputSink == nil {
p.setFallbackDebugLogTarget()
}
// Connect logger to configured debug log target.
p.setupLogger()
}
// SetDebugLoggingOutputTarget overrides the current debug logging target with
// the given output target. If the given output target is not valid the
// current target will be used instead. If there isn't a debug logging target
// already set then the default debug logging output target of os.Stderr will
// be used. This behavior is chosen for consistency with the current behavior
// of the Plugin.SetOutputTarget function.
//
// NOTE: While an error message is logged when calling this function with an
// invalid target, calling this function does not change the default debug
// logging state from disabled to enabled. That step must be performed
// separately by either enabling all debug logging options OR enabling select
// debug logging options.
func (p *Plugin) SetDebugLoggingOutputTarget(w io.Writer) {
if w == nil {
if p.logOutputSink == nil {
p.setFallbackDebugLogTarget()
}
// Connect logger to configured debug log target.
p.setupLogger()
// We log using an "unfiltered" logger call to ensure this has the
// best chance of being seen.
p.log("invalid output target provided; using default debug log target instead")
return
}
p.logOutputSink = w
// Connect logger to configured debug log target.
p.setupLogger()
// Use a filtered logger call to allow this message to be emitted or
// excluded based on user-specified debug logging settings.
p.logAction("custom debug logging target set as requested")
}
// DebugLoggingOutputTarget returns the user-specified debug output target or
// the default value if one was not specified.
func (p *Plugin) DebugLoggingOutputTarget() io.Writer {
if p.logOutputSink == nil {
return defaultPluginDebugLoggingOutputTarget()
}
return p.logOutputSink
}
func (p *Plugin) setFallbackDebugLogTarget() {
p.logOutputSink = defaultPluginDebugLoggingOutputTarget()
}
// setupLogger should be called after the debug log output sink is explicitly
// configured. If called before configuring the debug log output sink the
// plugin's default debug logger target will be used instead.
func (p *Plugin) setupLogger() {
var loggerTarget io.Writer
switch {
case p.logOutputSink == nil:
loggerTarget = defaultPluginDebugLoggerTarget()
default:
loggerTarget = p.logOutputSink
}
p.logger = log.New(loggerTarget, logMsgPrefix, logFlags)
}
// log uses the plugin's logger to write the given message to the configured
// output sink.
func (p *Plugin) log(msg string) {
if p.logger == nil {
return
}
if !strings.HasSuffix(msg, CheckOutputEOL) {
msg += CheckOutputEOL
}
p.logger.Print(msg)
}
// logAction is used to log actions taken by this library such as
// enabling/disabling settings or other general plugin activity.
func (p *Plugin) logAction(msg string) {
if !p.debugLogging.actions {
return
}
p.log(msg)
}
// logPluginOutputSize is used to log activity related to measuring all output
// to the configured plugin output sink.
func (p *Plugin) logPluginOutputSize(msg string) {
if !p.debugLogging.pluginOutputSize {
return
}
p.log(msg)
}