Piezīmes
Lai piekļūtu šai lapai, ir nepieciešama autorizācija. Varat mēģināt pierakstīties vai mainīt direktorijus.
Lai piekļūtu šai lapai, ir nepieciešama autorizācija. Varat mēģināt mainīt direktorijus.
This article provides an overview of how to add logging by 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, complete the following steps:
- Include the SLF4J adapter for
java.util.loggingas a dependency. - Create a file named logging.properties under the /src/main/resources project directory.
For more information about configuring your logger, see Configuring Logging Output in the Oracle documentation.
Add the Maven dependency for java.util.logging
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>
Add logging.properties to your project
To log by using java.util.logging, create a file named logging.properties under the ./src/main/resources directory of your project or anywhere else. This file contains the logging configurations to customize your logging needs. Provide the path to the file by setting the java.util.logging.config.file system property. Set this property before you create the logger instance. For more information, see Java Logging: Configuration.
Console logging
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
Log to a file
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 creates a file named myapplication.log in your home directory (%h). This logger doesn't support automatic file rotation after a certain period. If you need this functionality, you must write a scheduler to manage log file rotation.
Next steps
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 master logging, consider looking into the integrations that Azure offers into frameworks such as Spring.