vADC Blog

Apache Commons Logging (08/07/2009)

by riverbed on ‎07-02-2012 01:45 PM (291 Views)

Apache Commons Logging (08/07/2009)

This post is for anyone using Java extensions that make use of libraries that use org.apache.commons.logging (Apache Commons Logging). By default this logging infrastructure will use Log4J if it is available, otherwise it falls back to its own SimpleLog logging implementation. This is all very well, but as neither of these are aware of Stingray’s log levels and logging infrastructure any messages that come out will show up as scary WARN events in Stingray’s Event Log.


One way to suppress the warning events is to install the NoOp logger provided with the library.

  1. System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

If you put this code in a servlet's init method all log messages from underlying users of org.apache.commons.logging will be suppressed. You could make suppressing the log messages configurable using a servlet parameter, this means it is easy to enable them for debugging purposes.

  1. if (getInitParameter("debug") != null) {
  2.    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
  3. }

To go one up on this approach we have written a class that implements the org.apache.commons.logging.Log interface. Our implementation will map log messages to the Stingray Event Log levels appropriately.

The javac invocation used to compile the class file from the code is given below. Note that you need to obtain the file from your Stingray Traffic Manager, and the other file from the an Apache Commons Logging binary download.

javac -cp

To set this up to take effect for libraries used by your servlet you must upload the .class file to your Java Libraries & Data Catalog (or put it in a .jar file you upload.) Then change the code above to tell the logging system to use our logger:

  1. System.setProperty("org.apache.commons.logging.Log", "ZeusServletLogger");

The ZeusServletLogger class itself will examine the system property "ZeusServletLogger.level" which should be set to a string value of one of: "fatal", "error", "warn", "info", "debug", or "trace". This controls which messages are pushed to the Event Log, only messages at the specified level or higher (fatal is highest, trace lowest) will be sent to the Stingray logging system. Note that "trace" and "debug" messages both go to the Event Log as "debug". By default all messages are sent to the Stingray logging system and Stingray’s log level configuration will determine which ones make it to the Event Log. Our final sample snippet of servlet init method code suppresses "info" (and below) messages unless the servlet is passed a "debug" parameter.

  1. System.setProperty("org.apache.commons.logging.Log", "ZeusServletLogger");
  2. if (getInitParameter("debug") == null) {
  3.    System.setProperty("ZeusServletLogger.level", "warn");
  4. }

The Event Log screenshot below was generated performing the same actions that generated the screenshot at the top of this article. The difference is that this time it has been passed through our ZeusServletLogger with the log level set to "debug". Now we can easily see that there is a real warning message in the log!