-
Notifications
You must be signed in to change notification settings - Fork 22
/
toggledlogger.go
175 lines (148 loc) · 5.04 KB
/
toggledlogger.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package toggledlogger
import (
"io"
"log"
"sync/atomic"
"github.com/hashicorp/go-hclog"
)
// ToggledLogger is used to provide an hclog-compatible interface that can be
// turned on/off at will. This is useful for providing a logger where you only
// want logging to happen in specific debugging scenarios. The enabled flag is
// an atomic bool; a common pattern with this library will be to read a config
// value at SIGHUP and set the bool to the currently desired value.
//
// When using With and Named and such, the returned logger will share the
// enabled state of this logger. If this is not what you want, create a new
// ToggledLogger with the result of With or Name directly called on the
// underlying logger.
type ToggledLogger struct {
enabled *atomic.Bool
underlying hclog.Logger
}
// Ensure that we are implementing hclog.Logger
var _ hclog.Logger = (*ToggledLogger)(nil)
// NewToggledLogger creates a new logger, without performing nil checking on the
// underlying logger.
//
// If enabled is provided, it is used when checking whether to log, which can be
// useful for tying lifecycle of enablement of this logger to some other
// process. If nil, an internal value will be created and used.
func NewToggledLogger(underlying hclog.Logger, enabled *atomic.Bool) hclog.Logger {
tl := &ToggledLogger{
underlying: underlying,
enabled: enabled,
}
if enabled == nil {
tl.enabled = new(atomic.Bool)
}
return tl
}
// SetEnabled sets the enabled state of the logger
func (tl *ToggledLogger) SetEnabled(enabled bool) {
tl.enabled.Store(enabled)
}
// Log logs a message at the given level, if this logger is enabled
func (tl *ToggledLogger) Log(level hclog.Level, msg string, args ...any) {
if tl.enabled.Load() {
tl.underlying.Log(level, msg, args...)
}
}
// Trace logs a message at the Trace level, if this logger is enabled
func (tl *ToggledLogger) Trace(msg string, args ...any) {
if tl.enabled.Load() {
tl.underlying.Trace(msg, args...)
}
}
// Debug logs a message at the Debug level, if this logger is enabled
func (tl *ToggledLogger) Debug(msg string, args ...any) {
if tl.enabled.Load() {
tl.underlying.Debug(msg, args...)
}
}
// Info logs a message at the Info level, if this logger is enabled
func (tl *ToggledLogger) Info(msg string, args ...any) {
if tl.enabled.Load() {
tl.underlying.Info(msg, args...)
}
}
// Warn logs a message at the Warn level, if this logger is enabled
func (tl *ToggledLogger) Warn(msg string, args ...any) {
if tl.enabled.Load() {
tl.underlying.Warn(msg, args...)
}
}
// Error logs a message at the Error level, if this logger is enabled
func (tl *ToggledLogger) Error(msg string, args ...any) {
if tl.enabled.Load() {
tl.underlying.Error(msg, args...)
}
}
// IsTrace returns the call of the same name on the underlying logger
func (tl *ToggledLogger) IsTrace() bool {
return tl.underlying.IsTrace()
}
// IsDebug returns the call of the same name on the underlying logger
func (tl *ToggledLogger) IsDebug() bool {
return tl.underlying.IsDebug()
}
// IsInfo returns the call of the same name on the underlying logger
func (tl *ToggledLogger) IsInfo() bool {
return tl.underlying.IsInfo()
}
// IsWarn returns the call of the same name on the underlying logger
func (tl *ToggledLogger) IsWarn() bool {
return tl.underlying.IsWarn()
}
// IsError returns the call of the same name on the underlying logger
func (tl *ToggledLogger) IsError() bool {
return tl.underlying.IsError()
}
// ImpliedArgs returns the implied args of the underlying logger
func (tl *ToggledLogger) ImpliedArgs() []any {
return tl.underlying.ImpliedArgs()
}
// With returns the underlying logger with With called, but shares this logger's
// enabled state
func (tl *ToggledLogger) With(args ...any) hclog.Logger {
return &ToggledLogger{
underlying: tl.underlying.With(args...),
enabled: tl.enabled,
}
}
func (tl *ToggledLogger) Name() string {
return tl.underlying.Name()
}
// Named returns the underlying logger with Named called, but shares this
// logger's enabled state
func (tl *ToggledLogger) Named(name string) hclog.Logger {
return &ToggledLogger{
underlying: tl.underlying.Named(name),
enabled: tl.enabled,
}
}
// ResetNamed returns the underlying logger with ResetNamed called, but shares
// this logger's enabled state
func (tl *ToggledLogger) ResetNamed(name string) hclog.Logger {
return &ToggledLogger{
underlying: tl.underlying.ResetNamed(name),
enabled: tl.enabled,
}
}
// GetLevel returns the current level
func (tl *ToggledLogger) GetLevel() hclog.Level {
return tl.underlying.GetLevel()
}
// SetLevel sets the level of the underlying logger
func (tl *ToggledLogger) SetLevel(level hclog.Level) {
tl.underlying.SetLevel(level)
}
// StandardLogger is currently not supported and returns nil
func (tl *ToggledLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
return nil
}
// StandardWriter is currently not supported and returns nil
func (tl *ToggledLogger) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return nil
}