-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple.go
More file actions
275 lines (255 loc) · 8.33 KB
/
multiple.go
File metadata and controls
275 lines (255 loc) · 8.33 KB
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
// Copyright 2021 - 2023 PurpleSec Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package logx
import (
"fmt"
"os"
)
// Multi is a type of Log that is an alias for an array where each Log function
// will affect each Log instance in the array.
type Multi []Log
// Add appends the specified Log 'l' the Stack array.
func (m *Multi) Add(l Log) {
if l == nil {
return
}
*m = append(*m, l)
}
// Multiple returns a Stack struct that contains the Log instances
// specified in the 'l' vardict.
func Multiple(l ...Log) *Multi {
m := Multi(l)
return &m
}
// SetLevel changes the current logging level of this Log instance.
func (m Multi) SetLevel(l Level) {
for i := range m {
m[i].SetLevel(l)
}
}
// SetPrefix changes the current logging prefox of this Log instance.
func (m Multi) SetPrefix(p string) {
for i := range m {
m[i].SetPrefix(p)
}
}
// SetPrintLevel sets the logging level used when 'Print*' statements are called.
func (m Multi) SetPrintLevel(n Level) {
for i := range m {
m[i].SetPrintLevel(n)
}
}
// Print writes a message to the logger.
//
// The function arguments are similar to 'fmt.Sprint' and 'fmt.Print'. The only
// argument is a vardict of interfaces that can be used to output a string value.
//
// This function is affected by the setting of 'SetPrintLevel'. By default,
// this will print as an 'Info' logging message.
func (m Multi) Print(v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Print, 1, "", v...)
} else {
m[i].Print(v...)
}
}
}
// Panic writes a panic message to the logger.
//
// This function will result in the program exiting with a Go 'panic()' after
// being called. The function arguments are similar to 'fmt.Sprint' and 'fmt.Print.'
// The only argument is a vardict of interfaces that can be used to output a
// string value.
func (m Multi) Panic(v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Panic, 1, "", v...)
} else {
// NOTE(dij): Write as Error here to prevent the non-flexable logger
// from exiting the program before all logs can be written.
m[i].Error("", v...)
}
}
panic(fmt.Sprint(v...))
}
// Println writes a message to the logger.
//
// The function arguments are similar to fmt.Sprintln and fmt.Println. The only
// argument is a vardict of interfaces that can be used to output a string value.
//
// This function is affected by the setting of 'SetPrintLevel'. By default,
// this will print as an 'Info' logging message.
func (m Multi) Println(v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Print, 1, "", v...)
} else {
m[i].Println(v...)
}
}
}
// Panicln writes a panic message to the logger.
//
// This function will result in the program exiting with a Go 'panic()' after
// being called. The function arguments are similar to 'fmt.Sprintln' and
// 'fmt.Println'. The only argument is a vardict of interfaces that
// can be used to output a string value.
func (m Multi) Panicln(v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Panic, 1, "", v...)
} else {
// NOTE(dij): Write as Error here to prevent the non-flexable logger
// from exiting the program before all logs can be written.
m[i].Error("", v...)
}
}
panic(fmt.Sprint(v...))
}
// Info writes n informational message to the logger.
//
// The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The
// first argument is a string that can contain formatting characters. The second
// argument is a vardict of interfaces that can be omitted or used in the supplied
// format string.
func (m Multi) Info(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Info, 1, s, v...)
} else {
m[i].Info(s, v...)
}
}
}
// Error writes an error message to the logger.
//
// The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The
// first argument is a string that can contain formatting characters. The second
// argument is a vardict of interfaces that can be omitted or used in the supplied
// format string.
func (m Multi) Error(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Error, 1, s, v...)
} else {
m[i].Error(s, v...)
}
}
}
// Fatal writes a fatal message to the logger.
//
// This function will result in the program exiting with a non-zero error code
// after being called, unless the 'logx.FatalExits' setting is 'false'. The
// function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The first
// argument is a string that can contain formatting characters. The second argument
// is a vardict of interfaces that can be omitted or used in the supplied format
// string.
func (m Multi) Fatal(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Fatal, 1, s, v...)
} else {
// NOTE(dij): Write as Error here to prevent the non-flexable logger
// from exiting the program before all logs can be written.
m[i].Error(s, v...)
}
}
if FatalExits {
os.Exit(1)
}
}
// Trace writes a tracing message to the logger.
//
// The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The
// first argument is a string that can contain formatting characters. The second
// argument is a vardict of interfaces that can be omitted or used in the supplied
// format string.
func (m Multi) Trace(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Trace, 1, s, v...)
} else {
m[i].Trace(s, v...)
}
}
}
// Debug writes a debugging message to the logger.
//
// The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The
// first argument is a string that can contain formatting characters. The second
// argument is a vardict of interfaces that can be omitted or used in the supplied
// format string.
func (m Multi) Debug(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Debug, 1, s, v...)
} else {
m[i].Debug(s, v...)
}
}
}
// Printf writes a message to the logger.
//
// The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The
// first argument is a string that can contain formatting characters. The second
// argument is a vardict of interfaces that can be omitted or used in the supplied
// format string.
//
// This function is affected by the setting of 'SetPrintLevel'. By default,
// this will print as an 'Info' logging message.
func (m Multi) Printf(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Print, 1, s, v...)
} else {
m[i].Printf(s, v...)
}
}
}
// Panicf writes a panic message to the logger.
//
// This function will result in the program exiting with a Go 'panic()' after
// being called. The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'.
// The first argument is a string that can contain formatting characters. The
// second argument is a vardict of interfaces that can be omitted or used in
// the supplied format string.
func (m Multi) Panicf(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Panic, 1, s, v...)
} else {
// NOTE(dij): Write as Error here to prevent the non-flexable logger
// from exiting the program before all logs can be written.
m[i].Error(s, v...)
}
}
panic(fmt.Sprintf(s, v...))
}
// Warning writes a warning message to the logger.
//
// The function arguments are similar to 'fmt.Sprintf' and 'fmt.Printf'. The
// first argument is a string that can contain formatting characters. The second
// argument is a vardict of interfaces that can be omitted or used in the supplied
// format string.
func (m Multi) Warning(s string, v ...interface{}) {
for i := range m {
if x, ok := m[i].(LogWriter); ok {
x.Log(Warning, 1, s, v...)
} else {
m[i].Warning(s, v...)
}
}
}