2014-02-26 23 views
0

我想寫一些測試消息或一些文本內容通過java inbiz框架文件。如果文件已經有內容,則最近的消息必須附加在文件的底部。如何寫文本或登錄到ofbiz框架中的文件

簡而言之,我想要一個諸如ofbiz中Debug.log的功能。由於它將所有內容寫入debug.log文件,因此我需要一個單獨的文件來編寫我的文本消息。

我想這一點,

File file = new File("/applications/party/webapp/partymgr/test.txt"); 
if (!file.exists()) { 
try { 
    file.createNewFile(); 
    FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
    BufferedWriter bw = new BufferedWriter(fw); 
    Debug.logInfo("writing...........", module); 
    bw.write("this is first line in file"); 
    bw.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
} 

但它扔FileNotFoundException
因爲,我不是在ofbiz的專家,我不確定文件路徑。如果文件路徑是問題,請提出解決方案。

+0

獲取相對路徑你爲什麼不只需添加一個新的RollingFileAppender進行在log4j配置?如果你想把自己的perf數據寫到它自己的日誌文件中,你會這樣做...... – arajashe

+0

我已經看到了,但我無法理解這種機制。你能否以更詳細的方式說出或給我任何幫助的教程。謝謝@arajashe –

回答

0

您可以使用記錄儀記錄信息。

Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); 
     logger.setLevel(Level.INFO); 
     try { 
      FileHandler fileTxt = new FileHandler("/root/apache-ofbiz/applications/party/webapp/partymgr/test.txt",true); 

      SimpleFormatter formatterTxt = new SimpleFormatter(); 
      fileTxt.setFormatter(formatterTxt); 
      logger.addHandler(fileTxt); 

      logger.log(Level.INFO,"this is first line in file"); 

    } catch (SecurityException e) { 
    } catch (IOException e) { 
    } 

不要忘記提絕對文件路徑。例如:/root/apache-ofbiz/applications/party/webapp/partymgr/test.txt

或者

從組件

filepath = ComponentConfig.getRootLocation("party")+"webapp/log/test.txt"; 
+0

感謝您的回覆。假設它在其他機器上運行,我們如何知道絕對路徑。意思是,在我的系統中,我知道絕對路徑。如果在其他機器上執行我的代碼,它是如何工作的。如何保存該路徑中的文件。你有沒有關於ofbiz.log.1,ofbiz.log.2等ofbiz.log.N如何由ofbiz動態創建的想法。 @senthil Plz幫助。 –

+0

記錄器工作夥計..但我有文件路徑的問題。我無法在每一臺機器上給出這樣的路徑嗎? –

+0

檢查更新的答案。 – Senthilmurugan

相關問題