Logging stuff in enterprise applications is really a tough task. Actually, logging introduces a paradox: on one hand logging is really critical when something went wrong ; on the other hand, nobody cares about logging when everything is just fine. We, as developers, have to really take that into account.

Honestly, choosing a logging framework in Java is an easy task: log4j is a reasonable choice as it is widely used. The only advice that I have in mind is to take attention to resource consumption, especially if you are logging to several files.

Let's say that you have found a critical bug in production and you need more logging on a particular piece of code. You need to be able to act on your logging configuration live, right? Well, all frameworks have their own Log4jService: JBoss, Spring, Geronimo, whatever. What I am proposing you is a very simple logging service that you could deploy in any environment, even as an MBean if you want. The interface of this service is really basic:

JAVA:
  1. public interface Log4jServiceMBean {
  2.  
  3.     String getLoggerLevel(String loggerName);
  4.  
  5.     void setLoggerLevel(String loggerName, String levelName)
  6.             throws IllegalArgumentException;
  7.  
  8.     void reconfigure(String configurationUrl)
  9.             throws java.io.IOException, java.net.MalformedURLException;
  10.  
  11.     String dumpConfiguredLoggers();
  12.  
  13.     Map getLoggingConfiguration();
  14. }

If you know me, you would be surprised to see that the interface is lacking of Javadoc. Well it has, I just remove it to improve readability :) . The reason why the interface is named MBean is to allow the service to be deployed as an MBean.

The first method is really simple and allows you to know the level (e.g. ERROR, DEBUG, etc) of a given logger. The second one is more interesting since it allows you to change the level of a logger live! This answer our use case above. Two methods allow you to see the logging configuration.

The last one is nice since you can reconfigure entierly your logging configuration by passing an url to another log4j configuration file. It could be:

Note that the last case is home-made. Whenever the url starts with classpath: it is handled as a reference in the classpath. If you have Tiger (Java 1.5), you can register this service in your application as follows:

JAVA:
  1. Log4jService log4jService = new Log4jService();
  2. MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
  3. serviceObjectName = new ObjectName("nicoll.net:service=Logging");
  4. mBeanServer.registerMBean(log4jService, serviceObjectName);

The code is released as LGPL so feel free to use it.