In the last tutorial we saw that Spring framework by default uses JCL logging with JUL implementation. In this tutorial we will customize the underlying default JUL logging by providing logging properties.
Example Let's add JUL specific logging.properties
with some customization:
Adding JUL properties
src/main/resources/logging.propertieshandlers= 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$tT] [%4$s] {%2$s} %5$s %n
A bean with logging
package com.logicbig.example;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyBean {
private static Log log = LogFactory.getLog(MyBean.class);
public void doSomething() {
log.info("doing something");
}
}
Main class
Let's load the properties file and run spring application.
package com.logicbig.example;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.InputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;
@Configuration
public class ExampleMain {
@Bean
public MyBean myBean() {
return new MyBean();
}
public static void main(String[] args) {
loadJulLogProperties();
ConfigurableApplicationContext context =
new AnnotationConfigApplicationContext(ExampleMain.class);
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
private static void loadJulLogProperties() {
try (InputStream stream = ExampleMain.class.getResourceAsStream("/logging.properties")) {
if (stream != null) {
//Jul's log manager
LogManager.getLogManager().readConfiguration(stream);
} else {
Logger.getAnonymousLogger().severe("Could not find logging.properties file in classpath");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Output[2025-11-10 12:43:59] [INFO] {com.logicbig.example.MyBean doSomething} doing something
Note that spring internal logging will also be customized per JUL properties (not showing in above output).
In next couple of tutorials, we will explore other logging details in Spring core.
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|