title | description | ms.date | ms.topic | ms.custom | author | ms.author | ms.reviewer |
---|---|---|---|---|---|---|---|
Log with the Azure SDK for Java and java.util.logging | Provides an overview of the Azure SDK for Java integration with java.util.logging. | 04/01/2025 | conceptual | devx-track-java, devx-track-extended-java | KarlErickson | karler | srnagar |
This article provides an overview of how to add logging using java.util.logging
to applications that use the Azure SDK for Java. The java.util.logging
framework is part of the JDK. As mentioned in Configure logging in the Azure SDK for Java, all Azure client libraries log through Simple Logging Facade for Java (SLF4J), so you can use logging frameworks such as java.util.logging
.
To enable java.util.logging
, you must do two things:
- Include the SLF4J adapter for
java.util.logging
as a dependency, - Create a file called logging.properties under the /src/main/resources project directory.
For more information related to configuring your logger, see Configuring Logging Output in the Oracle documentation.
To add the Maven dependency, include the following XML in the project's pom.xml file. Replace the 1.7.30
version number with the latest released version number shown on the SLF4J JDK14 Binding page.
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.30</version> <!-- replace this version with the latest available version on Maven central --> </dependency>
To log using java.util.logging
, create a file called logging.properties under the ./src/main/resources directory of your project or anywhere else. This file will contain the logging configurations to customize your logging needs. Provide path to the file by setting the java.util.logging.config.file
system property. You must set this property before you create the logger instance. For more information, see Java Logging: Configuration.
You can create a configuration to log to the console as shown in the following example. This example is configured to log all logging events that are INFO level or higher, wherever they come from.
handlers = java.util.logging.ConsoleHandler .level = INFO java.util.logging.ConsoleHandler.level = INFO java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format=[%1$tF %1$tH:%1$tM:%1$tS.%1$tL] [%4$s] %3$s %5$s %n
The previous example logs to the console, which isn't normally the preferred location for logs. To configure logging to a file instead, use the following configuration:
handlers = java.util.logging.FileHandler .level = INFO java.util.logging.FileHandler.pattern = %h/myapplication.log java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.FileHandler.level = INFO
This code will create a file called myapplication.log in your home directory (%h
). This logger doesn't support automatic file rotation after a certain period. If you require this functionality, you'll need to write a scheduler to manage log file rotation.
This article covered the configuration of java.util.logging
and how to make the Azure SDK for Java use it for logging. Because the Azure SDK for Java works with all SLF4J logging frameworks, consider reviewing the SLF4J user manual for further details.
After you've mastered logging, consider looking into the integrations that Azure offers into frameworks such as Spring and MicroProfile.