taming webapp logging with log4j

Logging is an aspect of programming that should follow some simple rules. If I should describe logging for a dictionary it would be something like: “Providing essential information with object introspection at a decent level of output that enables you to look into a running application or providing runtime information for debugging.”

But even the simplest thing can be complicated if you use Java…

There are different logging frameworks like “log4j”:http://logging.apache.org/log4j/1.2/manual.html, “commons logging”:http://commons.apache.org/logging/ or “logback”:http://logback.qos.ch/ even though Java includes it’s own “java.util.logging”:http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/ API. To make it even worse, the Java community came up with a “simple logging facade”:http://www.slf4j.org/ so one can use the logging framework of choice but implement against the facade – WTF?!

h2. a standard that sucks

I would guess that 99% of Java code is written against log4j. Nobody really want’s to switch a logging framework during implementation and most developers I know see log4j as a technical standard.

I won’t recommend using log4j to anyone. The log4j documentation is crap! There is “no usefull literature”:http://www.amazon.de/Pro-Apache-Log4j-Samudra-Gupta/dp/1590594991/ref=sr_1_1?ie=UTF8&s=books-intl-de&qid=1270486062&sr=8-1 either (I never thought that one would have to buy a book for logging…). The framework misses essential things like pattern matching filters (you will have to use “log4j companions”:http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/filter/ExpressionFilter.html) and it is difficult to configure, especially on a “tomcat”:http://tomcat.apache.org/tomcat-5.5-doc/logging.html.

Log4j is a bitch when it comes to deployment. One has to be careful where to put the log4j jar(s) and xml or plain text configuration files. If you do it wrong you end up with broken logging behavior; webapps log to the wrong log file, spit out stuff to the system out or stop logging at all.

h2. this is how we do it

Our Java infrastructure is service oriented. We have a bunch of webservices hosted on serveral distributed tomcats and all wars depend on log4j for logging. We want to reload the logging configuration files at runtime (why is “jboss”:http://www.jboss.com/products/platforms/application/ able to do it and tomcat is not?!) and manipulate it via “JMX”:http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/.

h3. additional log4j configuration

To achieve this, we have to put some additional configuration into every webapp. Log4j comes with a watchdog that looks for changes in the configuration files. The functionality can be enabled by using the “configureAndWatch() method”:http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html#configureAndWatch%28java.lang.String%29. There is a precanned “Spring solution”:http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/util/Log4jConfigListener.html for this, but it has configuration overhead and environmental restrictions. So the best place to implement it ourselves is in a custom context listener:


   [...]
	
	
		de.nofail.TomcatLoggingListener
	
   [...]
   [...]
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		try {
			String log4jFile = String.format("%s-log4j.xml", servletContext.getContextPath());
			String configFilename = new File(getClass().getResource(log4jFile).toURI()).getAbsolutePath();
			DOMConfigurator.configureAndWatch(configFilename, 1000);
		} catch (URISyntaxException e) {
			throw new IllegalStateException("Error resolving log4j configuration file for context=" + servletContext, e);
		}
	}
   [...]

This listener will use a config file named _WEBAPP-log4j.xml_ lying in the root directory. The goal is, that every webapp has it’s own logging context. Every war should include a log4j.jar so that logging won’t be affected by other apps, the “classloader hierarchie”:http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html will ensure this. The different xml configuration files can than be placed in a “shared tomcat directory”:http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html for easy access of an operations team (which is probably yourself).

Using different configuration files has the advantage that you don’t have to mess around with appenders based on packages. That’s how one can configure different log levels for the same packages in each application, very usefull if you release a new application!

h3. Java Management Extension

There are different ways to integrate JMX functionality into a webapp. The simplest approach is to use another servlet listener for propagating an mbean:


   [...]
	
	
		de.nofail.JMXListener
	
   [...]
   [...]
	public void contextInitialized(final ServletContextEvent sce) {
		try {
			TomcatLogging mbean = new TomcatLogging();
			ObjectName clutter = new ObjectName("de.nofail:type=TomcatLogging");
			ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, clutter);
		} catch (Exception e) {
			throw new IllegalStateException("Could not create JMX context: " + e.getMessage(), e);
		}
	}
   [...]

If you are using “Spring”:http://www.springsource.com/ it’s easy to add “JMX support via annotations”:http://static.springsource.org/spring/docs/2.0.x/reference/jmx.html.

Now you can access your running webapp via jconsole:

h2. more information?

You can have a look at a “working example”:http://github.com/phoet/tomcat-logging “based on Maven”:http://blog.nofail.de/2010/03/creating-a-maven-webapp-from-scratch/ on github.

Drink a cup of Java(TM)(c)(r), but don’t forget the sugar!

6 thoughts on “taming webapp logging with log4j

  1. Pingback: Tweets die taming webapp logging with log4j | #nofail erwähnt -- Topsy.com

  2. Helen Neely

    Nice article. You have shown some of the seldom used feature of Log4j…
    In fact, don’t listen to Michał Mech, Log4j still rocks.

    Keep it up!

  3. dennis

    Remember that servletContext.getContextPath() returns an empty string for apps running in ROOT Context ( / ), config files need to be named “-log4j.xml”…

Comments are closed.