Manage logging with Logback and Apache Tomcat

This article will help the people who wants to abstract the logging part of their web application in the servlet container Apache Tomcat. We will put all the required libraries in the libraries of Tomcat and tweak a few configurations files in order to deliver a WAR file without logging libraries (you may still need log4j-over-slf4j though).

Required libraries

You can retrieve all the last versions of the required libraries using the following links :

Installation procedure

  • Install the libraries into the ${CATALINA_HOME}/lib folder
$ sudo mv slf4j-api-*.jar $CATALINA_HOME/lib/
$ sudo mv jul-to-slf4j-*.jar $CATALINA_HOME/lib/
$ sudo mv logback-classic-*.jar $CATALINA_HOME/lib/
$ sudo mv logback-core-*.jar $CATALINA_HOME/lib/
$ sudo chown -R tomcat:tomcat $CATALINA_HOME/lib
  • Update the file ${CATALINA_HOME}/conf/logging.properties 

You need to replace the content of the logging.properties file.

$ sudo cp $CATALINA_HOME/conf/logging.properties $CATALINA_HOME/conf/logging.properties-bak
$ echo "handlers = org.slf4j.bridge.SLF4JBridgeHandler" | sudo tee -a $CATALINA_HOME/conf/logging.properties
$ sudo chown tomcat:tomcat $CATALINA_HOME/conf/logging.properties
$ sudo chmod 600 $CATALINA_HOME/conf/logging.properties
  • Create a logback configuration folder in ${CATALINA_HOME}/conf

Next step is to create a folder for the logback configuration.

$ sudo mkdir $CATALINA_HOME/conf/logback
$ sudo chown tomcat:tomcat $CATALINA_HOME/conf/logback

You can now add your logback.xml file into that folder.

  • Create a setenv.sh file

The file needs to have the following content:

CLASSPATH=$CATALINA_HOME/lib/jul-to-slf4j-VERSION.jar:$CATALINA_HOME/lib/slf4j-api-VERSION.jar:$CATALINA_HOME/lib/logback-classic-VERSION.jar:$CATALINA_HOME/lib/logback-core-VERSION.jar:$CATALINA_HOME/conf/logback/

Then put this file into the $CATALINA_HOME/bin folder.

$ sudo mv setenv.sh $CATALINA_HOME/bin
$ sudo chown tomcat:tomcat $CATALINA_HOME/bin/setenv.sh
$ sudo chmod 755 $CATALINA_HOME/bin/setenv.sh
  • Restart the Tomcat server

Now you just need to restart the server and voilà ! The logback.xml file located in $CATALINA_HOME/conf/logback will be loaded.

Advertisements

8 thoughts on “Manage logging with Logback and Apache Tomcat

  1. Are jul, logback classic and logback core required for ALL implementations? I am trying to get slf4j installed with log4j2, and I’m unsure of the need
    Should I download them, as I’ve only grabbed slf4j?

    Regards,

      1. Thanks for your help but, I failed! Partly due to redundant links to Tomcat 8.0.5, when I was attempting this for Tomcat 8.0.8, I basically trashed my VM server!
        I am starting again and have found that I can install Tomcat 8.0.8 without the pain of building it and such. Now I am hoping that I can ‘pre-install’ logging and SLF4J in Mavericks, making the process more ‘visible’.
        I find one 2011 blog (http://jbrisbin.wordpress.com/2010/04/20/change-logging-package-to-slf4jlog4j-in-tcservertomcat/comment-page-1/#comment-63). They imply that jul isn’t mandatory and, by building without it, I wouldn’t need SLF4JBridgeHandler either.
        Before I wreck another VM, I would really appreciate your thoughts!

      2. Hi DVious, I’m sorry but I can’t help on you this subject as I’ve never built Tomcat from sources nor played with the version 8.

        I guess you’ll have to try this yourself. Btw, the article you linked describe the process for Tomcat 6, it may be out of date.

        Also, I’d like to hear from your experiments. Good luck.

  2. Thanks for replying, all the same.

    I have put some info together and will be attempting to build from souce, exculding juli and using Log4j2, in the next few days.

    I’ll let you know – might even publish my first blog on the topic!

    Regards,

  3. Hi,
    thanks for the article! I know that access log is handled by Valve. Does the solution above apply to access log as well?

    Cheers!

  4. Hey James,

    Nope, this solution will only setup the logging libraries into Tomcat, these libraries will be used by your web applications for logging.

    You still need to manage Tomcat logs using the standard way (logging.properties IIRC).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s