log4j日志文件上的插件版本

时间:2009-12-04 15:35:50

标签: log4j eclipse-rcp

我正在开发RCP应用程序并使用log4j生成日志。每当记录异常时,我想在日志文件中包含一行以标识应用程序版本。

我可以通过强制对log4j.properties文件中的log4j.appender.file.layout.ConversionPattern行执行此操作,只需将其写下来,但是我必须记得每次更改版本号时都要更改它这对我的神经来说太容易出错了。

有没有办法在log4j.properties文件或编程方式,以记录的值更改为更改应用程序版本号的方式?

谢谢。

2 个答案:

答案 0 :(得分:3)

听起来您希望能够以编程方式配置log4j。您可以使用log4j PropertyConfigurator.configure()方法提供包含日志记录属性的属性对象。在转换模式中,您可以将版本号指定为常量。另外,我建议您从定义应用程序的主插件(包)获取版本号,这样您就可以使用普通的Eclipse版本控制来定义它。您可以使用以下代码:

public String getVersion()
{
    Bundle bundle = Platform.getBundle("com.yourcompany.yourproject.pluginid");
    Dictionary headers = bundle.getHeaders();
    return (String)headers.get("Bundle-Version"); //$NON-NLS-1$
}

答案 1 :(得分:0)

您可以要求您的应用从plugin.properties甚至是custom my.properties文件中获取版本信息,例如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;
  }
...
  

然后从您的代码中调用:

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

然后可以在会话期间将其存储在静态变量中,并在log4j输出中重复使用。