2009-12-04 39 views
1

我正在開發RCP應用程序並使用log4j生成日誌。每當記錄異常時,我想在日誌文件中包含一行以標識應用程序版本。log4j日誌文件上的插件版本

我可以通過在log4j.properties文件上的log4j.appender.file.layout.ConversionPattern行上強制執行該操作,只需寫下它,但是我必須記住每次更改版本時都要更改它數字,這對我的神經來說太容易出錯。

有沒有辦法通過log4j.properties文件或編程方式來改變應用程序版本號的方式來記錄值的變化?

謝謝。

回答

3

這聽起來像你想能夠以編程方式配置log4j。您可以使用log4j PropertyConfigurator.configure()方法提供一個包含日誌記錄屬性的屬性對象。在您的轉換模式中,您可以將您的版本號指定爲常量。另外,我建議您從定義應用程序的主插件(包)中獲取版本號,以便您可以使用正常的Ec​​lipse版本控制內容來定義它。你可以使用這樣的代碼:

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

非常感謝你,弗朗西斯! – 2009-12-07 12:10:21

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輸出中重用噸。