Add Logging Messages in Spring 5.1 - All Java Config Version
Add Logging Messages in Spring 5.1 - All Java Config Version
The Problem
In Spring 5.1, the Spring Development team changed the logging levels internally. As a result, by default you will no longer see the red logging messages at the INFO level. This is different than in the videos.
The Solution
If you would like to configure your app to show similar logging messages as in the video, you can make the following updates. Note, you will not see the EXACT same messages, since the Spring team periodically changes the text of the internal logging messages. However, this should give you some additional logging data.
Overview of the steps
0. Create a logging properties file
1. Create a configuration class to configure the parent logger and console handler
Detailed Steps
0. Create a logging properties file
This properties file will define the logging levels for the application. The props file sets the logger level to FINE. For more detailed logging info, you can set the logging level to level to FINEST. You can read more about the logging levels at http://www.vogella.com/tutorials/Logging/article.html
File: src/mylogger.properties
- root.logger.level=FINE
- printed.logger.level=FINE
---
1. Create a configuration class to configure the parent logger and console handler
This class will set the parent logger level for the application context. It will also set the logging level for console handler. The logging levels are loaded from the configuration file using the @PropertySource annotation. The fields are injected using the @Value annotation. This class also has a @PostConstruct method to handle the actual configuration.
File: MyLoggerConfig.java
- package com.luv2code.springdemo;
- import java.util.logging.ConsoleHandler;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.util.logging.SimpleFormatter;
- import javax.annotation.PostConstruct;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.PropertySource;
- @Configuration
- @PropertySource("classpath:mylogger.properties")
- public class MyLoggerConfig {
- @Value("${root.logger.level}")
- private String rootLoggerLevel;
- @Value("${printed.logger.level}")
- private String printedLoggerLevel;
- @PostConstruct
- public void initLogger() {
- // parse levels
- Level rootLevel = Level.parse(rootLoggerLevel);
- Level printedLevel = Level.parse(printedLoggerLevel);
- // get logger for app context
- Logger applicationContextLogger = Logger.getLogger(AnnotationConfigApplicationContext.class.getName());
- // get parent logger
- Logger loggerParent = applicationContextLogger.getParent();
- // set root logging level
- loggerParent.setLevel(rootLevel);
- // set up console handler
- ConsoleHandler consoleHandler = new ConsoleHandler();
- consoleHandler.setLevel(printedLevel);
- consoleHandler.setFormatter(new SimpleFormatter());
- // add handler to the logger
- loggerParent.addHandler(consoleHandler);
- }
- }
Comments
Post a Comment