My name is Vladimir Vivien software engineer in the Tampa Bay area (US). I run the Tampa Java User Group (Tampa, FL) and have a wide range of technology interests including Java, Groovy/Grails, JavaFx, SunSPOT, BugLabs, component-based design, API design, and general ubiquity of the Java platform. Vladimir has posted 8 posts at DZone. View Full User Profile

Introducing JmxLogger: Realtime Log Monitoring with JMX

04.15.2009
| 13887 views |
  • submit to reddit

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/ 

Features 

  • 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

Console showing log activities for application.

Console showing log4J logs

JConsole

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

Conclusion

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/

Published at DZone with permission of its author, Vladimir Vivien.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Craig Doremus replied on Wed, 2009/04/15 - 1:15pm

Can you dynamically change logging levels using this library and the JMX Console?

Vladimir Vivien replied on Wed, 2009/04/15 - 5:32pm in response to: Craig Doremus

Craig,

No you can't change log level yet (using JMX). However, you can do that now if you are using the Java Logging API.  Starting with JDK 5,there's a LoggingMXBean (http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/LoggingMXBean.html) exposed on all platform MBeanServer.

Liam Knox replied on Thu, 2009/04/16 - 5:31pm

We have been using logging hosted for JMX via the HTTP Clients for about 6 or 7 years now based on simple in memory loggers. How does this handle stack trace display , I guess you would drill down? I still think a lot of aspects such as complex types, logs etc you have far more flexibility in html display. These CompositeData constructs display and exposure is still extremely intrusive, expensive and clunky.

Vladimir Vivien replied on Sat, 2009/04/18 - 8:45am in response to: Liam Knox

Liam,

Thanks for the feedback.  JmxLogger doesn't provide any display capabilities, it simply aggregates app logs as JMX event notifications.  You could, for instance, write a JMX listener that listens for log notifications and format the event however you want (HTML included).  JmxLogger does capture the entire log object from the logging api (including any throwable).

ashwyn tirkey replied on Thu, 2009/08/06 - 5:14am

I want an audit trail of system properties that are shown on JConsole using JMX .This info i want to be persisted to DB.How can i go about doing that?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.