2

I have this LoggerProducer class that's injected in a @Stateless bean to generate log entries as explained here.

Problem is that when CustomerBean is invoked (without even calling logger.info), the @Produces method (that retrieves the bean class name) fails with NullPointerException. What is wrong with this code?

@Named
@Singleton
public class LoggerProducer {

    @Produces
    public Logger produceLogger(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(
                   injectionPoint.getBean().getBeanClass()); // <- error here
    }   

}

The bean that injects the logger:

import org.slf4j.Logger;

@Stateless
@LocalBean
@Named
public class CustomerBean  {

    @Inject
    private Logger logger;

    ......
      logger.info("some message");
ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

5

Assuming that injectionPoint is not null (in your producer method), you can try this:

@Produces 
Logger createLogger(InjectionPoint injectionPoint) { 
    return LoggerFactory.getLogger( injectionPoint.getMember().getDeclaringClass().getName() );
}
Carlitos Way
  • 3,279
  • 20
  • 30