LoggingEventBuilder Class

  • java.lang.Object
    • com.azure.core.util.logging.LoggingEventBuilder

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
LoggingEventBuilder addKeyValue(String key, boolean value)

Adds key with boolean value to the context of current log being created.

LoggingEventBuilder addKeyValue(String key, Object value)

Adds key with Object value to the context of current log being created.

LoggingEventBuilder addKeyValue(String key, String value)

Adds key with String value pair to the context of current log being created.

LoggingEventBuilder addKeyValue(String key, Supplier<String> valueSupplier)

Adds key with String value supplier to the context of current log event being created.

LoggingEventBuilder addKeyValue(String key, long value)

Adds key with long value to the context of current log event being created.

RuntimeException 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 {} as the placeholder at warning log level.

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:

key - String key.
value - boolean value.

Returns:

The updated 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:

key - String key.
value - Object value.

Returns:

The updated 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:

key - String key.
value - String value.

Returns:

The updated LoggingEventBuilder object.

addKeyValue

public LoggingEventBuilder addKeyValue(String key, Supplier 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 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:

key - String key.
value - long value.

Returns:

The updated 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:

runtimeException - RuntimeException to be logged and returned.

Returns:

The passed RuntimeException.

log

public void log(String message)

Logs message annotated with context.

Parameters:

message - the message to log.

log

public void log(String format, Object[] args)

Logs a format-able message that uses {} as the placeholder at warning 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 the Throwable.

log

public Throwable log(Throwable throwable)

Logs the Throwable and returns it to be thrown.

Parameters:

throwable - Throwable to be logged and returned.

Returns:

The passed Throwable.

log

public void log(Supplier messageSupplier)

Logs message annotated with context.

Parameters:

messageSupplier - string message supplier.

log

public void log(Supplier messageSupplier, Throwable throwable)

Logs message annotated with context.

Parameters:

messageSupplier - string message supplier.
throwable - Throwable for the message.

Applies to