Using Logback with Weld

DZone 's Guide to

Using Logback with Weld

· Java Zone ·
Free Resource

The Weld stuff all comes with the jboss logging stuff woven into it. And the fun part is, that of course, since the logging is not part of a standard, you have to bring in another dependency (solder) to make it work. Then there's more fun. It declares jboss-logging as provided, which is apparently wrong. After pissing away way, way, way too much time trying to get this all working in my weld project with Arquillian tests, I decided I wanted to use logback. Thankfully, that went pretty quickly and painlessly.

First thing to do is add the logback-core, logback-classic and slf4j-api dependencies to your pom. Then make a class with a Produces annotated method comme ça:

package com.ontometrics.logging;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

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

public class LoggerFactoryProducer {
    public Logger produceLog(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass());

Of course, you need an annotation for the logger qualifier that identifies this as yours. I had the jboss logging working, but there were multiple producers and I was getting the warning on the injections. I was trying to get down to zero warnings when I found that the whole little cardhouse was just barely upright. Actually, eclipse (or jboss tools) was lopsided on this account too, because I got to where it was satisfied that it could figure out which logger to inject on 3/4 of my files, but 2 or 3 still gave the warning, though they were identically configured.

Next step was to just go around injecting the logger. The other good thing in this was there was no need to do anything to shrinkwrap. All works now. Also, you can pass a logback-test.xml config file to shrinkwrap. I don't like going to my jboss install to configure the logger, and I couldn't get it to show me debug messages, for whatever loopy reason.

From http://www.jroller.com/robwilliams/entry/using_logback_with_weld


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}