Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Track Your Apps with the Open Source Logging Framework, Log4j : Page 4

As many developers can attest, logging is one of the most accessible ways to monitor the health and performance of your Java applications. Find out how Log4j, a logging framework from Apache, can handle both simple and advanced (multi-app, multi-machine) logging scenarios.


advertisement
Some Advanced Log4J Logging
What you've learned thus far in this article is quite likely to meet most of your logging needs. The next step is to make our study of Log4j a bit more invasive, demonstrating usage of some of the appenders bundled with the Log4j distribution, namely, NTEventLogAppender and JMSQueueAppender.

The NTEventLogAppender
If you are using a Windows environment for your Java applications to run in, then you might wish that certain log events from your Java programs could find their way to the NT Event logs so that an administrator in charge of viewing the logs could react when something goes awry. Log4j provides this "hook" into the NT Event logs.

As you will see shortly, an NT administrator can use the Event Viewer tool to examine events from the Java program that make their way into the NT event logs. Writing to the NT event logs is made simple by the NTEventLogAppender, which ships with Log4j. The one thing that you must do to make things work is place the file NTEventLogAppender.dll in a directory that is on the PATH of the Windows system (for example, in the C:\windows\system32 directory) or else you will get a java.lang.UnsatisfiedLinkError when trying to use the NTEventLogAppender.



The sample code below (taken from the Log4JWindowsNTEvent class, which is available in the downloadable sample code for this article) shows how to use the NTEventLogAppender.

// get a logger instance called "Log4JWindowsEvent" Logger myLogger = Logger.getLogger("Log4JWindowsNTEvent"); // specify a source String mySource = "the source"; // create an NTEventLogAppender NTEventLogAppender eventLogAppender = new NTEventLogAppender(mySource,myLayout); //create a console appender ConsoleAppender consoleAppender = new ConsoleAppender(myLayout); // associate the appenders to the logger myLogger.addAppender(consoleAppender); myLogger.addAppender(eventLogAppender); // set the level of the logger myLogger.setLevel(Level.WARN); // log a fatal event myLogger.fatal("I have died of thirst");

As you can see, Log4j abstracts you from the intricacies of having to learn how to interface with the Windows API to write to the Event Log. All you have to do is construct an NTEventLogAppender (which I called eventLogAppender) and add this appender to your Logger (myLogger). I used the NTEventLogAppender(String source, Layout layout) constructor to specify the source of the event (which I called 'the source'). You can also do this with the setSource method.

To confirm that the event was logged to the NT Event Logs, you can use the Event Viewer application within the Windows Control Panel's Administrative Tools. Figure 1 shows the Windows Event Viewer in action, while Figure 2 shows how to get the details on the specific Log4j event from the application.


Figure 1. Momentous Events: The Event Viewer application can be used to view NT Events, including the sample Log4j log I wrote using the NTEventLogAppender.
 
Figure 2. Event Properties: Double-clicking on our event in the Event Viewer application gives us more information about the Log4j event we wrote.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date