Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a log4j Logger using Spring XML configuration?

Tags:

java

spring

log4j

How do I create a log4j Logger using Spring XML configuration?

I would like to do something like this so I can inject the logger into other instances:

<bean id="logger" class="org.apache.log4j.Logger">
    <property name="logName" value="my.Logger" />
</bean>
like image 554
Victor Lyuboslavsky Avatar asked May 12 '12 00:05

Victor Lyuboslavsky


People also ask

How do you configure log4j for logging in spring boot?

xml under the src folder. Create log4J configuration file log4j. properties under the src folder. The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.


1 Answers

You can construct beans via static methods using the factory-method attribute. So for log4j we can use the static Logger.getLogger() method to construct a bean:

<bean id="logger" class="org.apache.log4j.Logger" factory-method="getLogger">
    <constructor-arg type="java.lang.String" value="my.Logger" />
</bean>
like image 50
krock Avatar answered Oct 21 '22 18:10

krock