public void testLimitAndCount() throws Exception {
handler.close();
// very small limit value, count=2
// output, rename current output file to the second generation file
// close it and open a new file as rotation output
handler = new FileHandler("%t/testLimitCount%g", 1, 2, false);
handler.publish(r);
handler.close();
assertFileContent(TEMPPATH, "testLimitCount1", handler.getFormatter());
// very small limit value, count=1
// output once, rotate(equals to nothing output)
handler = new FileHandler("%t/testLimitCount%g", 1, 1, false);
handler.publish(r);
handler.close();
assertFileContent(TEMPPATH, "testLimitCount0", new LogRecord[0],
handler.getFormatter());
// normal case, limit is 60(>2*msg length <3*msg length), append is
// false
handler = new FileHandler("%t/testLimitCount%u", 60, 3, false);
LogRecord[] rs = new LogRecord[10];
// batch output twice to test the append mode
for (int i = 0; i < 5; i++) {
rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
handler.publish(rs[i]);
}
handler.close();
handler = new FileHandler("%t/testLimitCount%u", 60, 3, false);
for (int i = 5; i < 10; i++) {
rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
handler.publish(rs[i]);
}
assertFileContent(TEMPPATH, "testLimitCount0.1", new LogRecord[] {
rs[5], rs[6], rs[7] }, handler.getFormatter());
assertFileContent(TEMPPATH, "testLimitCount0.0", new LogRecord[] {
rs[8], rs[9] }, handler.getFormatter());
// normal case, limit is 60(>2*msg length <3*msg length), append is true
handler = new FileHandler("%t/testLimitCount%u", 60, 3, false);
// batch output twice to test the append mode
for (int i = 0; i < 5; i++) {
rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
handler.publish(rs[i]);
}
handler.close();
handler = new FileHandler("%t/testLimitCount%u", 60, 3, true);
for (int i = 5; i < 10; i++) {
rs[i] = new LogRecord(Level.SEVERE, "msg" + i);
handler.publish(rs[i]);
}
handler.close();
assertFileContent(TEMPPATH, "testLimitCount0.2", new LogRecord[] {
rs[3], rs[4], null, rs[5] }, handler.getFormatter());
assertFileContent(TEMPPATH, "testLimitCount0.1", new LogRecord[] {
rs[6], rs[7], rs[8] }, handler.getFormatter());
assertFileContent(TEMPPATH, "testLimitCount0.0",
new LogRecord[] { rs[9] }, handler.getFormatter());
FileHandler h1 = null;
FileHandler h2 = null;
try {
File logDir = new File("log");
reset("log", "");
logDir.mkdir();
h1 = new FileHandler("log/a", 0, 1);
assertNotNull(h1);
h2 = new FileHandler("log/a", 0, 1, false);
assertNotNull(h2);
} finally {
try {
h1.close();
} catch (Exception e) {
}
try {
h2.close();
} catch (Exception e) {
}
reset("log", "");
}
}