In this post I will show you how to inject an instance of Logger class using CDI (Context Dependency Injection) and SLF4J (Simple Logging Facade for Java).
To keep it simple, we only have a xhtml page (index), a managed bean (Controller.java) and the producer (LoggerProducer.java). The project configuration is as follows:

As we are using JSF with facelets, the index page looks like:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"> <h:head> <title>CDI Producer</title> </h:head> <h:body> <h2>The name is: #{controller.name}</h2> </h:body> </html> |
In this file we simply display the name that is setted in the controller (line 10).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package pt.joaobrito.controller; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; import org.slf4j.Logger; @Named @RequestScoped public class Controller { @Inject private Logger logger; private String name; public Controller() { } @PostConstruct public void init() { logger.info("before setting the name: name = " + name); this.name = "John Doe"; logger.info("the name was setted to: name = " + name); } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
In the controller we inject (@Inject) an instance of the Logger class and print to the console the name before and after the name is setted (lines 23 and 25). CDI knows which Logger to inject because we have a class that works as a factory of Loggers (LoggerProducer) with some special annotations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package pt.joaobrito.producer; import javax.enterprise.context.Dependent; import javax.enterprise.inject.Produces; import javax.enterprise.inject.spi.InjectionPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Dependent public class LoggerProducer { @Produces public Logger newLoggerInstance(InjectionPoint ip) { return LoggerFactory.getLogger(ip.getMember().getClass()); } } |
In the LoggerProducer class we have an annotation (@Produces) in a method that returns a Logger instance. This is enough to CDI inject a Logger when needed. Notice that in line 13 we pass as a parameter the injection point (ip) in order to get the class where the logger is being injected (in this specific case ‘Controller.class’). This is only necessary beacuse the getLogger method needs it.
Finally, here are the results (the log and the web page):
log
As you can see here, we have two lines displaying the information that we expected: first the name is null and after the set of the variable name, name = John Doe.

That’s it. It’s just that simple!
Conclusion
First we create a factory of loggers, then we inject them whenever we need them:
@Produces to indicate CDI what we are producing and
@Inject to inject the instance ready to use.
Notice that this methodology works with other types rather than Logger. You may inject whatever you want by simply following the above technic.
Feel free to comment.