Package com.azure.core.util.logging
Class LoggingEventBuilder
java.lang.Object
com.azure.core.util.logging.LoggingEventBuilder
This class provides fluent API to write logs using
ClientLogger
and
enrich them with additional context.
Code samples
Logging event with context.
logger.atInfo() .addKeyValue("key1", "value1") .addKeyValue("key2", true) .addKeyValue("key3", () -> getName()) .log("A formattable message. Hello, {}", name);
-
Method Summary
Modifier and TypeMethodDescriptionaddKeyValue
(String key, boolean value) Adds key with boolean value to the context of current log being created.addKeyValue
(String key, long value) Adds key with long value to the context of current log event being created.addKeyValue
(String key, Object value) Adds key with Object value to the context of current log being created.addKeyValue
(String key, String value) Adds key with String value pair to the context of current log being created.addKeyValue
(String key, Supplier<String> valueSupplier) Adds key with String value supplier to the context of current log event being created.log
(RuntimeException runtimeException) Logs theRuntimeException
and returns it to be thrown.void
Logs message annotated with context.void
Logs a format-able message that uses{}
as the placeholder atwarning
log level.Logs theThrowable
and returns it to be thrown.void
Logs message annotated with context.void
Logs message annotated with context.
-
Method Details
-
addKeyValue
Adds key with String value pair to the context of current log being created.Code samples
Adding string value to logging event context.
logger.atInfo() .addKeyValue("key", "value") .log("A formattable message. Hello, {}", name);
- Parameters:
key
- String key.value
- String value.- Returns:
- The updated
LoggingEventBuilder
object.
-
addKeyValue
Adds key with Object value to the context of current log being created. If logging is enabled at given level, and object is not null, usesvalue.toString()
to serialize object.Code samples
Adding string value to logging event context.
logger.atVerbose() // equivalent to addKeyValue("key", () -> new LoggableObject("string representation").toString() .addKeyValue("key", new LoggableObject("string representation")) .log("Param 1: {}, Param 2: {}, Param 3: {}", "param1", "param2", "param3");
- Parameters:
key
- String key.value
- Object value.- Returns:
- The updated
LoggingEventBuilder
object.
-
addKeyValue
Adds key with boolean value to the context of current log being created.- Parameters:
key
- String key.value
- boolean value.- Returns:
- The updated
LoggingEventBuilder
object.
-
addKeyValue
Adds key with long value to the context of current log event being created.Code samples
Adding an integer value to logging event context.
logger.atVerbose() .addKeyValue("key", 1L) .log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
- Parameters:
key
- String key.value
- long value.- Returns:
- The updated
LoggingEventBuilder
object.
-
addKeyValue
Adds key with String value supplier to the context of current log event being created.- Parameters:
key
- String key.valueSupplier
- String value supplier function.- Returns:
- The updated
LoggingEventBuilder
object.
-
log
Logs message annotated with context.- Parameters:
message
- the message to log.
-
log
Logs message annotated with context.- Parameters:
messageSupplier
- string message supplier.
-
log
Logs message annotated with context.- Parameters:
messageSupplier
- string message supplier.throwable
-Throwable
for the message.
-
log
Logs a format-able message that uses{}
as the placeholder atwarning
log level.- Parameters:
format
- The format-able message to log.args
- Arguments for the message. If an exception is being logged, the last argument should be theThrowable
.
-
log
Logs theThrowable
and returns it to be thrown.- Parameters:
throwable
- Throwable to be logged and returned.- Returns:
- The passed
Throwable
. - Throws:
NullPointerException
- Ifthrowable
isnull
.
-
log
Logs theRuntimeException
and returns it to be thrown. This API covers the cases where a checked exception type needs to be thrown and logged.- Parameters:
runtimeException
- RuntimeException to be logged and returned.- Returns:
- The passed
RuntimeException
. - Throws:
NullPointerException
- IfruntimeException
isnull
.
-