Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Improve Eclipse software development with robust logging plug-in

JavaFAQ Home » Java Tools Go to all tips in Java Tools


Bookmark and Share

Good developers know the importance of careful design, testing, and debugging, and Eclipse helps with all these tasks, but what about logging? Many developers believe that logging is integral to good software development practices; you'll no doubt agree if you've ever had to fix a problem in someone else's already deployed application. Fortunately, the impact of log statements on performance is minimal or nonexistent for most cases, and because the logging tools are easy to use, the learning curve is very small. So, with the excellent tools available, there is no excuse for not including log statements in your applications.

Tools at your disposal

If you are writing an Eclipse plug-in, you can use the services provided by org.eclipse.core.runtime.ILog that is accessed through the method getLog() from your Plugin class. Just create an instance of org.eclipse.core.runtime.Status with the right information and call the log() method on ILog.

This log object accepts more than one log listener instance. Eclipse adds two listeners:

* A Listener that writes logs to the "Error Log" View
* A Listener that writes logs to the log file located at “${workspace}/.metadata/.log"

You can also create your own log listener. Just implement the interface org.eclipse.core.runtime.ILogListener and add it to the log object through the method addLogListener(). The logging() method in your class will be called for each log event.

All this is very nice, but there are some problems with this approach. What if you want to change the log destination of an already deployed plug-in? Or control the amount of information logged? Also, this implementation may impact performance as it always sends log events to all listeners. That is why you might normally see logs only in extreme cases, like error conditions.

On the other hand, there are two great tools specialized just for logging. One comes with Java 2 SDK 1.4 in the java.util.logging package. The other is from Apache, called Log4j.

Both have the concept of a hierarchy of Logger objects that can send log events to any number of Handlers (called Appenders in Log4j), which delegate the message formatting to Formatters (called Layouts in Log4j). Both can be configured by property files. Log4j also uses xml files for configuration.

A logger can have a name and be associated to a Level. A logger can inherit its settings (levels, handlers) from its parent. A logger named "org" is automatically the parent of another, named "org.eclipse", so whatever you set for "org" in the configuration file can be inherited by the "org.eclipse" logger.

My preference? I have used both, and I have a special preference for Log4j. I use java.util.logging only if I have a very simple application and I don't want to add the log4j.jar to it. For full explanations of both, please refer to the Java docs and Apache site (see Resources for links).

An improved log
Wouldn't it be great if there was a way to improve the Eclipse log experience? The two main problems with it are:

* Lack of an external configuration file
* Performance issues associated with the fact that there is no fine grained control of its behavior

Given this challenge, I started thinking about ways to integrate logging tools into Eclipse. My first candidate was java.util.logging, simply because it is already in the JSDK1.4 distribution.

I wanted to allow a plug-in writer to be able to customize logging through a configuration file and have the log event go to any handler already available. I planned to create two additional handlers: one that would send the log event to the "Error Log" view and another that would write it to the Plug-in state location: “${workspace}/.metadata/.plugins/${plugin.name}".

All this would be contained in a Plug-in Log Manager. You would just add it to your plug-in dependencies and get the logger objects from it.

From my experience, however, I would not recommend using java.util.logging for this. Its implementation goes to great lengths to keep only one single LogManager instance. It uses the system class loader to achieve this. Thus you have only one hierarchy of loggers for all users. You lose isolation. So, if more than one application is using loggers, they will share settings, and one application’s logger instance can potentially inherit settings from another application’s logger.

So, why not extend the LogManager and instantiate one yourself? The problem with this approach is that the LogManager instance uses the system class loader to instantiate classes from the configuration file. One of the strengths of plug-ins is to provide isolation through the usage of different class loaders. If you need isolation for your log manager, java.util.logging would not be suitable due to architectural limitations.

On the other hand, Log4j has proved to be very useful. Its hierarchy of loggers is kept in an object called, believe it or not, Hierarchy. So, you can create one hierarchy per plug-in. Problem solved. You can also create a custom appender (handler) to send events to the "Error Log" view and another to send it to the plug-in state location. Life is good.

Let’s review how to do this, starting from the plug-in writer's point of view. Just create your plug-in, add com.tools.logging to its dependency list, and create a Log4j configuration file. Instantiate a PluginLogManager and configure it with this file. This should be done only once, so you can do it when the plug-in starts. For the log statements, just use them as you would do with Log4j. Listing 1 shows you an example:

Use a logging framework for Eclipse plug-ins that is itself a plug-in a


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy