Logback and webapplications

Lately I prefer to use Logback for logging purposes. Logback natively supports slf4j, a log abstraction api through other logging frameworks all can log to a logging framework of your choice.
When using logback in webapplication and you want your logging configuration file outside of your app you can set a systemproperty to point to the location.
However if you want it configured like you are used to do with Spring, log4j and the log4j servletcontext listener, you have to write the context listener yourself.
It’s a bit silly to do that again for every webapplication. So here it is, the logback-webfragment.
The logback-webfragment is a Servlet 3.x webfragment configered to be loaded before other fragments. It takes a location from a web.xml configparameter and loads the configuration from there. Optionally you can add another context parameter to enable one of the 5 default configurations I’ve added.
The idea for the code of the listener is taken from this ticket in logbacks issuetracker. Currently logback-webfragment will only work for Java 1.7 and higher. The maven artifact name is named aaa-logback-webfragment. The aaa is there to increase the chance that the webfragment is loaded before others that are also configured to be loaded first.
In analogy to logback itself the webfragments license is EPL. It is not yet in maven central repo. Maybe I have to test it a bit more.

4 Replies to “Logback and webapplications”

  1. Hi Ceki Thanks, yes we use both MDC and NDC for different tinghs. I know latest versions of Slf4j now have MDC and we love it. I’ll write an entry later about why we love both MDC and NDC.

  2. that might be good idea, as it is my intention to follow the same license(s) as logback for this.
    Currently fixing a java6 compatible version too, i’ll add the licensing stuff in the same push.

  3. Very nice thanks.
    About the license though, would it be possible to follow logback all the way and use a dual license? Otherwise using in applications which contain a GPL family license (e.g. JBoss) is not allowed.

    Thanks.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: