WildDev WildDev - 5 months ago 15
Java Question

Spring XML - autowired Logger doesn't determine target class name

I've added

log4j
Logger into Spring xml configuration to use it later as a component:

<bean id="logger" class="org.apache.log4j.Logger" factory-method="getLogger">
<constructor-arg type="java.lang.String" value="LoggerName"/>
</bean>


seems everything works fine, but logs are don't point to the target classes where they're have been autowired:

2016-06-30 09:54:23 DEBUG LoggerName:29 - Account does not exists


How to setup the Logger bean properly to let it determine the target class names like this:

2016-06-30 09:54:23 DEBUG AccountValidator:29 - Account does not exists


where
AccountValidator
is actual class that the Logger has been autowired into?

Answer

In my opinion you should instanciate a logger per class :

private static final Logger logger = Logger.getLogger(AccountValidator.class);

If you absolutely need to do it with injection you can use CDI's injection point mechanism. This post is about what you want :

What is the Spring DI equivalent of CDI's InjectionPoint?

Comments