Friday, April 17, 2009

Integrating Eclipse Logging with log4j


As you may know, Eclipse uses it's own logging API, which is different than log4j. All the eclipse core plugins use Eclipse logging. What can you do if you have dependencies which are already using log4j? Should you use Eclipse logging or log4j in your plugin? This article deals about the options available and how to integrate both of them.


RCP logging in Eclipse and dependencies in Log4j:

Have your plugins write to Eclipse log and maintain two different log files. i.e one from log4j and one (.log) from Eclipse. One problem with writing to Eclipse log is there is no external config file with which users can control the levels of logging and the classes/packages for which they want to see the log. Also, this also means that only Eclipse logs will show up inside your application (in Error Log view).

Third party plugins to see log4j:

While the above option does not look good, the good news is there are some eclipse plugins available here and they would show the log view inside eclipse. So, if your customers ever want to see the other logs inside the application, then you can use this.

Ultimate destination is log4j:

If you want your ultimate destination to be log4j, then you can go with this option. You can attach listeners to eclipse ILOG (see Platform.addLogListener(ILogListener)) and then your listener can then convert and write to your log4j. This is one way to get all the logging in to place i.e to your log4j, since your dependencies are already writing to log4j. Once you have this, you can use third party plugins (described above) to show all the logs in one view.

Ultimate destination is Eclipse Log:

If you want your ultimate destination to be Eclipse log, this is harder to achieve. I don't know of any option to convert your dependecy project's logs to Eclipse log. But, if you plugin is writing to log4j, then you can use the plugin (or just the idea) presented in this article. This plugin uses custom appenders and hierarchy event listeners and converts the log4j log to Eclipse Log. This is a great option to use if you do not have dependency plugins which write to log4j. Also, you can precisely control the log levels at run-time using this option.


We saw several options to use and combine eclipse logging and log4j. You can also have your ultimate destination to be both log4j and Eclipse log by combining the above options presented.

No comments: