Every time I see a log file on the server, a file is generated every 100MB: I have never understood why some logs will append a timestamp or number to the back of the log after a period of time. It turns out that there is a configuration in log4j - log rollback .
The specific use method is as follows
log4j.rootLogger=DEBUG,ROLLING_FILE,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d-5p %c{1} %x - %m%n #Set file rollback log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.File=./log/test.log log4j.appender.ROLLING_FILE.Append=true #Set the size of each log file to 3KB, which is convenient for testing log4j.appender.ROLLING_FILE.MaxFileSize=3KB #Set the maximum number of log files to 3 log4j.appender.ROLLING_FILE.MaxBackupIndex=3 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=<%d>[%5p] %c-%L -%m%n1) Test procedure:
public class TestLog { public static final Logger log = LoggerFactory.getLogger(TestLog.class); public static void main(String[] args) { int count = 0; while(true){ count++; log.debug("count=" + count); if(count % 30 == 0) break; } } }Log output result:
2) Modify the code:
if(count % 100 == 0) break;Log output result:
3) Modify the code so that the generated log files exceed the set maximum number of 3
if(count % 200 == 0) break;output log result
I believe that through the comparison of the above three results, it is easy to understand what is going on with the log rollback!