|
Project Information
Members
Featured
Downloads
Wiki pages
Links
|
JmxLoggerJmxLogger makes it easy to do realtime application log monitoring. The JmxLogger API provides both Java Util Logging Handler and a Log4J Appender classes for integration with your choice of logging technologies that you feel confortable with. You simply configure your logging framework, as you would normally do, and the JmxLogger automatically capture and braodcasts your event logs to any registered JMX client. It provides a built-in log console to monitor your log locally or remotely (no more tail -f server.log). Download the new 0.3.0 version with these features. Features
Getting Started
Configure JmxLogger for Java Util LoggingIf 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 The handler supports several attributes that are not provided and are automatically assigned a default value. In this configuration, we are declaring a JmxLogger with Level set to 'INFO' The SimpleFormatter class will be used to format logs JMX ObjectName for remote monitoring is set to 'jmxlogger:type=LogEmitter' Java Util Logging Codeimport 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. Configure JmxLogger for Log4JJmxLogger 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>Log4J Codeimport 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!");
}
}Running ProcessYou 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.ClassWhen 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). For more on how to use JMX remote connectivity see - http://java.sun.com/j2se/1.5.0/docs/guide/management/agent.html. JmxLogger ConsoleOnce 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 This will start the console:
Expression FilterAs mentioned, you can also use a filter expression to narrow down which log messages you want to receive. In the following screen, the logs are filtered where raw message contains the string "something went wrong": Using JConsole, you can see these events as they are logged as well. JConsoleSince JmxLogger is based on the JMX standard Java API, all logs can also be viewed using JConsole: Shutting Down the LoggerWhen using JmxLogger in in stand-alone mode, it will instantiate an MBean server if none is provided. The server will run indefinitely unless it is shutdown explicitly. Here is how you do it: Java Util LoggingYou must call the LogManager.reset() method. This will release the underlying MBean server. Log4JCall LogManager.shutdown(). This will release the underlying MBean server. |
JmxLogger exposes several parameters which can be used in filter expressions at runtime. See wiki for detail.