Menu
-->
This article gives example logging configurations for the Azure SDK for Java. For more detail on configuration options, such as setting log levels or custom logging by class, refer to the documentation for your chosen logging framework.
- In this tutorial, we're going to examine Lombok log annotations like @Slf4j, @Log4j or @Log. Use Log Annotations. Lombok provides several log annotations to work with different logging libraries.
- Log4j Implemented Over SLF4J - Log4j implemented over SLF4J org.slf4j: log4j-over-slf4j: 1.7.30 - Official search of Maven Central Repository Maven Central Repository Search Quick Stats Report A.
Log4j Implemented Over SLF4J - Log4j implemented over SLF4J. Maven Central Repository Search Quick Stats Report A Vulnerability GitHub Search. Close search org.slf4j:log4j-over-slf4j. Log4j Implemented Over SLF4J.
NOTE ON PERFORMANCE Contrary to other bridging modules, namely jcl-over-slf4j and log4j-over-slf4j, which reimplement JCL and respectively log4j, the jul-to-slf4j module does not reimplement the java.util.logging because packages under the java. namespace cannot be replaced. The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. Java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time. More information can be found on the SLF4J website.
Exclude Log4j-over-slf4j Maven
The Azure SDK for Java client libraries use the Simple Logging Facade for Java (SLF4J). SLF4J allows you to use your preferred logging framework, which is called at the time of application deployment. If a client builder offers the ability to set HttpLogOptions, an HttpLogDetailLevel and any permissible headers and query parameters must also be specifed in order for any logs to be output.
Note
This article applies to the most recent versions of the Azure SDK client libraries. To see if a library is supported, refer to the list of Azure SDK latest releases. If your application is using an older version of the Azure SDK client libraries, refer to specific instructions in the applicable service documentation.
Declare a logging framework
Before you implement these loggers, you must declare the relevant framework as a dependency in your project. For more information, see the SLF4J user manual.
The following sections provide configuration examples for common logging frameworks.
Use Log4j
The following examples show configurations for the Log4j logging framework. For more information, see the Log4j documentation.
Enable Log4j by adding a Maven dependency
Add the following to your project's pom.xml file:
Enable Log4j using a properties file
Create a log4j.properties file in the ./src/main/resource directory of your project and add the following content:
Enable Log4j using an XML file
Create a log4j.xml file in the ./src/main/resource directory of your project and add the following content:
Use Log4j 2
The following examples show configurations for the Log4j 2 logging framework. For more information, see the Log4j 2 documentation.
Enable Log4j 2 by adding a Maven dependency
Add the following to your project's pom.xml file:
Enable Log4j 2 using a properties file
Create a log4j2.properties file in the ./src/main/resource directory of your project and add the following content:
Enable Log4j 2 using an XML file
Create a log4j2.xml file in the ./src/main/resource directory of your project and add the following content:
Use Logback
The following examples show basic configurations for the Logback logging framework. For more information, see the Logback documentation.
Enable Logback by adding a Maven dependency
Add the following to your project's pom.xml file:
Enable Logback using an XML file
Create a logback.xml file in the ./src/main/resources directory of your project and add the following content:
Use Logback in a Spring Boot application
The following examples show some configurations for using Logback with Spring. You'll typically add logging configurations to a logback.xml file in the ./src/main/resources directory of your project. Spring looks at this file for various configurations including logging. For more information, see the Logback documentation.
You can configure your application to read Logback configurations from any file. To link your logback.xml file to your Spring application, create an application.properties file in the ./src/main/resources directory of your project and add the following content:
To create a Logback configuration for logging to the console, add the following to your logback.xml file:
To configure logging to a file that is rolled over after each hour and archived in gzip format, add the following to your logback.xml file:
Configure fallback logging for temporary debugging
In scenarios where it’s not possible to redeploy your application with an SLF4J logger, you can use the fallback logger built into the Azure client libraries for Java, in Azure Core 1.3.0 or later. To enable this logger, you must first confirm there is no SLF4J logger (as this will take precedence), and then set the
AZURE_LOG_LEVEL
environment variable. After you set the environment variable, you’ll need to restart your application to start generating logs.The following table shows the allowed values for this environment variable.
Log level | Allowed environment variable values |
---|---|
VERBOSE | 'verbose', 'debug' |
INFORMATIONAL | 'info', 'information', 'informational' |
WARNING | 'warn', 'warning' |
ERROR | 'err', 'error' |
Setting an HttpLogDetailLevel
Regardless of the logging mechanism used, if a client builder offers the ability to set HttpLogOptions, these options must additionally be configured to output any logs. An HttpLogDetailLevel must be specifed to indicate what information should be logged. This value defaults to
NONE
, so if it is not specified, no logs will be output even if the logging framework or fallback logging is properly configured. For security reasons, headers and query parameters are redacted by default, so the log options must also be provided with a Set<String>
indicating which headers and query parameters are safe to print. These value may be configured as shown below. The logging is set to print both body content and header values, all header values will be redacted except the value for the user specified metadata corresponding to the key 'foo'
, and all query parameters will be redacted except for the sas token query parameter 'sv'
indicating the signed version of any sas which may be present.Log4j-over-slf4j-1.7.25 Maven
Note
This example uses a Storage client builder, but the principle applies for any builder which accepts
HttpLogOptions
. Furthermore, this example does not demonstrate complete configuration of a client and is only intended to illustrate configuration of logging. For more information on configuring clients, see the documentation on the respective builders.