LoggingEventBuilder Class
- java.
lang. Object - com.
azure. core. util. logging. LoggingEventBuilder
- com.
public final class 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 Type | Method and Description |
---|---|
Logging |
addKeyValue(String key, boolean value)
Adds key with boolean value to the context of current log being created. |
Logging |
addKeyValue(String key, Object value)
Adds key with Object value to the context of current log being created. |
Logging |
addKeyValue(String key, String value)
Adds key with String value pair to the context of current log being created. |
Logging |
addKeyValue(String key, Supplier<String> valueSupplier)
Adds key with String value supplier to the context of current log event being created. |
Logging |
addKeyValue(String key, long value)
Adds key with long value to the context of current log event being created. |
Runtime |
log(RuntimeException runtimeException)
Logs the RuntimeException and returns it to be thrown. |
void |
log(String message)
Logs message annotated with context. |
void |
log(String format, Object[] args)
Logs a format-able message that uses |
Throwable |
log(Throwable throwable)
Logs the Throwable and returns it to be thrown. |
void |
log(Supplier<String> messageSupplier)
Logs message annotated with context. |
void |
log(Supplier<String> messageSupplier, Throwable throwable)
Logs message annotated with context. |
Methods inherited from java.lang.Object
Method Details
addKeyValue
public LoggingEventBuilder addKeyValue(String key, boolean value)
Adds key with boolean value to the context of current log being created.
Parameters:
Returns:
LoggingEventBuilder
object.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, uses value.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:
Returns:
LoggingEventBuilder
object.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:
Returns:
LoggingEventBuilder
object.addKeyValue
public LoggingEventBuilder addKeyValue(String key, Supplier
Adds key with String value supplier to the context of current log event being created.
Parameters:
Returns:
LoggingEventBuilder
object.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:
Returns:
LoggingEventBuilder
object.log
public RuntimeException log(RuntimeException runtimeException)
Logs the RuntimeException and returns it to be thrown. This API covers the cases where a checked exception type needs to be thrown and logged.
Parameters:
Returns:
log
public void log(String message)
Logs message annotated with context.
Parameters:
log
public void log(String format, Object[] args)
Logs a format-able message that uses {}
as the placeholder at warning
log level.
Parameters:
log
public Throwable log(Throwable throwable)
Logs the Throwable and returns it to be thrown.
Parameters:
Returns:
log
public void log(Supplier
Logs message annotated with context.
Parameters:
log
public void log(Supplier
Logs message annotated with context.
Parameters:
Applies to
Azure SDK for Java