Oliver Drotbohm Archive About Tags

Spring, logging and JMX

October 1st, 2010

Pretty much every application out in the wild is using logging to some extend. As applications tend to have bugs (yes, yours as well) it’s a common requirement to alter log levels at runtime. As JMX (Java Management Extensions) is a standard to monitor and manage applications it seems to be a reasonable approach to expose the loggers used in an application via JMX to allow tuning of log levels via a JMX client like jconsole. So let’s take a look on how you enable JMX with Spring.

<context:mbean-server />
<context:mbean-export />

The first element from the context namespace sets up a JMX server so that MBeans can be registered. The second element activates automatic export of all Spring beans the either leverage Spring JMX support (annotation based) or Spring beans that satisfy the JMX naming scheme (a class implementing an interface named ${classname}MBean). Fortunately Log4J ships with a LoggerDynamicMBean that exports a Logger instance to the JMX infrastructure. It’s API is not really built with dependency injection in mind but due to the flexibility of the Spring XML configuration language we could register this MBean as follows:

<bean class="org.apache.log4j.jmx.LoggerDynamicMBean">
  <constructor-arg>
    <bean class="org.apache.log4j.Logger" factory-method="getLogger">
      <constructor-arg value="org.springframework.jmx" />
    </bean>
  </constructor-arg>
</bean>

Running this in combination with the XML snippet above now exposes the logger for org.springframework.jmx as JMX MBean.

XML Hell? Go make it clear and nice!

Declaring the MBean for a single logger requires quite an amount of XML and is boilerplate to a large degree. The two things that are unique in this case is that we want to expose a Log4J logger and the logger name of org.springframework.jmx. So what if we could expose the logger with something like this:

<jmx:logger type="Log4J" name="org.springframework.jmx" />

We can! I’ve created a small sample project project containing a custom JMX namespace and put it up on Github. The README file contains instructions how to play around with the Sample class, altering the log level via jconsole and see results in the console output. Right now it’s pretty much just a proof of concept and could be improved in a few corners. We should support other logging libraries and we could transparently register and whcih is not the case currently. Feel free to suggest or add extensions!

blog comments powered by Disqus