JDK Logging (JUL) in Spring Boot Applications

When writing spring boot applications, it is recommended to use SLF4J API for logging. However it is also possible to use the logging implementation provided by JDK directly. However JDK logging implementation (JUL) provides a different set of logging levels compared to SLF4J or Log4J. The logging levels used by JDK library are,

FINEST > FINER > FINE > CONFIG > INFO > WARNING > SEVERE

SLF4J and other logging implementations such as Log4j use the following log levels,

ALL > TRACE > DEBUG > INFO > WARN > ERROR > FATAL > OFF

Spring boot’s SLF4J bridge maps the log levels to the JDK logging levels using the following table.

FINEST  => TRACE
FINER   => DEBUG
FINE    => DEBUG
CONFIG  => INFO
INFO    => INFO
WARNING => WARN
SEVERE  => ERROR

Spring boot propagates the logging configuration set in application.properties to JDK logging classes as well. Hence even if you are using both logging libraries in your application (JDK logging and Logback with SLF4J), the same set of configuration in application.properties can be used! For example, the following configuration sets the root logger to WARN (WARNING) level and all application classes to TRACE (FINEST). Note that you need to use the log levels defined by SLF4J.

# application.properties values
logging.level.root=WARN
logging.level.com.quickprogrammingtips=TRACE

To configure only the logback system, create a custom logback.xml or logback-spring.xml. To configure only the JDK logging classes (JUL), create a custom logging.properties file. These files should be created in src/main/resources folder.

Using JDK Logging (JUL) in Spring Boot Classes

Here is a simple spring boot controller which uses the JDK logging library,

package com.quickprogrammingtips.springboot;

import java.util.logging.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorld2Controller {
    private static final Logger LOG = Logger.getLogger(HelloWorld2Controller.class.getName());

    @RequestMapping("/hello2")
    @ResponseBody
    String home() {
        LOG.finest("Sending hello world message!");
        return "Hello World!";
    }
}

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution! Please email us.

Leave a Reply