Logging in Eclipse RCP

A few days ago, I tried to find information on how to do logging in Eclipse RCP without using Log4j. There are plenty of sites on the net giving information on how to setup a plugin to use log4j with Eclipse RCP, see here and here, but not much on Eclipse's native logging framework. I couldn't find any coherent information on the net setting out how to do this.

The interfaces and classes necessary to use logging in Eclipse RCP

I assume this is because it is easy enough for most people to figure out, and because the Eclipse logging framework seems somewhat weak when compared to the mainstream logging frameworks available tody.  Nevertheless I investigated how the native framework worked and decided to document it here for others.

Logging in Eclipse RCP works differently than what most of us are used to, if you have been using log4j, commons logging or java logging. In Eclipse you need to get a reference to an object that implements the ILog interface (documented here.). You can get a reference to the logger by calling the getLog method on the Activator object like so:

ILog logger = Activator.getDefault().getLog();

You then log a message by creating a IStatus Object and logging it like so:

IStatus status = new Status(IStatus.ERROR,Activator.PLUGIN_ID,e.getMessage(),e);
logger.log(status);

The links provided to the API docs above link to Eclipse3.2 documentation. There seems to have been some slight API changes since then (3.4) but I couldn't find the 3.4 API docs. The changes seem mostly to be in convenience methods which the eclipse auto-complete feature should help you find.

 Default log file location and creating your own application log file

The default location of the Eclipse log file, is under your eclipse workspace directory under,  <workspace>/metadata/.log, but I found none of my messages were being logged there. To get my messages to be logged, I had to create a LogListener  class that extended the ILogListener interface:

public class LogListener implements ILogListener {

    private File logFile;
   
    public LogListener(){
        String path = System.getProperty("user.dir");
        logFile = new File(path+"/log.log");
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
    @Override
    public void logging(IStatus status, String plugin) {
        try {
            BufferedWriter bos = new BufferedWriter(new FileWriter(logFile,true));
            StringBuffer str = new StringBuffer(plugin);
            str.append(": ");
            str.append(status.getMessage());
            String stackTrace = Util.getStackTraceAsString((Exception) status.getException());
            str.append(stackTrace);
            bos.write(str.toString());
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

If you only want to log messages of a certain severity and above, then I assume you will have to override this in the logging method mandated by the ILogListener interface.

Once you have your LogListener class, you need to register it with the ILog object to start receiving log messages. This is done with the addLogListener method of the ILog obect:

logger.addLogListener(logListener);

After that you will start getting your log messages in the location setup in your LogListener class. The Eclipse RCP logging framework, as I understand it, is clearly not as flexible and configurable as log4j's and is probably why most people end up using log4j with their eclipse applications instead.

Comments

Thanks Mark. I was working on a POC and wanted a quick fix logging feature for my plugin. Your post helped me a lot.

good post man, indeed quite useful logging tips in java. In my opinion using correct logging level for different message is most important thing to learn and logging has important performance impact (ever found process running in DEBUG mode in production ) and has to be handled efficiently. I have also blogged my tips as 10 tips on logging in Java , let me know how do you find it.

Your article helps me understanding logging in RCP.I think "log view" in eclipse is associated with ILog. So it will be useful if you use log view in eclipse.

Your spam filter doesn't let me write how bad this post is. 

Mark Clarke's picture

Hi Nickolay,

Sorry that you experience problems with the spam filter. Maybe try without urls? Also please note that this bloc post is from over 6 years ago so a lot might have changed.

Is there a way to have more informations then the ones contained in the Status object ?