/
ClientLogger.java
718 lines (670 loc) · 28.1 KB
/
ClientLogger.java
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.util.logging;
import com.azure.core.implementation.logging.DefaultLogger;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLogger;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import static com.azure.core.implementation.logging.LoggingUtils.doesArgsHaveThrowable;
import static com.azure.core.implementation.logging.LoggingUtils.removeNewLinesFromLogMessage;
import static com.azure.core.implementation.logging.LoggingUtils.removeThrowable;
/**
* This is a fluent logger helper class that wraps a pluggable {@link Logger}.
*
* <p>This logger logs format-able messages that use {@code {}} as the placeholder. When a {@link Throwable throwable}
* is the last argument of the format varargs and the logger is enabled for {@link ClientLogger#verbose(String,
* Object...) verbose}, the stack trace for the throwable is logged.</p>
*
* <p>A minimum logging level threshold is determined by the
* {@link Configuration#PROPERTY_AZURE_LOG_LEVEL AZURE_LOG_LEVEL} environment configuration. By default logging is
* <b>disabled</b>.</p>
*
* <p><strong>Log level hierarchy</strong></p>
* <ol>
* <li>{@link ClientLogger#error(String, Object...) Error}</li>
* <li>{@link ClientLogger#warning(String, Object...) Warning}</li>
* <li>{@link ClientLogger#info(String, Object...) Info}</li>
* <li>{@link ClientLogger#verbose(String, Object...) Verbose}</li>
* </ol>
*
* <p>The logger is capable of producing json-formatted messages enriched with key value pairs.
* Context can be provided in the constructor and populated on every message or added per each log record.</p>
* @see Configuration
*/
public class ClientLogger {
private final Logger logger;
private final String globalContextSerialized;
private final boolean hasGlobalContext;
/**
* Retrieves a logger for the passed class using the {@link LoggerFactory}.
*
* @param clazz Class creating the logger.
*/
public ClientLogger(Class<?> clazz) {
this(clazz.getName());
}
/**
* Retrieves a logger for the passed class name using the {@link LoggerFactory}.
*
* @param className Class name creating the logger.
* @throws RuntimeException when logging configuration is invalid depending on SLF4J implementation.
*/
public ClientLogger(String className) {
this(className, Collections.emptyMap());
}
/**
* Retrieves a logger for the passed class using the {@link LoggerFactory}.
*
* @param clazz Class creating the logger.
* @param context Context to be populated on every log record written with this logger.
* Objects are serialized with {@code toString()} method.
*/
public ClientLogger(Class<?> clazz, Map<String, Object> context) {
this(clazz.getName(), context);
}
/**
* Retrieves a logger for the passed class name using the {@link LoggerFactory} with
* context that will be populated on all log records produced with this logger.
*
* <!-- src_embed com.azure.core.util.logging.clientlogger#globalcontext -->
* <pre>
* Map<String, Object> context = new HashMap<>();
* context.put("connectionId", "95a47cf");
*
* ClientLogger loggerWithContext = new ClientLogger(ClientLoggerJavaDocCodeSnippets.class, context);
* loggerWithContext.info("A formattable message. Hello, {}", name);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger#globalcontext -->
*
* @param className Class name creating the logger.
* @param context Context to be populated on every log record written with this logger.
* Objects are serialized with {@code toString()} method.
* @throws RuntimeException when logging configuration is invalid depending on SLF4J implementation.
*/
public ClientLogger(String className, Map<String, Object> context) {
Logger initLogger = LoggerFactory.getLogger(className);
logger = initLogger instanceof NOPLogger ? new DefaultLogger(className) : initLogger;
globalContextSerialized = LoggingEventBuilder.writeJsonFragment(context);
hasGlobalContext = !CoreUtils.isNullOrEmpty(globalContextSerialized);
}
/**
* Logs a format-able message that uses {@code {}} as the placeholder at the given {@code logLevel}.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging with a specific log level</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.log -->
* <pre>
* logger.log(LogLevel.VERBOSE,
* () -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.log -->
*
* @param logLevel Logging level for the log message.
* @param message The format-able message to log.
*/
public void log(LogLevel logLevel, Supplier<String> message) {
log(logLevel, message, null);
}
/**
* Logs a format-able message that uses {@code {}} as the placeholder at {@code verbose} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging with a specific log level and exception</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.log#throwable -->
* <pre>
* Throwable illegalArgumentException = new IllegalArgumentException("An invalid argument was encountered.");
* logger.log(LogLevel.VERBOSE,
* () -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"),
* illegalArgumentException);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.log#throwable -->
*
* @param logLevel Logging level for the log message.
* @param message The format-able message to log.
* @param throwable Throwable for the message. {@link Throwable}.
*/
public void log(LogLevel logLevel, Supplier<String> message, Throwable throwable) {
if (message != null && canLogAtLevel(logLevel)) {
performDeferredLogging(logLevel, message, throwable);
}
}
/**
* Logs a message at {@code verbose} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at verbose log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.verbose -->
* <pre>
* logger.verbose("A log message");
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.verbose -->
*
* @param message The message to log.
*/
public void verbose(String message) {
if (logger.isDebugEnabled()) {
if (hasGlobalContext) {
atVerbose().log(message);
} else {
logger.debug(removeNewLinesFromLogMessage(message));
}
}
}
/**
* Logs a format-able message that uses {@code {}} as the placeholder at {@code verbose} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at verbose log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.verbose#string-object -->
* <pre>
* logger.verbose("A formattable message. Hello, {}", name);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.verbose#string-object -->
*
* @param format The formattable message to log.
* @param args Arguments for the message. If an exception is being logged, the last argument should be the {@link
* Throwable}.
*/
public void verbose(String format, Object... args) {
if (logger.isDebugEnabled()) {
performLogging(LogLevel.VERBOSE, false, format, args);
}
}
/**
* Logs a message at {@code info} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at verbose log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.info -->
* <pre>
* logger.info("A log message");
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.info -->
*
* @param message The message to log.
*/
public void info(String message) {
if (logger.isInfoEnabled()) {
if (hasGlobalContext) {
atInfo().log(message);
} else {
logger.info(removeNewLinesFromLogMessage(message));
}
}
}
/**
* Logs a format-able message that uses {@code {}} as the placeholder at {@code informational} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at informational log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.info#string-object -->
* <pre>
* logger.info("A formattable message. Hello, {}", name);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.info#string-object -->
*
* @param format The format-able message to log
* @param args Arguments for the message. If an exception is being logged, the last argument should be the {@link
* Throwable}.
*/
public void info(String format, Object... args) {
if (logger.isInfoEnabled()) {
performLogging(LogLevel.INFORMATIONAL, false, format, args);
}
}
/**
* Logs a message at {@code warning} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at warning log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.warning -->
* <pre>
* Throwable detailedException = new IllegalArgumentException("A exception with a detailed message");
* logger.warning(detailedException.getMessage());
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.warning -->
*
* @param message The message to log.
*/
public void warning(String message) {
if (logger.isWarnEnabled()) {
if (hasGlobalContext) {
atWarning().log(message);
} else {
logger.warn(removeNewLinesFromLogMessage(message));
}
}
}
/**
* Logs a format-able message that uses {@code {}} as the placeholder at {@code warning} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at warning log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.warning#string-object -->
* <pre>
* Throwable exception = new IllegalArgumentException("An invalid argument was encountered.");
* logger.warning("A formattable message. Hello, {}", name, exception);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.warning#string-object -->
*
* @param format The format-able message to log.
* @param args Arguments for the message. If an exception is being logged, the last argument should be the {@link
* Throwable}.
*/
public void warning(String format, Object... args) {
if (logger.isWarnEnabled()) {
performLogging(LogLevel.WARNING, false, format, args);
}
}
/**
* Logs a message at {@code error} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging a message at error log level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.error -->
* <pre>
* try {
* upload(resource);
* } catch (IOException ex) {
* logger.error(ex.getMessage());
* }
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.error -->
*
* @param message The message to log.
*/
public void error(String message) {
if (logger.isErrorEnabled()) {
if (hasGlobalContext) {
atError().log(message);
} else {
logger.error(removeNewLinesFromLogMessage(message));
}
}
}
/**
* Logs a format-able message that uses {@code {}} as the placeholder at {@code error} log level.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging an error with stack trace.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.error#string-object -->
* <pre>
* try {
* upload(resource);
* } catch (IOException ex) {
* logger.error("A formattable message. Hello, {}", name, ex);
* }
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.error#string-object -->
*
* @param format The format-able message to log.
* @param args Arguments for the message. If an exception is being logged, the last argument should be the {@link
* Throwable}.
*/
public void error(String format, Object... args) {
if (logger.isErrorEnabled()) {
performLogging(LogLevel.ERROR, false, format, args);
}
}
/**
* Logs the {@link RuntimeException} at the warning level and returns it to be thrown.
* <p>
* This API covers the cases where a runtime exception type needs to be thrown and logged. If a {@link Throwable} is
* being logged use {@link #logThrowableAsWarning(Throwable)} instead.
*
* @param runtimeException RuntimeException to be logged and returned.
* @return The passed {@link RuntimeException}.
* @throws NullPointerException If {@code runtimeException} is {@code null}.
*/
public RuntimeException logExceptionAsWarning(RuntimeException runtimeException) {
Objects.requireNonNull(runtimeException, "'runtimeException' cannot be null.");
return logThrowableAsWarning(runtimeException);
}
/**
* Logs the {@link Throwable} at the warning level and returns it to be thrown.
* <p>
* This API covers the cases where a checked exception type needs to be thrown and logged. If a {@link
* RuntimeException} is being logged use {@link #logExceptionAsWarning(RuntimeException)} instead.
*
* @param throwable Throwable to be logged and returned.
* @param <T> Type of the Throwable being logged.
* @return The passed {@link Throwable}.
* @throws NullPointerException If {@code throwable} is {@code null}.
* @deprecated Use {@link #logThrowableAsWarning(Throwable)} instead.
*/
@Deprecated
public <T extends Throwable> T logThowableAsWarning(T throwable) {
Objects.requireNonNull(throwable, "'throwable' cannot be null.");
if (!logger.isWarnEnabled()) {
return throwable;
}
performLogging(LogLevel.WARNING, true, throwable.getMessage(), throwable);
return throwable;
}
/**
* Logs the {@link Throwable} at the warning level and returns it to be thrown.
* <p>
* This API covers the cases where a checked exception type needs to be thrown and logged. If a {@link
* RuntimeException} is being logged use {@link #logExceptionAsWarning(RuntimeException)} instead.
*
* @param throwable Throwable to be logged and returned.
* @param <T> Type of the Throwable being logged.
* @return The passed {@link Throwable}.
* @throws NullPointerException If {@code throwable} is {@code null}.
*/
public <T extends Throwable> T logThrowableAsWarning(T throwable) {
Objects.requireNonNull(throwable, "'throwable' cannot be null.");
if (logger.isWarnEnabled()) {
performLogging(LogLevel.WARNING, true, throwable.getMessage(), throwable);
}
return throwable;
}
/**
* Logs the {@link RuntimeException} at the error level and returns it to be thrown.
* <p>
* This API covers the cases where a runtime exception type needs to be thrown and logged. If a {@link Throwable} is
* being logged use {@link #logThrowableAsError(Throwable)} instead.
*
* @param runtimeException RuntimeException to be logged and returned.
* @return The passed {@code RuntimeException}.
* @throws NullPointerException If {@code runtimeException} is {@code null}.
*/
public RuntimeException logExceptionAsError(RuntimeException runtimeException) {
Objects.requireNonNull(runtimeException, "'runtimeException' cannot be null.");
return logThrowableAsError(runtimeException);
}
/**
* Logs the {@link Throwable} at the error level and returns it to be thrown.
* <p>
* This API covers the cases where a checked exception type needs to be thrown and logged. If a {@link
* RuntimeException} is being logged use {@link #logExceptionAsError(RuntimeException)} instead.
*
* @param throwable Throwable to be logged and returned.
* @param <T> Type of the Throwable being logged.
* @return The passed {@link Throwable}.
* @throws NullPointerException If {@code throwable} is {@code null}.
*/
public <T extends Throwable> T logThrowableAsError(T throwable) {
Objects.requireNonNull(throwable, "'throwable' cannot be null.");
if (!logger.isErrorEnabled()) {
return throwable;
}
performLogging(LogLevel.ERROR, true, throwable.getMessage(), throwable);
return throwable;
}
/*
* Performs the logging. Call only if logging at this level is enabled.
*
* @param format format-able message.
* @param args Arguments for the message, if an exception is being logged last argument is the throwable.
*/
private void performLogging(LogLevel logLevel, boolean isExceptionLogging, String format, Object... args) {
if (hasGlobalContext) {
LoggingEventBuilder.create(logger, logLevel, globalContextSerialized, true)
.log(format, args);
return;
}
// If the logging level is less granular than verbose remove the potential throwable from the args.
String throwableMessage = "";
if (doesArgsHaveThrowable(args)) {
// If we are logging an exception the format string is already the exception message, don't append it.
if (!isExceptionLogging) {
Object throwable = args[args.length - 1];
// This is true from before but is needed to appease SpotBugs.
if (throwable instanceof Throwable) {
throwableMessage = ((Throwable) throwable).getMessage();
}
}
/*
* Environment is logging at a level higher than verbose, strip out the throwable as it would log its
* stack trace which is only expected when logging at a verbose level.
*/
if (!logger.isDebugEnabled()) {
args = removeThrowable(args);
}
}
format = removeNewLinesFromLogMessage(format);
switch (logLevel) {
case VERBOSE:
logger.debug(format, args);
break;
case INFORMATIONAL:
logger.info(format, args);
break;
case WARNING:
if (!CoreUtils.isNullOrEmpty(throwableMessage)) {
format += System.lineSeparator() + throwableMessage;
}
logger.warn(format, args);
break;
case ERROR:
if (!CoreUtils.isNullOrEmpty(throwableMessage)) {
format += System.lineSeparator() + throwableMessage;
}
logger.error(format, args);
break;
default:
// Don't do anything, this state shouldn't be possible.
break;
}
}
/*
* Performs deferred logging. Call only if logging at this level is enabled.
*
* @param logLevel sets the logging level
* @param args Arguments for the message, if an exception is being logged last argument is the throwable.
*/
private void performDeferredLogging(LogLevel logLevel, Supplier<String> messageSupplier, Throwable throwable) {
if (hasGlobalContext) {
// LoggingEventBuilder writes log messages as json and performs all necessary escaping, i.e. no
// sanitization needed
LoggingEventBuilder.create(logger, logLevel, globalContextSerialized, true)
.log(messageSupplier, throwable);
return;
}
String message = removeNewLinesFromLogMessage(messageSupplier.get());
String throwableMessage = (throwable != null) ? throwable.getMessage() : "";
switch (logLevel) {
case VERBOSE:
if (throwable != null) {
logger.debug(message, throwable);
} else {
logger.debug(message);
}
break;
case INFORMATIONAL:
logger.info(message);
break;
case WARNING:
if (!CoreUtils.isNullOrEmpty(throwableMessage)) {
message += System.lineSeparator() + throwableMessage;
}
logger.warn(message);
break;
case ERROR:
if (!CoreUtils.isNullOrEmpty(throwableMessage)) {
message += System.lineSeparator() + throwableMessage;
}
logger.error(message);
break;
default:
// Don't do anything, this state shouldn't be possible.
break;
}
}
/*
* @param args The arguments passed to evaluate suppliers in args.
* @return Return the argument with evaluated supplier
*/
Object[] evaluateSupplierArgument(Object[] args) {
if (isSupplierLogging(args)) {
args[0] = ((Supplier<?>) args[0]).get();
}
return args;
}
/*
* @param args The arguments passed to determine supplier evaluation
* @return Determines if it is supplier logging
*/
boolean isSupplierLogging(Object[] args) {
return (args.length == 1 && args[0] instanceof Supplier)
|| (args.length == 2 && args[0] instanceof Supplier && (args[1] instanceof Throwable || args[1] == null));
}
/**
* Determines if the app or environment logger support logging at the given log level.
*
* @param logLevel Logging level for the log message.
* @return Flag indicating if the environment and logger are configured to support logging at the given log level.
*/
public boolean canLogAtLevel(LogLevel logLevel) {
if (logLevel == null) {
return false;
}
switch (logLevel) {
case VERBOSE:
return logger.isDebugEnabled();
case INFORMATIONAL:
return logger.isInfoEnabled();
case WARNING:
return logger.isWarnEnabled();
case ERROR:
return logger.isErrorEnabled();
default:
return false;
}
}
/**
* Creates {@link LoggingEventBuilder} for {@code error} log level that can be
* used to enrich log with additional context.
* <p><strong>Code samples</strong></p>
*
* <p>Logging with context at error level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive -->
* <pre>
* logger.atVerbose()
* .addKeyValue("key", 1L)
* .log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive -->
*
* @return instance of {@link LoggingEventBuilder} or no-op if error logging is disabled.
*/
public LoggingEventBuilder atError() {
return LoggingEventBuilder.create(logger, LogLevel.ERROR, globalContextSerialized, canLogAtLevel(LogLevel.ERROR));
}
/**
* Creates {@link LoggingEventBuilder} for {@code warning} log level that can be
* used to enrich log with additional context.
* <p><strong>Code samples</strong></p>
*
* <p>Logging with context at warning level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.atWarning -->
* <pre>
* logger.atWarning()
* .addKeyValue("key", "value")
* .log("A formattable message. Hello, {}", name, exception);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.atWarning -->
*
* @return instance of {@link LoggingEventBuilder} or no-op if warn logging is disabled.
*/
public LoggingEventBuilder atWarning() {
return LoggingEventBuilder.create(logger, LogLevel.WARNING, globalContextSerialized,
canLogAtLevel(LogLevel.WARNING));
}
/**
* Creates {@link LoggingEventBuilder} for {@code info} log level that can be
* used to enrich log with additional context.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging with context at info level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.atInfo -->
* <pre>
* logger.atInfo()
* .addKeyValue("key", "value")
* .log("A formattable message. Hello, {}", name);
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.atInfo -->
*
* @return instance of {@link LoggingEventBuilder} or no-op if info logging is disabled.
*/
public LoggingEventBuilder atInfo() {
return LoggingEventBuilder.create(logger, LogLevel.INFORMATIONAL, globalContextSerialized,
canLogAtLevel(LogLevel.INFORMATIONAL));
}
/**
* Creates {@link LoggingEventBuilder} for {@code verbose} log level that can be
* used to enrich log with additional context.
* <p><strong>Code samples</strong></p>
*
* <p>Logging with context at verbose level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive -->
* <pre>
* logger.atVerbose()
* .addKeyValue("key", 1L)
* .log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.atverbose.addKeyValue#primitive -->
*
* @return instance of {@link LoggingEventBuilder} or no-op if verbose logging is disabled.
*/
public LoggingEventBuilder atVerbose() {
return LoggingEventBuilder.create(logger, LogLevel.VERBOSE, globalContextSerialized,
canLogAtLevel(LogLevel.VERBOSE));
}
/**
* Creates {@link LoggingEventBuilder} for log level that can be
* used to enrich log with additional context.
*
* <p><strong>Code samples</strong></p>
*
* <p>Logging with context at provided level.</p>
*
* <!-- src_embed com.azure.core.util.logging.clientlogger.atLevel -->
* <pre>
* LogLevel level = response.getStatusCode() == 200 ? LogLevel.INFORMATIONAL : LogLevel.WARNING;
* logger.atLevel(level)
* .addKeyValue("key", "value")
* .log("message");
* </pre>
* <!-- end com.azure.core.util.logging.clientlogger.atLevel -->
*
* @param level log level.
* @return instance of {@link LoggingEventBuilder} or no-op if logging at provided level is disabled.
*/
public LoggingEventBuilder atLevel(LogLevel level) {
return LoggingEventBuilder.create(logger, level, globalContextSerialized,
canLogAtLevel(level));
}
}