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

Realtime Log Monitoring using JmxLogger

02.11.2010
| 6580 views |
  • submit to reddit

In this latest release of JmxLogger (version 0.3.0 - http://code.google.com/p/jmx-logger/), many features have been added including a built-in console to view logs as they are streamed across the network.  JmxLogger allows to see your Java application logs in realtime without having to start a ssh session and "tail -f server.log" to see what is going on with your application.

Features

  • Easy integration with your favorite logging framework
  • Support for Java Util Logging API
  • Support for the Log4J logging API
  •    Works with your app servers (GlassFish, JBoss, Tomcat, etc)
  • No additional API coding required, configure your logging framework and that's it
  • Leverages JMX client/agent infrastructure for logging/monitoring
  • Support one or more logger running within same VM
  • design for performance / scalability
    • dedicated log worker threads
    • logged messages are quickly dispatched to dedicated work queue
    • all log filtering occur on dedicated threads
  • Specify and control the your log levels
  • Set log filter at agent-side to save network chatter
  • Expression language support for complex event filtering
  • Set log filter expression using
    • log event parameters (level, message, logger name, etc)
    • log event statistics (log count, logger count, etc)
    • system status (avail memory, runtime, thread count, etc)
  • Multi-function JmxLogger GUI console to view logs
    • Say bye to "tail -f server.log": monitor application log locally/remotely
    • ability to connect, pause, resume JmxLoger connection
    • control JmxLogger log settings remotely
    • change/update log levels directly from console
    • change/update log filter expression
    • color-coded log mesage based on level for readability
    • consolidated message console for ease of use
    • support secure/non-secure connection using JMX security settings
  • Usage of JmxLogger is transparent to the developer.
    • In your code, send your log events using your logging framework
    • The JmxLogger (appender or handler class) then delegates propagation of your log events as JMX notifications using the JMX API.

Configure JmxLogger for Java Util Logging

If you are a user of the Java util Logging API, you should already be familiar with how to configure (declaratively or programmatically) the framwork for logging. The JmxLogger handler is configured like any other Java Logging handler:

handlers=jmxlogger.integration.logutil.JmxLogHandler, java.util.logging.ConsoleHandler

# Default global logging level.
.level=INFO

# jmx log handler
jmxlogger.Handler.level=INFO
jmxlogger.Handler.objectName=jmxlogger:type=LogEmitter

# Console log handler
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
jmxlogger.Handler.objectName=jmxlogger:type=LogEmitter

Configure JmxLogger for Log4J

JmxLogger also supports the Log4J logging framework. If you are a user of the Log4J framework, you should already be familiar with the configuration steps for Log4J appenders. The JmxLogger appender is configured as shown below:

<?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="Threshold" value="INFO"/>
<param name="ObjectName" value="jmxlogger:type=LogEmitter"/>
<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>

 Run Application

You must provide several system properties to start your process with JMX remote connectivity & security enabled. For this example, we are only going to enable the remote port and turn off security

java -cp your:class:path \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=7070 \
-Dcom.sun.management.jmxremote.authenticate=false \
your.process.main.Class

When you start the process, it will setup JMX with remote connectivity. This will enable you to connect and see your log using the JmxLogger console (see below).

 Run the JmxLogger Console

Once you have started your process, you can view the log in realtime by connecting to the JmxLogger agent started by the logging framework. To start JmxLogger, do

java -cp mvel2-mvel2-2.0.14.jar:jmxlogger-0.3.0.jar \
jmxlogger.tools.console.Main

When you start the console, you connect to the remote process:

 

 After a successful connection, JmxLogger will start receiving log statements from the remote JmxLogger agent embedded in the remote process:

 

 Expression Filter

As mentioned, you can also use a filter expressions (using MVEL) to narrow down which log messages you want to receive. In the following screen, the logs are filtered where the raw message contains the string "something went wrong":

 

JBoss Log Monitoring with JmxLogger

You can setup JBoss to work with JmxLogger to monitor JBoss logs remotely on the network.  This involves  4 steps:

  1. Modify {JBOSS_HOME}/bin/run.conf to add JMX remote security/connectivity command-line properties.
  2. Motify {JBOSS_HOME}/{JBOSS_INSTANCE}/conf/jboss-log4j.xml and add the JmxLogger appender.
  3. Add JmxLogger dependencies to {JBOSS_HOME}/{JBOSS_INSTANCE}/lib.
  4. Restart JBoss.

Start the JmxLogger console, connect to the JmxLogger agent, if everything is configured propery, you should start see the log statements from JBoss:

JmxLogger also works with GlassFish and Tomcat.  Visit the project's web site for more documentation.

Get JmxLogger

Checkout JmxLogger and documentation on how to setup JmxLogger with your application and app server at http://code.google.com/p/jmx-logger/ 

0
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.)