English 中文(简体)
Plugin version on log4j log file
原标题:

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.

最佳回答

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$
}
问题回答

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.





相关问题
Specify time zone of log4j s date

Is it possible to specify the time zone that log4j will use? I need the dates in the log file to be a different time zone than the application s. log4j s PatternLayout uses SimpleDateFormat. ...

java wrapper around log4j logger and java.util.logging

I am writing a library. The library can be used by applications that use log4j loggers and java.util.logging loggers. So, I wrote a quick wrapper class, that encapsulates both loggers. I allow the ...

Grails log4j configuration

I ve repeatedly had problems with the DSL introduced by Grails in version 1.1 for configuring Log4J. My current configuration looks like this: log4j = { debug com.mypackages ...

Creating daily logs with Log4j?

What configuration values are needed to setup Log4j to use the following pattern? MyApp-Mon.log MyApp-Tue.log MyApp-Wed.log Etc With each file containing the days log. This sounds easy enough to do ...

Overriding log4j settings at deploy time

For an application which is deployed on a large number of machines I took the decision to deploy a standard log4j.xml file with the application package, just to make sure that have the same settings ...

How to validate Java logging properties files?

I have a basic facility for allowing users to remotely apply changes to the logging files in my application. Some logs are configured using java.util.logging properties files, and some are configured ...

How do you Change a Package s Log Level using Log4j?

I ve encountered the following bug: http://issues.apache.org/jira/browse/AXIS2-4363 It states the following: This error only occurs when log level for org.apache.axiom is DEBUG so a ...

热门标签