FileHandler를 확장하여 setLevel
메서드를 재정 의하여 회전을 수신 할 수 있습니다. 그런 다음 제한을 1 바이트로 설정하여 FileHandler를 항상 회전하도록 강제 한 다음 조건이 충족되지 않으면 회전이 일어나지 않도록합니다. 당신은 단지 하나의 로그 파일에 대한 약간의 최대 한계를 원하는 경우
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
public class CountingFileHandler extends FileHandler {
private static final RuntimeException PREVENT_ROTATE = new RuntimeException();
private final long maxRecords;
private long count;
public CountingFileHandler(String pattern, long maxRecords, int files) throws IOException {
super(pattern, 1, files, false);
this.maxRecords = maxRecords;
}
@Override
public synchronized void setLevel(Level lvl) {
if (Level.OFF.equals(lvl)) { //Rotation sets the level to OFF.
if (++count < maxRecords) {
throw PREVENT_ROTATE;
}
count = 0L;
}
super.setLevel(lvl);
}
@Override
public synchronized void publish(LogRecord record) {
try {
super.publish(record);
} catch (RuntimeException re) {
if (re != PREVENT_ROTATE) {
throw re;
}
}
}
public static void main(String[] args) throws Exception {
System.out.println(new File(".").getCanonicalPath());
CountingFileHandler cfh = new CountingFileHandler("test%g.log", 2, 5);
cfh.setFormatter(new SimpleFormatter());
for (int i = 0; i < 10; i++) {
cfh.publish(new LogRecord(Level.SEVERE, Integer.toString(i)));
}
cfh.close();
}
}
그렇지 않으면, 당신은 단지 Long.MAX_VALUE
의 지속 시간 com.sun.mail.util.logging.DurationFilter를 설치할 수 있습니다 : 여기
은 샘플 솔루션입니다. 해당 필터는
javax.mail.jar
or the logging-mailhandler.jar
에 포함되어 있습니다. 이 솔루션은 원하는 회전을 제공하지 않습니다.
감사합니다. 정확히 제가 원했던 것입니다. – RahulKY