Sunday, July 22, 2012

How to create a different logback configuration for each web application under Tomcat


I was looking at a particularly peculiar situation today as I was trying to setup logback for a web application. I was going for maximum customization with my logging settings, and as such I set two targets for my configuration:
  1) I wanted my logback.xml to be placed outside the WAR archive, so that it could be modified on the fly without a need for repackaging
  2) I wanted to be able to have a different logging configuration file for each application that I had deployed under Tomcat
Since I spent quite a bit of time trying to get this to work, I figured it would be a good idea to share this information, maybe it can save some time for others.

Now the first point I was trying to solve wasn't very difficult to tackle, since logback will look for its configuration file on the classpath. Therefore dumping a file named logback.xml into the Tomcat lib folder was all it took to have the configuration outside the WAR file. The catch here is that we now have a logback.xml file on the classpath of all the applications currently deployed in Tomcat, so we are influencing all of them with this configuration.

The next step is to configure logback to use a different configuration file for each application. We will be relying on JNDI to achieve this. Firstly we want to add the following line to catalina.sh, under tomcat/bin:


This line will tell logback to select the logger context based on data available via JNDI lookup. Now we need to set a different JNDI value for each application. This is done by adding to web.xml the following piece:


With these modifications logback will look for a file named logback-appName.xml for configuration. This means that under tomcat/lib we will now have files such as: logback-app1.xml, logback-app2.xml etc. For my initial goals this was sufficient. However, I didn't like having all those different xml files for different applications bundled together in the tomcat/lib folder with the jars, so I decided that I want to create a separate folder for each application's logback configuration, so that my lib would look like this:


Fortunately, it turned out that all I needed to make this happen was another JNDI variable, that I added in web.xml:


Finally everything was to my liking, after setting logback to put the log files to ${catalina.base}/logs everything was ready to be pushed to the repo. In case more details are needed, the logback documentation that talks about this functionality can be found here: http://logback.qos.ch/manual/loggingSeparation.html

1 comment:

  1. Have this working except the logback/configuration-resource version, which cannot find the logback.xml from a subfolder of ${catalina.base}/lib.

    Don't understand why, unless there's extra tomcat config needed? The logback manual (http://logback.qos.ch/manual/loggingSeparation.html) suggests I can pick config up from subfolders of WEB-INF but I want to exernalise the config from the war...

    Any suggestions?

    ReplyDelete