RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Set the Logging Level Over a Tomcat Cluster Dynamically : Page 4

Rather than modifying your Log4J configuration and then redeploying the correct code version with the modified logging level, you can dynamically set the Log4J logging level over each node in a Tomcat cluster with JMX.


Receiving the Message in the Other Nodes

Once you are able to send these messages to the other nodes in the cluster, you will need to implement a Tomcat ClusterListener to receive the events. This implementation will set the logging level for the correct MBean determined by the application name that is passed as part of the message:

public class ClusterLoggingListener extends ClusterListener {

    private static final Log log = LogFactory.getLog(ClusterLoggingListener.class);
    public void messageReceived(ClusterMessage message) {
        if(message instanceof LogLevelMessage) {
            LogLevelMessage logMessage = (LogLevelMessage) message;
            List list = MBeanServerFactory.findMBeanServer(null);
            MBeanServer server  = (MBeanServer)list.iterator().next();
            Hashtable<String, String> nameParameters = new Hashtable<String, String>();
            nameParameters.put("logger", "root");
            try {
                ObjectName logBean = new ObjectName(logMessage.getApplication(), nameParameters);
                Attribute attribute = new Attribute("priority", logMessage.getLoggingLevel());
                server.setAttribute(logBean, attribute);
            } catch (Exception e) {
                log.error("Problem setting the logging level for application " + 
                        logMessage.getApplication() + " to level " + 
    public boolean accept(ClusterMessage message) {
        return message instanceof LogLevelMessage;

You must deploy this class in the server directory and enter it in the Tomcat server.xml file as a ClusterListener within the <Cluster> element:

<ClusterListener className="dynamiclogging.tomcat.ClusterLoggingListener"/>

You should now be able to set the logging level for any application, over all nodes, in the cluster by calling SetLog4JLevelServlet on any node in the cluster.

Deploying the Example Code

To deploy the example code for setting the logging level in your cluster, take the following steps:
  1. Load the Eclipse project and build it using the dist target. This will create set-log-levels.war and set-log-levels-server.jar in the dist directory.
  2. Put the set-log-levels-server.jar in the Tomcat server\lib directory and deploy the set-log-levels.war file across the cluster.
  3. Use the SetLog4jLevelServlet (e.g., /setLogLevel?level=debug) to set the logging level and use PerformLogTestServlet (/testLogLevel) to test the logging level changes.

Jon Dickinson is the principal consultant at Accolade Consulting. He focuses on helping organizations improve the software they deliver through agile development techniques.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date