Class ClientLogger
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
AZURE_LOG_LEVEL environment configuration. By default logging is
disabled.
Log level hierarchy
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.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionClientLogger(Class<?> clazz) Retrieves a logger for the passed class using theLoggerFactory.ClientLogger(Class<?> clazz, Map<String, Object> context) Retrieves a logger for the passed class using theLoggerFactory.ClientLogger(String className) Retrieves a logger for the passed class name using theLoggerFactory.ClientLogger(String className, Map<String, Object> context) Retrieves a logger for the passed class name using theLoggerFactorywith context that will be populated on all log records produced with this logger. -
Method Summary
Modifier and TypeMethodDescriptionatError()CreatesLoggingEventBuilderforerrorlog level that can be used to enrich log with additional context.atInfo()CreatesLoggingEventBuilderforinfolog level that can be used to enrich log with additional context.CreatesLoggingEventBuilderfor log level that can be used to enrich log with additional context.CreatesLoggingEventBuilderforverboselog level that can be used to enrich log with additional context.CreatesLoggingEventBuilderforwarninglog level that can be used to enrich log with additional context.booleancanLogAtLevel(LogLevel logLevel) Determines if the app or environment logger support logging at the given log level.voidLogs a message aterrorlog level.voidLogs a format-able message that uses{}as the placeholder aterrorlog level.voidLogs a message atinfolog level.voidLogs a format-able message that uses{}as the placeholder atinformationallog level.voidLogs a format-able message that uses{}as the placeholder at the givenlogLevel.voidLogs a format-able message that uses{}as the placeholder atverboselog level.logExceptionAsError(RuntimeException runtimeException) Logs theRuntimeExceptionat the error level and returns it to be thrown.logExceptionAsWarning(RuntimeException runtimeException) Logs theRuntimeExceptionat the warning level and returns it to be thrown.<T extends Throwable>
TlogThowableAsWarning(T throwable) Deprecated.<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.voidLogs a message atverboselog level.voidLogs a format-able message that uses{}as the placeholder atverboselog level.voidLogs a message atwarninglog level.voidLogs a format-able message that uses{}as the placeholder atwarninglog level.
-
Constructor Details
-
ClientLogger
Retrieves a logger for the passed class using theLoggerFactory.- Parameters:
clazz- Class creating the logger.
-
ClientLogger
Retrieves a logger for the passed class name using theLoggerFactory.- Parameters:
className- Class name creating the logger.- Throws:
RuntimeException- when logging configuration is invalid depending on SLF4J implementation.
-
ClientLogger
Retrieves a logger for the passed class using theLoggerFactory.- Parameters:
clazz- Class creating the logger.context- Context to be populated on every log record written with this logger. Objects are serialized withtoString()method.- Throws:
NullPointerException- Ifclazzis null.
-
ClientLogger
Retrieves a logger for the passed class name using theLoggerFactorywith 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:
className- Class name creating the logger.context- Context to be populated on every log record written with this logger. Objects are serialized withtoString()method.- Throws:
RuntimeException- when logging configuration is invalid depending on SLF4J implementation.
-
-
Method Details
-
log
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
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
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
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
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
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
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
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
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
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
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.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
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
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
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
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.
-
atError
CreatesLoggingEventBuilderforerrorlog 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:
- instance of
LoggingEventBuilderor no-op if error logging is disabled.
-
atWarning
CreatesLoggingEventBuilderforwarninglog 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:
- instance of
LoggingEventBuilderor no-op if warn logging is disabled.
-
atInfo
CreatesLoggingEventBuilderforinfolog 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:
- instance of
LoggingEventBuilderor no-op if info logging is disabled.
-
atVerbose
CreatesLoggingEventBuilderforverboselog 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:
- instance of
LoggingEventBuilderor no-op if verbose logging is disabled.
-
atLevel
CreatesLoggingEventBuilderfor 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:
level- log level.- Returns:
- instance of
LoggingEventBuilderor no-op if logging at provided level is disabled.
-
logThrowableAsWarning(Throwable)instead.