1

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?

WildDev
  • 2,250
  • 5
  • 35
  • 67
  • Why do you want to inject it ? Did you try to instanciate it with the lhe class you want to use. e.g. private static final Logger logger = Logger.getLogger(AccountValidator.class); – Thomas Betous Jun 30 '16 at 07:54
  • @tbetous, yeah it would work. Dunno, maybe it's about code design – WildDev Jun 30 '16 at 07:57

1 Answers1

2

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?

Community
  • 1
  • 1
Thomas Betous
  • 4,633
  • 2
  • 24
  • 45