Package com.azure.core.util.logging
Class LoggingEventBuilder
- java.lang.Object
-
- com.azure.core.util.logging.LoggingEventBuilder
-
public final class LoggingEventBuilder extends Object
This class provides fluent API to write logs usingClientLoggerand 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description LoggingEventBuilderaddKeyValue(String key, boolean value)Adds key with boolean value to the context of current log being created.LoggingEventBuilderaddKeyValue(String key, long value)Adds key with long value to the context of current log event being created.LoggingEventBuilderaddKeyValue(String key, Object value)Adds key with Object value to the context of current log being created.LoggingEventBuilderaddKeyValue(String key, String value)Adds key with String value pair to the context of current log being created.LoggingEventBuilderaddKeyValue(String key, Supplier<String> valueSupplier)Adds key with String value supplier to the context of current log event being created.RuntimeExceptionlog(RuntimeException runtimeException)Logs theRuntimeExceptionand returns it to be thrown.voidlog(String message)Logs message annotated with context.voidlog(String format, Object... args)Logs a format-able message that uses{}as the placeholder atwarninglog level.Throwablelog(Throwable throwable)Logs theThrowableand returns it to be thrown.voidlog(Supplier<String> messageSupplier)Logs message annotated with context.voidlog(Supplier<String> messageSupplier, Throwable throwable)Logs message annotated with context.
-
-
-
Method Detail
-
addKeyValue
public LoggingEventBuilder addKeyValue(String key, String value)
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
LoggingEventBuilderobject.
-
addKeyValue
public LoggingEventBuilder addKeyValue(String key, Object value)
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
LoggingEventBuilderobject.
-
addKeyValue
public LoggingEventBuilder addKeyValue(String key, boolean value)
Adds key with boolean value to the context of current log being created.- Parameters:
key- String key.value- boolean value.- Returns:
- The updated
LoggingEventBuilderobject.
-
addKeyValue
public LoggingEventBuilder addKeyValue(String key, long value)
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
LoggingEventBuilderobject.
-
addKeyValue
public LoggingEventBuilder addKeyValue(String key, Supplier<String> valueSupplier)
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
LoggingEventBuilderobject.
-
log
public void log(String message)
Logs message annotated with context.- Parameters:
message- the message to log.
-
log
public void log(Supplier<String> messageSupplier)
Logs message annotated with context.- Parameters:
messageSupplier- string message supplier.
-
log
public void log(Supplier<String> messageSupplier, Throwable throwable)
Logs message annotated with context.- Parameters:
messageSupplier- string message supplier.throwable-Throwablefor the message.
-
log
public void log(String format, Object... args)
Logs a format-able message that uses{}as the placeholder atwarninglog 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
public Throwable log(Throwable throwable)
Logs theThrowableand returns it to be thrown.- Parameters:
throwable- Throwable to be logged and returned.- Returns:
- The passed
Throwable. - Throws:
NullPointerException- Ifthrowableisnull.
-
log
public RuntimeException log(RuntimeException runtimeException)
Logs theRuntimeExceptionand 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- IfruntimeExceptionisnull.
-
-