How Spring Boot uses JUL for logging
In Spring Boot, we can use various logging frameworks for logging. Among them, JUL (Java Util Logging) is the logging framework that comes with the Java platform, which provides a simple API and configuration for easy logging. This article describes how to use JUL for logging in Spring Boot and provides sample code.
Configure JUL
By default, Spring Boot uses Logback as its logging framework. If you want to use JUL for logging, some configuration is required. First, we need to add the following dependencies to pom.xml
the file :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.30</version>
</dependency>
In the dependencies above, we excluded the default logging framework dependency spring-boot-starter-logging
and added the Jetty server dependency. Additionally, we have added a dependency jul-to-slf4j
of which converts the JUL logger to an SLF4J logger for use in Spring Boot.
Next, we need to add the following configuration to application.properties
the file :
logging.level.root=INFO
logging.level.org.springframework=INFO
logging.level.org.hibernate=INFO
logging.level.com.example=FINEST
logging.config=classpath:logging.properties
In the above configuration, we specified the log level and the location of the log configuration file. logging.level.root
Represents the log level of the root logger, logging.level.org.springframework
represents the log level of the Spring framework, logging.level.org.hibernate
represents the log level of the Hibernate framework, and logging.level.com.example
represents the log level of our own application. Here, we set the log level of our application to FINEST
, indicating the most detailed log. logging.config
Indicates the location of the logging configuration file, which we will set to classpath:logging.properties
.
Next, we need to create a file logging.properties
called , which will configure JUL. In this file, we can configure options such as JUL's log level, output destination, formatter and filter. Here is a simple example:
handlers=java.util.logging.ConsoleHandler
.level=FINEST
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
In the configuration above, we specified the log level, output destination and formatter. handlers
The output target is specified, here we output the log to the console. .level
Indicates the log level of the root logger, java.util.logging.ConsoleHandler.level
the log level of the console output target, java.util.logging.ConsoleHandler.formatter
and the formatter of the console output target, which we use here SimpleFormatter
.
Using JUL with Spring Boot
After configuring JUL, we can use JUL for logging in Spring Boot. First, we need to create a logger logger
called , for example:
private static final Logger logger = Logger.getLogger(DemoController.class.getName());
In the above code, we use Logger.getLogger
the method to create a logger
logger named , specifying the name of the class where the logger is located.
Next, we can log using the logger. For example:
logger.info("This is an info message.");
In the code above, we use logger.info
the method to record an info level log.
JUL supports a variety of log levels, including SEVERE
, WARNING
, INFO
, CONFIG
, FINE
, andFINER
levels. FINEST
You can select an appropriate log level for recording according to actual needs.
Additionally, JUL supports logging with placeholders, for example:
String message = "Hello, {}!";
String name = "world";
logger.info(message, name);
In the above code, we use the placeholder {}
to indicate the part that needs to be replaced in the log, and then pass in the value to be replaced in the logging method. This method can avoid the performance problems caused by concatenating strings, and also avoid the problem of incorrect log output caused by string splicing errors.
sample code
Here is a complete sample code demonstrating how to use JUL for logging in Spring Boot:
import java.util.logging.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
private static final Logger logger = Logger.getLogger(DemoController.class.getName());
@GetMapping("/hello")
public String hello() {
logger.info("This is an info message.");
String message = "Hello, {}!";
String name = "world";
logger.info(message, name);
return "Hello, world!";
}
}
In the code above, we created DemoController
a Spring MVC controller called with a hello
handler method called . In this method, we use the method described above to record two information-level logs and return a string.
Summarize
In this article, we introduced how to use JUL for logging in Spring Boot and provided sample code. JUL is the logging framework that comes with the Java platform, providing a simple API and configuration for easy logging. By configuring and using JUL, we can perform logging more conveniently and improve the maintainability and reliability of the application.