6

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>
1
  • 1
    My very personal opinion: I dislike this approach as I'm used to take advantage of the log4j flexibility of enabling and disabling logging for specific packages (while developing). And I'd look into logback while you're at it. Commented May 12, 2012 at 1:37

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.