Archive for the ‘Logback’ Category

Using Logback With Spring MVC

January 7th, 2014 No comments

Logback is yet another logging framework intended to be successor of the widely used Log4J. I came across Logback several years ago but it never paid a whole lot of attention to it. Now I am convinced that Logback and SLF4J is the way logging should be done in applications.

So, what’s so great about Logback. The Logback site lists several reasons why Logback is superior. Here are my three favorite reasons:

  • Logback is faster than Log4J – Logging is a cross cutting concern and ideally shouldn’t have any impact on the application’s performance. But with the number of log statements that enterprise applications contain these days, this is not true. So the faster the logging framework, the better in my opinion.
  • Stack traces contain packaging data – Here is an example stack trace logged using Logback:

    java.lang.RuntimeException: null
    at com.inflinx.spring4.web.controller.HomeController.home( ~[HomeController.class:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.7.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.7.0_45]
    at java.lang.reflect.Method.invoke( ~[na:1.7.0_45]
    at ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at ~[spring-web-4.0.0.RELEASE.jar:4.0.0.RELEASE]

    Notice the packaging information at the end of each line inside [] brackets. It definitely simplifies troubleshooting issues.

  • Condensed package names – It is possible to instruct Logback to generate condensed package names in the logs. With this feature turned on, you will see o.s.w.servlet.DispatcherServlet in the logs instead of org.springframework.web.servlet.DispatcherServlet.

    If you like this feature and want to use it in Log4J, check out EnhancedPatternLayout

Though, you can directly use Logback in your application, it usually make sense to use a logging facade like SLF4J.

Now that I have convinced you that Logback is a better option, let’s see what it takes to use it. If we are using Maven, using Logback simply involves declaring the following dependencies:

The next step is to create logback.xml file under src/main/resources and logback-test.xml file under src/test/resources. Logback first looks for logback-test.xml file in classpath. If it doesn’t find it, it tries to look for logback.xml file. This makes it easy to have different configurations for test and production.

The logback.xml file is similar to log4j.xml file that many of us are familiar with. Here is a sample logback.xml file that you can use:

It is possible for Logback to automatically scan for configuration modifications and reload its configuration. The scan and scanPeriod attributes can be used to set the reload information. This is done using scan and scanPeriod attributes in the configuration element. The layout pattern used above will produce logs like this:

12:53:34.374 [localhost-startStop-1] DEBUG o.s.c.e.PropertySourcesPropertyResolver – Searching for key ‘spring.liveBeansView.mbeanDomain’ in [systemProperties]

Now we are all set to use Logback. Here is an example from the Spring 4 starter web project that demo’s Logback usage:

Categories: Logback Tags: ,