M
msk_30339
I am using the following code to use the CompositeRollingAppender
class to roll a Log4j log file every day or when the log file is 1KB
(in this example), whichever comes first. However, when I run this
class several times, it keeps writing to the same log file without
rolling to the next one, even when the size exceeds 1KB.
Also, if I change the counter in the for loop, it starts to act odd.
If I make it iterate 10 times, I get a log file. If I make it iterate
15 times, I get a log file with a .3 extension. If I make it iterate
20 times, I get a log file with a .8 extension. I'm sure this all
comes back to something wrong in my configuration settings, but I
don't know what it is.
Any thoughts are greatly appreciated.
Code Follows:
----------------------------------
import org.apache.log4j.*;
public class TestClass{
public static void main(String args[]) {
TestClass l = new TestClass();
l.invoke();
}
public void invoke() {
CompositeRollingAppender cra = new CompositeRollingAppender();
cra.setRollingStyle(3);
cra.setStaticLogFileName(true);
cra.setFile("C:\\TestLogFile3.txt");
cra.setCountDirection(-1);
cra.setMaxFileSize("1KB");
cra.setMaxSizeRollBackups(10);
cra.setDatePattern("'.'yyyy-MM-dd");
PatternLayout pl = new PatternLayout();
pl.setConversionPattern("%m%n");
cra.setLayout(pl);
cra.activateOptions() ;
Logger myLogger = Logger.getLogger("");
myLogger.addAppender(cra);
for (int i = 0 ; i < 10 ; i++) {
System.out.println("Writing");
myLogger.debug("132456789013245678901324567890132456789013245678901324567890132456789013245678901324567890");
}
LogManager.shutdown();
}
}
class to roll a Log4j log file every day or when the log file is 1KB
(in this example), whichever comes first. However, when I run this
class several times, it keeps writing to the same log file without
rolling to the next one, even when the size exceeds 1KB.
Also, if I change the counter in the for loop, it starts to act odd.
If I make it iterate 10 times, I get a log file. If I make it iterate
15 times, I get a log file with a .3 extension. If I make it iterate
20 times, I get a log file with a .8 extension. I'm sure this all
comes back to something wrong in my configuration settings, but I
don't know what it is.
Any thoughts are greatly appreciated.
Code Follows:
----------------------------------
import org.apache.log4j.*;
public class TestClass{
public static void main(String args[]) {
TestClass l = new TestClass();
l.invoke();
}
public void invoke() {
CompositeRollingAppender cra = new CompositeRollingAppender();
cra.setRollingStyle(3);
cra.setStaticLogFileName(true);
cra.setFile("C:\\TestLogFile3.txt");
cra.setCountDirection(-1);
cra.setMaxFileSize("1KB");
cra.setMaxSizeRollBackups(10);
cra.setDatePattern("'.'yyyy-MM-dd");
PatternLayout pl = new PatternLayout();
pl.setConversionPattern("%m%n");
cra.setLayout(pl);
cra.activateOptions() ;
Logger myLogger = Logger.getLogger("");
myLogger.addAppender(cra);
for (int i = 0 ; i < 10 ; i++) {
System.out.println("Writing");
myLogger.debug("132456789013245678901324567890132456789013245678901324567890132456789013245678901324567890");
}
LogManager.shutdown();
}
}