WildDev WildDev - 1 year ago 64
Java Question

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

I've added

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"/>

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

is actual class that the Logger has been autowired into?

Answer Source

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?