0% found this document useful (0 votes)
77 views

HEADS UP - Add Logging Messages in Spring 5.1: The Problem

Uploaded by

Sandy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

HEADS UP - Add Logging Messages in Spring 5.1: The Problem

Uploaded by

Sandy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HEADS UP - Add Logging

Messages in Spring 5.1

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 listed
below. 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.

© 2019 luv2code LLC 1 www.luv2code.com


Adding Spring Security to CRM REST API

Overview of the steps

1. Create a bean to configure the parent logger and console handler

2. Configure the bean in the Spring XML config file

Detailed Steps

1. Create a bean 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. It 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

This class also has an init method to handle the actual configuration.
The init method is executed after the bean has been created and
dependencies injected.

File: MyLoggerConfig.java
1. package com.luv2code.springdemo;
2.
3. import java.util.logging.ConsoleHandler;
4. import java.util.logging.Level;
5. import java.util.logging.Logger;

©2018 luv2code LLC 2 www.luv2code.com


Adding Spring Security to CRM REST API

6. import java.util.logging.SimpleFormatter;
7.
8. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9.
10. public class MyLoggerConfig {
11.
12. private String rootLoggerLevel;
13. private String printedLoggerLevel;
14.
15. public void setRootLoggerLevel(String rootLoggerLevel) {
16. this.rootLoggerLevel = rootLoggerLevel;
17. }
18.
19. public void setPrintedLoggerLevel(String printedLoggerLevel) {
20. this.printedLoggerLevel = printedLoggerLevel;
21. }
22.
23. public void initLogger() {
24.
25. // parse levels
26. Level rootLevel = Level.parse(rootLoggerLevel);
27. Level printedLevel = Level.parse(printedLoggerLevel);
28.
29. // get logger for app context
30. Logger applicationContextLogger = Logger.getLogger(AnnotationConfigA
pplicationContext.class.getName());
31.
32. // get parent logger
33. Logger loggerParent = applicationContextLogger.getParent();
34.
35. // set root logging level
36. loggerParent.setLevel(rootLevel);
37.
38. // set up console handler
39. ConsoleHandler consoleHandler = new ConsoleHandler();
40. consoleHandler.setLevel(printedLevel);
41. consoleHandler.setFormatter(new SimpleFormatter());
42.
43. // add handler to the logger
44. loggerParent.addHandler(consoleHandler);
45. }
46.
47. }

---

2. Configure the bean in the Spring XML config file

In your XML config file, add the following bean entry. Make sure to list
this as the first bean so that it is initialized first. Since the bean is
initialized first, then you will get all of the logging traffic. If you move it
later in the config file after the other beans, then you will miss out on
some of the initial logging messages.

File: applicationContext.xml (snippet)

©2018 luv2code LLC 3 www.luv2code.com


Adding Spring Security to CRM REST API

1. <!--
2. Add a logger config to see logging messages.
3. - For more detailed logs, set values to "FINEST"
4. - For info on logging levels, see: http://www.vogella.com/tutorials/Logging/ar
ticle.html
5. -->
6. <bean id="myLoggerConfig" class="com.luv2code.springdemo.MyLoggerConfig" init-metho
d="initLogger">
7. <property name="rootLoggerLevel" value="FINE" />
8. <property name="printedLoggerLevel" value="FINE"/>
9. </bean>

---

Source code is available here.

---

Once you make these updates, then you will be able to see additional
logging data. :-)

©2018 luv2code LLC 4 www.luv2code.com

You might also like