Class ClientLogger
- java.lang.Object
-
- com.azure.core.util.logging.ClientLogger
-
public class ClientLogger extends Object
This is a fluent logger helper class that wraps a pluggableLogger.This logger logs format-able messages that use
{}as the placeholder. When athrowableis the last argument of the format varargs and the logger is enabled forverbose, the stack trace for the throwable is logged.A minimum logging level threshold is determined by the
AZURE_LOG_LEVELenvironment configuration. By default logging is disabled.Log level hierarchy
- See Also:
Configuration
-
-
Constructor Summary
Constructors Constructor Description ClientLogger(Class<?> clazz)Retrieves a logger for the passed class using theLoggerFactory.ClientLogger(String className)Retrieves a logger for the passed class name using theLoggerFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleancanLogAtLevel(LogLevel logLevel)Determines if the app or environment logger support logging at the given log level.voiderror(String message)Logs a message aterrorlog level.voiderror(String format, Object... args)Logs a format-able message that uses{}as the placeholder aterrorlog level.voidinfo(String message)Logs a message atinfolog level.voidinfo(String format, Object... args)Logs a format-able message that uses{}as the placeholder atinformationallog level.voidlog(LogLevel logLevel, Supplier<String> message)Logs a format-able message that uses{}as the placeholder at the givenlogLevel.voidlog(LogLevel logLevel, Supplier<String> message, Throwable throwable)Logs a format-able message that uses{}as the placeholder atverboselog level.RuntimeExceptionlogExceptionAsError(RuntimeException runtimeException)Logs theRuntimeExceptionat the error level and returns it to be thrown.RuntimeExceptionlogExceptionAsWarning(RuntimeException runtimeException)Logs theRuntimeExceptionat the warning level and returns it to be thrown.<T extends Throwable>
TlogThowableAsWarning(T throwable)Deprecated.UselogThrowableAsWarning(Throwable)instead.<T extends Throwable>
TlogThrowableAsError(T throwable)Logs theThrowableat the error level and returns it to be thrown.<T extends Throwable>
TlogThrowableAsWarning(T throwable)Logs theThrowableat the warning level and returns it to be thrown.voidverbose(String message)Logs a message atverboselog level.voidverbose(String format, Object... args)Logs a format-able message that uses{}as the placeholder atverboselog level.voidwarning(String message)Logs a message atwarninglog level.voidwarning(String format, Object... args)Logs a format-able message that uses{}as the placeholder atwarninglog level.
-
-
-
Constructor Detail
-
ClientLogger
public ClientLogger(Class<?> clazz)
Retrieves a logger for the passed class using theLoggerFactory.- Parameters:
clazz- Class creating the logger.
-
ClientLogger
public ClientLogger(String className)
Retrieves a logger for the passed class name using theLoggerFactory.- Parameters:
className- Class name creating the logger.- Throws:
RuntimeException- it is an error.
-
-
Method Detail
-
log
public void log(LogLevel logLevel, Supplier<String> message)
Logs a format-able message that uses{}as the placeholder at the givenlogLevel.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:
logLevel- Logging level for the log message.message- The format-able message to log.
-
log
public void log(LogLevel logLevel, Supplier<String> message, Throwable throwable)
Logs a format-able message that uses{}as the placeholder atverboselog 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:
logLevel- Logging level for the log message.message- The format-able message to log.throwable- Throwable for the message.Throwable.
-
verbose
public void verbose(String message)
Logs a message atverboselog level.Code samples
Logging a message at verbose log level.
logger.verbose("A log message");- Parameters:
message- The message to log.
-
verbose
public void verbose(String format, Object... args)
Logs a format-able message that uses{}as the placeholder atverboselog level.Code samples
Logging a message at verbose log level.
logger.verbose("A formattable message. Hello, {}", name);- Parameters:
format- The formattable message to log.args- Arguments for the message. If an exception is being logged, the last argument should be theThrowable.
-
info
public void info(String message)
Logs a message atinfolog level.Code samples
Logging a message at verbose log level.
logger.info("A log message");- Parameters:
message- The message to log.
-
info
public void info(String format, Object... args)
Logs a format-able message that uses{}as the placeholder atinformationallog level.Code samples
Logging a message at informational log level.
logger.info("A formattable message. Hello, {}", name);- Parameters:
format- The format-able message to logargs- Arguments for the message. If an exception is being logged, the last argument should be theThrowable.
-
warning
public void warning(String message)
Logs a message atwarninglog 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:
message- The message to log.
-
warning
public void warning(String format, Object... args)
Logs a format-able message that uses{}as the placeholder atwarninglog 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:
format- The format-able message to log.args- Arguments for the message. If an exception is being logged, the last argument should be theThrowable.
-
error
public void error(String message)
Logs a message aterrorlog level.Code samples
Logging a message at error log level.
try { upload(resource); } catch (IOException ex) { logger.error(ex.getMessage()); }- Parameters:
message- The message to log.
-
error
public void error(String format, Object... args)
Logs a format-able message that uses{}as the placeholder aterrorlog level.Code samples
Logging an error with stack trace.
try { upload(resource); } catch (IOException ex) { logger.error("A formattable message. Hello, {}", name, ex); }- 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.
-
logExceptionAsWarning
public RuntimeException logExceptionAsWarning(RuntimeException runtimeException)
Logs theRuntimeExceptionat 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
Throwableis being logged uselogThrowableAsWarning(Throwable)instead.- Parameters:
runtimeException- RuntimeException to be logged and returned.- Returns:
- The passed
RuntimeException. - Throws:
NullPointerException- IfruntimeExceptionisnull.
-
logThowableAsWarning
@Deprecated public <T extends Throwable> T logThowableAsWarning(T throwable)
Deprecated.UselogThrowableAsWarning(Throwable)instead.Logs theThrowableat 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
RuntimeExceptionis being logged uselogExceptionAsWarning(RuntimeException)instead.- Type Parameters:
T- Type of the Throwable being logged.- Parameters:
throwable- Throwable to be logged and returned.- Returns:
- The passed
Throwable. - Throws:
NullPointerException- Ifthrowableisnull.
-
logThrowableAsWarning
public <T extends Throwable> T logThrowableAsWarning(T throwable)
Logs theThrowableat 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
RuntimeExceptionis being logged uselogExceptionAsWarning(RuntimeException)instead.- Type Parameters:
T- Type of the Throwable being logged.- Parameters:
throwable- Throwable to be logged and returned.- Returns:
- The passed
Throwable. - Throws:
NullPointerException- Ifthrowableisnull.
-
logExceptionAsError
public RuntimeException logExceptionAsError(RuntimeException runtimeException)
Logs theRuntimeExceptionat 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
Throwableis being logged uselogThrowableAsError(Throwable)instead.- Parameters:
runtimeException- RuntimeException to be logged and returned.- Returns:
- The passed
RuntimeException. - Throws:
NullPointerException- IfruntimeExceptionisnull.
-
logThrowableAsError
public <T extends Throwable> T logThrowableAsError(T throwable)
Logs theThrowableat 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
RuntimeExceptionis being logged uselogExceptionAsError(RuntimeException)instead.- Type Parameters:
T- Type of the Throwable being logged.- Parameters:
throwable- Throwable to be logged and returned.- Returns:
- The passed
Throwable. - Throws:
NullPointerException- Ifthrowableisnull.
-
canLogAtLevel
public boolean canLogAtLevel(LogLevel logLevel)
Determines if the app or environment logger support logging at the given log level.- Parameters:
logLevel- Logging level for the log message.- Returns:
- Flag indicating if the environment and logger are configured to support logging at the given log level.
-
-