Automatic Remote Logging Management via JMX
It all started weeks ago, where I have accidentally discovered a “hidden JDK 5 treasure” (from Daniel Fuch's blogs at http://blogs.sun.com/jmxetc/) , called java.lang.instrument, a Java package that provides services that allowing Java language agents to instrument programs running on the JVM. Most people use it for profiling, or pre-instrument purpose.
So, I decided to create a Automatic Remote Logging Management Agent, that able wrap any Java application that use apache log4j for logging, and provide us the following functionalities:
* Abilities to change log level (debug, info, warning, error) on any configured logger dynamically without restart.
* Abilities to locate any configured log output files.
* Abilities to browse (either head, or tail, or from any filePointer) any configured log files.
Base on requirements above, I come out JMXMBean interface below:
public interface RemoteLoggingMBean {
public void activateDebug(String loggerName);
public void activateInfo(String loggerName);
public void activateWarning(String loggerName);
public void activateError(String loggerName);
/**
* Retrieve current configured log output files base on given logger.
* The system will only return log files with the following appender
* org.apache.log4j.DailyRollingFileAppender, org.apache.log4j.FileAppender
* org.apache.log4j.RollingFileAppender;
* @param loggerName
* @return Array of file name with relataive file path points to current running directory.
*/
public String[] retrieveCurrentConfiguredLogFiles(String loggerName);
/**
* Similar to Unix head command, read a text file forward from the beggining
* of the file till it reaches line limit, or EOF
* @param fileName the logFileName.
* @param linesToRead, Number of line to be read.
* @return JMX CompositeData with the structure of StartFilePointer (SimpleType.Long),
* EndFilePointer(SimpleType.Long), LogMessages(SimpleType.String)
* @throws java.io.IOException
*/
public CompositeData headLog(String fileName, int linesToRead) throws OpenDataException, IOException;
public CompositeData headLog(String fileName, int linesToRead, long fromFilePointer) throws OpenDataException, IOException;
public CompositeData tailLog(String fileName, int linesToRead) throws OpenDataException, IOException;
public CompositeData tailLog(String fileName, int linesToRead, long fromFilePointer) throws OpenDataException, IOException;
}
The rest of code is delivered by using various JMX and Apache Log 4J API, which I am not going to discuss here, as this is obviously not JMX nor Java Logging tutorial.
If u interest, u could download the whole project source from here. Is not under any copy write protection. U are welcome to download it, modify it, even change the author name, use it or distribute it to anyone.
Anywhere, back to topic. To use the wrapper on any Java application that use Apache Log4J for logging, follows steps below:
1. Either download the binary RemoteJMXLoggingAgent.jar from here or build it from the source
2. Put the binary to your application class path, make sure it sit together with log4j.jar
3. Append the line at your Java Application startup script
"-javaagent:(deploy directory)/RemoteJMXLoggingAgent.jar
-Drmi.agent.port=AnyRMIPort (optional, default=3000)"
That's it, u application is now able to offer above Remote Logging management functionalities via JMX. To test this, open jconsole from any PC, and connect your remote application via the JMX URL below:
service:jmx:rmi:///jndi/rmi://hostname:rmi_port/remoteLoggerAgent
The Mbean on managing your application logger is called "org.coolboy.RemoteLoggingManager", have fun!
* Attachment remotejmxloggingagent.jar (10.0 K)
* Attachment remoteloggingagentsrc.jar (687.3 K)