views:

123

answers:

2

I'm developing an RCP application and use log4j to generate logs. Whenever an exception is logged, I want to include a line on the log file to identify the application version.

I could do that by force on the log4j.appender.file.layout.ConversionPattern line on the log4j.properties file, simply writing it down, but then I'd have to remember to change it everytime I change the version number, which is too error-prone for my nerves.

Is there a way to that, either on the log4j.properties file or programatically, in a way that the value logged changes as I change the application version number?

Thank you.

A: 

You could ask your application to get the version information from a plugin.properties or even a custom my.properties file, like in this thread:

public class MyPlugin extends AbstractUIPlugin {

  protected final static String MY_PROPERTIES = "my.properties";    
  protected PropertyResourceBundle myProperties;

  public PropertyResourceBundle getMyProperties(){

  if (myProperties == null){
    try {
      myProperties = new PropertyResourceBundle(
        FileLocator.openStream(this.getBundle(),
          new Path(MY_PROPERTIES),false));
    } catch (IOException e) {
      this.logError(e.getMessage());
    }
  return myProperties;
  }
...

Then from within your code you would call:

MyPlugin.getInstance().getMyProperties().getString("foo");

This could then be store in a static variable for the duration of your session, and reused in your log4j output.

VonC
+1  A: 

It sounds like you want to be able to configure log4j programatically. You can use the log4j PropertyConfigurator.configure() method to provide a properties object that contains your logging properties. In your conversion pattern you can then specify your version number as a constant. Also, I recommend that you get the version number from the main plugin (bundle) that defines your application, so you can use the normal Eclipse versioning stuff to define it. You can use code like this:

public String getVersion()
{
    Bundle bundle = Platform.getBundle("com.yourcompany.yourproject.pluginid");
    Dictionary headers = bundle.getHeaders();
    return (String)headers.get("Bundle-Version"); //$NON-NLS-1$
}
Francis Upton
Thank you very much, Francis!
Mario Marinato -br-