Last active
February 5, 2023 06:34
-
-
Save darbyluv2code/cfb16c2fd1825a947d8faca3724b47a9 to your computer and use it in GitHub Desktop.
Spring Logging for Spring 5.1 - XML Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
sir, please provide a course for beginner to expert level, or how to work in the company
Very good course and references
Where should I put those files?
Put it in the right order and still not working :'(
thinhify, you have to delete from applicationContext.xml this :
<constructor-arg ref="myFortuneService" />
Remove bean id="myFortuneService"
class="com.luv2code.springdemo.HappyFortuneService">
bean>
If getting exceptions.
how can i run this file sir?
@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
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