My favorites | Sign in
Project Logo
                
People details
Project owners:
  vladimir.vivien
Project committers:
harel.eran, rj.salicco

JmxLogger

JmxLogger makes it easy to broadcast your Java Util Logging or your Log4J log events as JMX notifications. The JmxLogger API provides a Java Util Logging Handler and a Log4J Appender which provides integration points between your favorite logging framework and JMX. JmxLogger hides the complexity of dealing with JMX. You simply configure your logging framework as you normally do, and that's it. You are ready to broadcast your logging events as JMX notification events.

Features

Getting Started

Java Logging Handler

If you are a user of the Java util Logging API, you should already be familiar on how to configure (declaratively or programmatically) the framwork for logging. Below is a sample of what your configuration file would look like when configuring JmxLogger's Handler class.

The handler is configured like any other Java Logging handler. Notice that the handler supports several attributes which can be specified in the logging properties file. Missing attributes are automatically assigned a default value. So, you can have a blank configuration if you wanted to (as long as the Handler is declared).

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

# Default global logging level.
.level=INFO

# jmx log handler
jmxlogger.Handler.level=INFO
jmxlogger.Handler.filter=
jmxlogger.Handler.logPattern=
jmxlogger.Handler.formatter=java.util.logging.SimpleFormatter
jmxlogger.Handler.objectName=jmxlogger.util.logging:type=JmxLogHandler
jmxlogger.Handler.server=platform

# Console log handler
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

Java Logging Example

Once you have your handler configured, you simply use the logging API as you normally do. Usage of JmxLogger will be transparent to the developer. You simply send your log event to your logging framework. The framework then delegates propagation of your log event as JMX notifications using the JmxLogger API.

import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class SomeClassA {
    private static final Logger logger = Logger.getLogger(SomeClassA.class.getName());
    public static void main(String[] args) {
        logger.log(Level.INFO, "I am happy!");
        logger.log(Level.WARNING, "I am concerned...");
        logger.log(Level.SEVERE, "I am in trouble, something went wrong.");
        logger.log(Level.FINE, "I am up, I am down, I am all around!");
    }
}

For detail on how to use Java Util Logging API, see Java Logging Overview.

Log4J Appender

Similarly to Java Logging, JmxLogger supports the Log4J framework for logging. If you are a user of the Log4J framework, you should already be familiar with the configuration steps for the Log4J logger.

Below is a sample log4j.xml configuration file. It shows how the JmxLogger Appender is configured for logging. The appender supports several attributes which can be left blank. The class defaults to sensible defaults if an attribute is missing.

<?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>

Log4J Example

Once you have JmxLogger configured (for Java Logging or Log4J), you are ready to use it. Usage of JmxLogger will be transparent to the developer. You simply send your log event to your logging framework. The framework then delegates propagation of your log event as JMX notifications using the JmxLogger API.

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class SomeClassB {
    private static Logger logger = Logger.getLogger(SomeClassB.class);
    public static void main (String[] args) {
        logger.log(Level.INFO, "I am happy!");
        logger.log(Level.WARN, "I am concerned...");
        logger.log(Level.ERROR, "I am in trouble, something went wrong.");
        logger.log(Level.DEBUG, "I am up, I am down, I am all around!");
    }
}

JmxLogger Logs

When you log your event using either Java Logging or Log4J (as show above in the example sections), your log events will get routed to the Handler or Appender defined in your configuration files. In both examples, we have a console and JmxLogger defined. Therefore, all logged events will be show up in the standard console.

Logged events will also be emitted as JMX notifications. Using JConsole, you can see these events as they are logged in your application.

That's it! You have seen how JmxLogger can effortlessly integrate JMX and the two standard logging API's available (Java Logging and Log4J).









Hosted by Google Code