Saturday, November 18, 2017

Injecting Logger

According to https://docs.jboss.org/weld/reference/2.4.0.CR1/en-US/html/injection.html :

import org.slf4j.Logger;

@Named
@SessionScoped
public class CaloriesController implements Serializable  {
 @Inject
 private Logger logger;
    public void insertUser() {
     logger.debug("insertUser");
    }
}


but this is not enough... you will get a "WELD-001408 Unsatisfied dependencies for type Logger with qualifiers @Default at injection point " ...

You have to prepare also a PRODUCER:

package org.pierre.calories.common;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@ManagedBean
public class LoggingProducer {
 
 @Produces
 public Logger getLogger(final InjectionPoint ip) {
  return LoggerFactory.getLogger(ip.getMember().getDeclaringClass());
 }

}



See also https://stackoverflow.com/questions/19768405/weld-001408-unsatisfied-dependencies-for-type-logger-with-qualifiers-default


Again, one would hope that in 2017 these things were a bit better engineered and transparent.... but this is the world of IT, a huge morassic mess.... La Brea Tar Pits:



No comments: