ClientLogger Class
- java.
lang. Object - com.
azure. core. util. logging. ClientLogger
- com.
public class ClientLogger
This is a fluent logger helper class that wraps a pluggable Logger.
This logger logs format-able messages that use {}
as the placeholder. When a throwable is the last argument of the format varargs and the logger is enabled for verbose, the stack trace for the throwable is logged.
A minimum logging level threshold is determined by the PROPERTY_AZURE_LOG_LEVEL environment configuration. By default logging is disabled.
Log level hierarchy
- Error
- Warning
- Info
- Verbose
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.
Constructor Summary
Constructor | Description |
---|---|
ClientLogger(Class<?> clazz) |
Retrieves a logger for the passed class using the LoggerFactory. |
ClientLogger(Class<?> clazz, Map<String,Object> context) |
Retrieves a logger for the passed class using the LoggerFactory. |
ClientLogger(String className) |
Retrieves a logger for the passed class name using the LoggerFactory. |
ClientLogger(String className, Map<String,Object> context) |
Retrieves a logger for the passed class name using the LoggerFactory with context that will be populated on all log records produced with this logger. |
Method Summary
Modifier and Type | Method and Description |
---|---|
T |
logThowableAsWarning(T throwable)
Deprecated
Use #logThrowableAsWarning(Throwable) instead.
Logs the Throwable at the warning level and returns it to be thrown. |
T |
logThrowableAsError(T throwable)
Logs the Throwable at the error level and returns it to be thrown. |
T |
logThrowableAsWarning(T throwable)
Logs the Throwable at the warning level and returns it to be thrown. |
Logging |
atError()
Creates LoggingEventBuilder for |
Logging |
atInfo()
Creates LoggingEventBuilder for |
Logging |
atLevel(LogLevel level)
Creates LoggingEventBuilder for log level that can be used to enrich log with additional context. |
Logging |
atVerbose()
Creates LoggingEventBuilder for |
Logging |
atWarning()
Creates LoggingEventBuilder for |
boolean |
canLogAtLevel(LogLevel logLevel)
Determines if the app or environment logger support logging at the given log level. |
void |
error(String message)
Logs a message at |
void |
error(String format, Object[] args)
Logs a format-able message that uses |
void |
info(String message)
Logs a message at |
void |
info(String format, Object[] args)
Logs a format-able message that uses |
void |
log(LogLevel logLevel, Supplier<String> message)
Logs a format-able message that uses |
void |
log(LogLevel logLevel, Supplier<String> message, Throwable throwable)
Logs a format-able message that uses |
Runtime |
logExceptionAsError(RuntimeException runtimeException)
Logs the RuntimeException at the error level and returns it to be thrown. |
Runtime |
logExceptionAsWarning(RuntimeException runtimeException)
Logs the RuntimeException at the warning level and returns it to be thrown. |
void |
verbose(String message)
Logs a message at |
void |
verbose(String format, Object[] args)
Logs a format-able message that uses |
void |
warning(String message)
Logs a message at |
void |
warning(String format, Object[] args)
Logs a format-able message that uses |
Methods inherited from java.lang.Object
Constructor Details
ClientLogger
public ClientLogger(Class clazz)
Retrieves a logger for the passed class using the LoggerFactory.
Parameters:
ClientLogger
public ClientLogger(Class clazz, Map
Retrieves a logger for the passed class using the LoggerFactory.
Parameters:
toString()
method.
ClientLogger
public ClientLogger(String className)
Retrieves a logger for the passed class name using the LoggerFactory.
Parameters:
ClientLogger
public ClientLogger(String className, Map
Retrieves a logger for the passed class name using the LoggerFactory with context that will be populated on all log records produced with this logger.
Map<String, Object> context = new HashMap<>();
context.put("connectionId", "95a47cf");
ClientLogger loggerWithContext = new ClientLogger(ClientLoggerJavaDocCodeSnippets.class, context);
loggerWithContext.info("A formattable message. Hello, {}", name);
Parameters:
toString()
method.
Method Details
logThowableAsWarning
@Deprecated
public T
Deprecated
Logs the Throwable at the warning level and returns it to be thrown.
This API covers the cases where a checked exception type needs to be thrown and logged. If a RuntimeException is being logged use logExceptionAsWarning(RuntimeException runtimeException) instead.
Parameters:
Returns:
logThrowableAsError
public T
Logs the Throwable at the error level and returns it to be thrown.
This API covers the cases where a checked exception type needs to be thrown and logged. If a RuntimeException is being logged use logExceptionAsError(RuntimeException runtimeException) instead.
Parameters:
Returns:
logThrowableAsWarning
public T
Logs the Throwable at the warning level and returns it to be thrown.
This API covers the cases where a checked exception type needs to be thrown and logged. If a RuntimeException is being logged use logExceptionAsWarning(RuntimeException runtimeException) instead.
Parameters:
Returns:
atError
public LoggingEventBuilder atError()
Creates LoggingEventBuilder for error
log level that can be used to enrich log with additional context.
Code samples
Logging with context at error level.
logger.atVerbose()
.addKeyValue("key", 1L)
.log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
Returns:
atInfo
public LoggingEventBuilder atInfo()
Creates LoggingEventBuilder for info
log level that can be used to enrich log with additional context.
Code samples
Logging with context at info level.
logger.atInfo()
.addKeyValue("key", "value")
.log("A formattable message. Hello, {}", name);
Returns:
atLevel
public LoggingEventBuilder atLevel(LogLevel level)
Creates LoggingEventBuilder for log level that can be used to enrich log with additional context.
Code samples
Logging with context at provided level.
LogLevel level = response.getStatusCode() == 200 ? LogLevel.INFORMATIONAL : LogLevel.WARNING;
logger.atLevel(level)
.addKeyValue("key", "value")
.log("message");
Parameters:
Returns:
atVerbose
public LoggingEventBuilder atVerbose()
Creates LoggingEventBuilder for verbose
log level that can be used to enrich log with additional context.
Code samples
Logging with context at verbose level.
logger.atVerbose()
.addKeyValue("key", 1L)
.log(() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
Returns:
atWarning
public LoggingEventBuilder atWarning()
Creates LoggingEventBuilder for warning
log level that can be used to enrich log with additional context.
Code samples
Logging with context at warning level.
logger.atWarning()
.addKeyValue("key", "value")
.log("A formattable message. Hello, {}", name, exception);
Returns:
canLogAtLevel
public boolean canLogAtLevel(LogLevel logLevel)
Determines if the app or environment logger support logging at the given log level.
Parameters:
Returns:
error
public void error(String message)
Logs a message at error
log level.
Code samples
Logging a message at error log level.
try {
upload(resource);
} catch (IOException ex) {
logger.error(ex.getMessage());
}
Parameters:
error
public void error(String format, Object[] args)
Logs a format-able message that uses {}
as the placeholder at error
log level.
Code samples
Logging an error with stack trace.
try {
upload(resource);
} catch (IOException ex) {
logger.error("A formattable message. Hello, {}", name, ex);
}
Parameters:
info
public void info(String message)
Logs a message at info
log level.
Code samples
Logging a message at verbose log level.
logger.info("A log message");
Parameters:
info
public void info(String format, Object[] args)
Logs a format-able message that uses {}
as the placeholder at informational
log level.
Code samples
Logging a message at informational log level.
logger.info("A formattable message. Hello, {}", name);
Parameters:
log
public void log(LogLevel logLevel, Supplier
Logs a format-able message that uses {}
as the placeholder at the given logLevel
.
Code samples
Logging with a specific log level
logger.log(LogLevel.VERBOSE,
() -> String.format("Param 1: %s, Param 2: %s, Param 3: %s", "param1", "param2", "param3"));
Parameters:
log
public void log(LogLevel logLevel, Supplier
Logs a format-able message that uses {}
as the placeholder at verbose
log level.
Code samples
Logging with a specific log level and exception
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);
Parameters:
logExceptionAsError
public RuntimeException logExceptionAsError(RuntimeException runtimeException)
Logs the RuntimeException at the error level and returns it to be thrown.
This API covers the cases where a runtime exception type needs to be thrown and logged. If a Throwable is being logged use #logThrowableAsError(Throwable) instead.
Parameters:
Returns:
RuntimeException
.logExceptionAsWarning
public RuntimeException logExceptionAsWarning(RuntimeException runtimeException)
Logs the RuntimeException at the warning level and returns it to be thrown.
This API covers the cases where a runtime exception type needs to be thrown and logged. If a Throwable is being logged use #logThrowableAsWarning(Throwable) instead.
Parameters:
Returns:
verbose
public void verbose(String message)
Logs a message at verbose
log level.
Code samples
Logging a message at verbose log level.
logger.verbose("A log message");
Parameters:
verbose
public void verbose(String format, Object[] args)
Logs a format-able message that uses {}
as the placeholder at verbose
log level.
Code samples
Logging a message at verbose log level.
logger.verbose("A formattable message. Hello, {}", name);
Parameters:
warning
public void warning(String message)
Logs a message at warning
log level.
Code samples
Logging a message at warning log level.
Throwable detailedException = new IllegalArgumentException("A exception with a detailed message");
logger.warning(detailedException.getMessage());
Parameters:
warning
public void warning(String format, Object[] args)
Logs a format-able message that uses {}
as the placeholder at warning
log level.
Code samples
Logging a message at warning log level.
Throwable exception = new IllegalArgumentException("An invalid argument was encountered.");
logger.warning("A formattable message. Hello, {}", name, exception);
Parameters:
Applies to
Azure SDK for Java