Introducing JmxLogger: Realtime Log Monitoring with JMX

DZone 's Guide to

Introducing JmxLogger: Realtime Log Monitoring with JMX

· Performance Zone ·
Free Resource

JmxLogger makes it easy to monitor application log events in realtime using JMX.  You can use the JmxLogger API's Log4J Appender or the Java Logging Handler to broadcast your application logs as JMX notifications. Using the familiar configuration mechanism (i.e. log4j.xml or Java Logging properties file) of your favorite logging framework, developers can quickly integrate realtime application log monitoring into their existing without any code changes. All it takes is an entry in your logging configuration and you're done.

See project site at http://code.google.com/p/jmx-logger/ 


  • Support for Java Util Logging API
  • Support for the Log4J logging API
  • Easy integration with your favorite logging framework
  • No coding required, simply configure your logging framework to get started.
  • Specify and control the level of event to broadcast
  • Monitor event through a console or listener scripts

Getting Started

  • Download the jar (generic or with log4j support)
  • Add jar to your classpath
  • Configure your favorite logging framework and you set
  • Use a JMX Console (JConsole or VisualVM) to monitor your application

Sample Code - Log4J

For this write up, we'll use the following code to demonstrate how JmxLogger can be used with Log4J.  The code uses Log4J's Logger API to log a random event every 2 seconds.

public class Log4JAgent {    private static final Map<Level,String> logs =  new HashMap<Level,String>();    static {        logs.put(Level.INFO, "I am happy!");        logs.put(Level.WARN, "I am concerned...");        logs.put(Level.ERROR, "I am in trouble, something went wrong.");        logs.put(Level.DEBUG, "I am up, I am down, I am all around!");    }    public static void main(String[] args) throws InterruptedException{        Logger logger = Logger.getLogger(LoggingUtilAgent.class);        int next = 0;        for(;;){            Map.Entry<Level,String> entry = (Entry<Level, String>) logs.entrySet().toArray()[next];            logger.log(entry.getKey(), entry.getValue());            next = new Random().nextInt(logs.size());            Thread.currentThread().sleep(2000);        }    }}

Configure Log4J

The next code snippet shows how we configure JmxLogger's Log4J Appender to intercept application logs and broadcast them as JMX event notifications.  Notice that the configuration also has Console appender, so you will see the event on the console as well.

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  <appender name="console" class="org.apache.log4j.ConsoleAppender">    <param name="Target" value="System.out"/>    <layout class="org.apache.log4j.PatternLayout">      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>    </layout>  </appender>  <appender name="jmxlogger" class="jmxlogger.integration.log4j.JmxLogAppender">    <param name="ObjectName" value="jmxlogger.log4j:type=Log4jAppender"/>    <param name="ServerSelection" value="platform"/>    <param name="LogPattern" value="Exception"/>    <layout class="org.apache.log4j.PatternLayout">      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>    </layout>  </appender>  <root>    <priority value ="DEBUG" />    <appender-ref ref="console" />    <appender-ref ref="jmxlogger" />  </root></log4j:configuration>

As the application runs, log activities are sent to both the console and are also sent to JMX as management notification events.


Console showing log activities for application.

Console showing log4J logs


As the application runs, log activities as are also sent to JMX MBeanServer as event notifications.  These notifications can be viewed in a JMX management console such as JConsole as shown below.

JConsole showing event notifications


Logging is an integral and critical part of all enterprise applications.  Combining application activity logging and JMX can provide a realtime view into your application at runtime.  The JmxLogger API facilitates the monitoring of your application's activities using logging frameworks that you are familiar with, mainly, Log4J and the built-in Java Logging framework.  

If you are interested in ginving JmxLogger a try, go to http://code.google.com/p/jmx-logger/

java ,jmx ,management

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}