2014-02-26 2 views
0

ofbiz 프레임 워크에서 java를 통해 파일에 몇 가지 테스트 메시지 또는 일부 텍스트 내용을 쓰려고합니다. 파일에 이미 내용이있는 경우 최근 메시지를 파일 맨 아래에 추가해야합니다.forbiz 프레임 워크에서 텍스트 또는 로그 파일에 쓰는 방법

간단히 말해서, 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

에서 상대 경로를 가져옵니다 /root/apache-ofbiz/applications/party/webapp/partymgr/test.txt 왜 해달라고 당신은 단순히 새로운 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) { 
    } 

참고 절대 파일 경로는 언급하지 마십시오. 예는 :

또는

구성 요소

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

답장을 보내 주셔서 감사합니다. 다른 컴퓨터에서 실행중인 경우 어떻게 절대 경로를 알 수 있습니까? 내 시스템에서, 나는 절대 경로를 안다. 다른 컴퓨터에서 코드를 실행하면 어떻게 작동합니까? 해당 경로에 파일을 저장하는 방법. ofbiz.log.1, ofbiz.log.2 등 ofbiz.log.N이 ofbiz에 의해 동적으로 생성되는 방법에 대해 알고 계십니까? @senthil Plz 도움. –

+0

로거는 친구 야.하지만 파일 경로에 문제가 있습니다. 나는 모든 기계에서 그처럼 길을 줄 수는없는 것입니까? –

+0

업데이트 된 답변을 확인하십시오. – Senthilmurugan

관련 문제