2010-04-30 4 views
4

java.util.logging.Logger는 파일 끝에 추가 대신 파일 데이터를 덮어 씁니다 (덮어 쓰기).java.util.logging.Logger가 데이터를 덮어 씁니다.

이 정보가 맞습니까? 앱과 로그 시스템을 초기화 할 때마다 1 개의 파일을 생성해야합니까?

그렇지 않은 경우 어떻게 파일 끝에 쓸 수 있도록 설정합니까?

답변

10

java.util.logging.FileHandler.append=true

추가]은 FileHandler이 기존 파일 (false 기본값)에 추가할지 여부를 지정합니다.

FileHandler Doc

는 로그 파일의 크기와 개수를 순환처럼 통제권있는 다른 호텔을 고를 수 있습니다하지만 난 속성은 당신이 관심있는 일이라고 생각합니다.

+0

이제는 새로운 파일 이었기 때문에 새로운 xml 헤더와 새로운 루트 태그를 만들어 추가하는 중입니다.이 문제를 해결할 방법이 있습니까? –

+0

@Tom Brito - 무엇을 기대합니까? 기존 파일을 읽고 XML 헤더와 루트 요소가 있는지 확인하십시오. XML 로그 파일이 해당 루트 요소에 대해 닫는 태그를 갖도록하려면 어떻게 제안합니까? –

+0

@Stephen 파일에 닫는 태그가 없으면 파일이 손상되어 유효한 XML이 아닙니다. –

7

안녕하세요.이 코드 스 니펫으로 답변을 개선하고 싶습니다.

import java.io.IOException; 
import java.util.logging.FileHandler; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.util.logging.SimpleFormatter; 

public class MyTestLogger { 
    public static void main(String[] args) throws SecurityException, 
      IOException { 
     /* 
     * The below line is the syntax for the file handler which has the capability of 
     * appending the logs in the file. The second argument decides the appending. 
     * FileHandler fileTxt = new FileHandler("eLog.txt", true); 
     */ 
     FileHandler fileTxt = new FileHandler("eLog.txt", true); 
     SimpleFormatter formatter = new SimpleFormatter(); 
     fileTxt.setFormatter(formatter); 
     Logger LOGGER = Logger.getLogger(MyTestLogger.class.getName()); 
     LOGGER.addHandler(fileTxt); 
     LOGGER.setLevel(Level.SEVERE); 
     LOGGER.severe("This is a serious problem !"); 
    } 
} 
관련 문제