Introducing JmxLogger: Realtime Log Monitoring with JMX
Join the DZone community and get the full member experience.
Join For Freejmxlogger 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.
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.
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/
Opinions expressed by DZone contributors are their own.
Trending
-
Getting Started With the YugabyteDB Managed REST API
-
Top 10 Pillars of Zero Trust Networks
-
Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
-
Operator Overloading in Java
Comments