This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathLogger.groovy
433 lines (392 loc) · 10.4 KB
/
Logger.groovy
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*-
* #%L
* wcm.io
* %%
* Copyright (C) 2017 wcm.io DevOps
* %%
* 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.
* #L%
*/
package io.wcm.devops.jenkins.pipeline.utils.logging
import com.cloudbees.groovy.cps.NonCPS
import io.wcm.devops.jenkins.pipeline.utils.ConfigConstants
import org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException
import org.jenkinsci.plugins.workflow.cps.DSL
/**
* Logging functionality for pipeline scripts.
*/
class Logger implements Serializable {
private static final long serialVersionUID = 1L
/**
* Reference to the dsl/script object
*/
static DSL dsl
/**
* Reference to the CpsScript/WorkflowScript
*/
static Script script
/**
* The log level
*/
static LogLevel level = LogLevel.TRACE
/**
* The name of the logger
*/
public String name = ""
/**
* Flag if the logger is initialized
*/
public static Boolean initialized = false
/**
* @param name The name of the logger
*/
Logger(String name = "") {
this.name = name
}
/**
* @param logScope The object the logger is for. The name of the logger is autodetected.
*/
Logger(Object logScope) {
if (logScope instanceof Object) {
this.name = getClassName(logScope)
if (this.name == null) {
this.name = "$logScope"
}
}
}
/**
* Initializes the logger with DSL/steps object and LogLevel
*
* @param dsl The DSL object of the current pipeline script (available via this.steps in pipeline scripts)
* @param logLvl The log level to use during execution of the pipeline script
* @deprecated still used by tests
*/
@NonCPS
static void init(DSL dsl, LogLevel logLvl = LogLevel.INFO) {
if (logLvl == null) logLvl = LogLevel.INFO
level = logLvl
if (Logger.initialized == true) {
return
}
this.dsl = dsl
initialized = true
Logger tmpLogger = new Logger('Logger')
tmpLogger.deprecated('Logger.init(DSL dsl, logLevel)','Logger.init(Script script, logLevel)')
}
/**
* Initializes the logger with CpsScript object and LogLevel
*
* @param script CpsScript object of the current pipeline script (available via this in pipeline scripts)
* @param map The configuration object of the pipeline
*/
@NonCPS
static void init(Script script, LogLevel logLvl = LogLevel.INFO) {
if (logLvl == null) logLvl = LogLevel.INFO
level = logLvl
if (Logger.initialized == true) {
return
}
this.script = script
this.dsl = (DSL) script.steps
initialized = true
}
/**
* Initializes the logger with CpsScript object and configuration map
*
* @param script CpsScript object of the current pipeline script (available via this in pipeline scripts)
* @param map The configuration object of the pipeline
*/
@NonCPS
static void init(Script script, Map map) {
LogLevel lvl
if (map) {
lvl = map[ConfigConstants.LOGLEVEL] ?: LogLevel.INFO
} else {
lvl = LogLevel.INFO
}
init(script, lvl)
}
/**
* Initializes the logger with CpsScript object and loglevel as string
*
* @param script CpsScript object of the current pipeline script (available via this in pipeline scripts)
* @param sLevel the log level as string
*/
@NonCPS
static void init(Script script, String sLevel) {
if (sLevel == null) sLevel = LogLevel.INFO
init(script, LogLevel.fromString(sLevel))
}
/**
* Initializes the logger with DSL/steps object and loglevel as integer
*
* @param script CpsScript object of the current pipeline script (available via this in pipeline scripts)
* @param iLevel the log level as integer
*
*/
@NonCPS
static void init(Script script, Integer iLevel) {
if (iLevel == null) iLevel = LogLevel.INFO.getLevel()
init(script, LogLevel.fromInteger(iLevel))
}
/**
* Logs a trace message followed by object dump
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void trace(String message, Object object) {
log(LogLevel.TRACE, message, object)
}
/**
* Logs a info message followed by object dump
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void info(String message, Object object) {
log(LogLevel.INFO, message, object)
}
/**
* Logs a debug message followed by object dump
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void debug(String message, Object object) {
log(LogLevel.DEBUG, message, object)
}
/**
* Logs warn message followed by object dump
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void warn(String message, Object object) {
log(LogLevel.WARN, message, object)
}
/**
* Logs a error message followed by object dump
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void error(String message, Object object) {
log(LogLevel.ERROR, message, object)
}
/**
* Logs a fatal message followed by object dump
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void fatal(String message, Object object) {
log(LogLevel.FATAL, message, object)
}
/**
* Logs a trace message
*
* @param message The message to be logged
*/
@NonCPS
void trace(String message) {
log(LogLevel.TRACE, message)
}
/**
* Logs a trace message
*
* @param message The message to be logged
*/
@NonCPS
void info(String message) {
log(LogLevel.INFO, message)
}
/**
* Logs a debug message
*
* @param message The message to be logged
*/
@NonCPS
void debug(String message) {
log(LogLevel.DEBUG, message)
}
/**
* Logs a warn message
*
* @param message The message to be logged
*/
@NonCPS
void warn(String message) {
log(LogLevel.WARN, message)
}
/**
* Logs a error message
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void error(String message) {
log(LogLevel.ERROR, message)
}
/**
* Logs a deprecation message
*
* @param message The message to be logged
*/
@NonCPS
void deprecated(String message) {
try {
Logger.dsl.addWarningBadge(message)
} catch (Throwable ex) {
// no badge plugin available
}
log(LogLevel.DEPRECATED, message)
}
/**
* Logs a deprecation message with deprecated and replacement
*
* @param deprecatedItem The item that is depcrecated
* @param newItem The replacement (if exist)
*/
@NonCPS
void deprecated(String deprecatedItem, String newItem) {
String message = "The step/function/class '$deprecatedItem' is marked as deprecated and will be removed in future releases. " +
"Please use '$newItem' instead."
deprecated(message)
}
/**
* Logs a fatal message
*
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void fatal(String message) {
log(LogLevel.FATAL, message)
}
/**
* Helper function for logging/dumping a object at the given log level
*
* @param logLevel the loglevel to be used
* @param message The message to be logged
* @param object The object to be dumped
*/
@NonCPS
void log(LogLevel logLevel, String message, Object object) {
if (doLog(logLevel)) {
def objectName = getClassName(object)
if (objectName != null) {
objectName = "($objectName) "
} else {
objectName = ""
}
def objectString = object.toString()
String msg = "$name : $message -> $objectName$objectString"
writeLogMsg(logLevel, msg)
}
}
/**
* Helper function for logging at the given log level
*
* @param logLevel the loglevel to be used
* @param message The message to be logged
*/
@NonCPS
void log(LogLevel logLevel, String message) {
if (doLog(logLevel)) {
String msg = "$name : $message"
writeLogMsg(logLevel, msg)
}
}
/**
* Utility function for writing to the jenkins console
*
* @param logLevel the loglevel to be used
* @param msg The message to be logged
*/
@NonCPS
private static void writeLogMsg(LogLevel logLevel, String msg) {
String lvlString = "[${logLevel.toString()}]"
lvlString = wrapColor(logLevel.getColorCode(), lvlString)
if (dsl != null) {
dsl.echo("$lvlString $msg")
}
}
/**
* Wraps a string with color codes when terminal is available
* @param logLevel
* @param str
* @return
*/
@NonCPS
private static String wrapColor(String colorCode, String str) {
String ret = str
if (hasTermEnv()) {
ret = "\u001B[${colorCode}m${str}\u001B[0m"
}
return ret
}
/**
* Helper function to detect if a term environment is available
* @return
*/
@NonCPS
private static Boolean hasTermEnv() {
String termEnv = null
if (script != null) {
try {
termEnv = script.env.TERM
} catch (Exception ex) {
}
}
return termEnv != null
}
/**
* Utiltiy function to determine if the given logLevel is active
*
* @param logLevel
* @return true , when the loglevel should be displayed, false when the loglevel is disabled
*/
@NonCPS
private static boolean doLog(LogLevel logLevel) {
if (logLevel.getLevel() >= level.getLevel()) {
return true
}
return false
}
/**
* Helper function to get the name of the object
* @param object
* @return
*/
@NonCPS
private static String getClassName(Object object) {
String objectName = null
// try to retrieve as much information as possible about the class
try {
Class objectClass = object.getClass()
objectName = objectClass.getName().toString()
objectName = objectClass.getCanonicalName().toString()
} catch (RejectedAccessException e) {
// do nothing
}
return objectName
}
}