Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Last active February 5, 2023 06:34
Show Gist options
  • Save darbyluv2code/cfb16c2fd1825a947d8faca3724b47a9 to your computer and use it in GitHub Desktop.
Save darbyluv2code/cfb16c2fd1825a947d8faca3724b47a9 to your computer and use it in GitHub Desktop.
Spring Logging for Spring 5.1 - XML Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
Add a logger config to see logging messages.
- For more detailed logs, set values to "FINEST"
- For info on logging levels, see: http://www.vogella.com/tutorials/Logging/article.html
-->
<bean id="myLoggerConfig" class="com.luv2code.springdemo.MyLoggerConfig" init-method="initLogger">
<property name="rootLoggerLevel" value="FINE" />
<property name="printedLoggerLevel" value="FINE"/>
</bean>
<!-- Define your beans here -->
<!-- define the dependency -->
<bean id="myFortuneService"
class="com.luv2code.springdemo.HappyFortuneService">
</bean>
<bean id="myCoach"
class="com.luv2code.springdemo.TrackCoach">
<!-- set up constructor injection -->
<constructor-arg ref="myFortuneService" />
</bean>
</beans>
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 org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyLoggerConfig {
private String rootLoggerLevel;
private String printedLoggerLevel;
public void setRootLoggerLevel(String rootLoggerLevel) {
this.rootLoggerLevel = rootLoggerLevel;
}
public void setPrintedLoggerLevel(String printedLoggerLevel) {
this.printedLoggerLevel = printedLoggerLevel;
}
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);
}
}
@darbyluv2code
Copy link
Author

darbyluv2code commented Jun 23, 2019

HappyFortuneService is an implementation of the FortuneService interface. This code is covered in my premium online course: Spring and Hibernate for Beginners. Details here: http://www.luv2code.com/full-spring

@varun-creator
Copy link

sir, please provide a course for beginner to expert level, or how to work in the company

@jaideepjagyasi
Copy link

Very good course and references

@zee123123
Copy link

Where should I put those files?

@thinhify
Copy link

thinhify commented Aug 2, 2021

Put it in the right order and still not working :'(

@DanielIvascu
Copy link

thinhify, you have to delete from applicationContext.xml this :


	<constructor-arg ref="myFortuneService" />

@saurabhsingh17
Copy link

saurabhsingh17 commented Jun 2, 2022

Remove bean id="myFortuneService"
class="com.luv2code.springdemo.HappyFortuneService">
bean>

	If getting exceptions.

@pingponghikhik
Copy link

how can i run this file sir?

@darbyluv2code
Copy link
Author

@pingponghikhik - Can you repost the question to the classroom discussion Q&A? Here's the link to the classroom discussion Q&A:
https://www.udemy.com/spring-hibernate-tutorial/learn/v4/questions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment