Over a million developers have joined DZone.

Using Logback with Weld

DZone's Guide to

Using Logback with Weld

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}