简体   繁体   中英

RollingFileAppender configuring through Java

My log needs to have maxsize 5KB, no backup files and needs to append. When the log reach 5KB it erase the amount of data necessary to fit the new data. For example:

1: abcd

2: bcde

3: cdef

     Logger rootLogger = Logger.getRootLogger();
     rootLogger.setLevel(Level.DEBUG);    
     PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c %x - %m%n");
     rootLogger.addAppender(new ConsoleAppender(layout));
     RollingFileAppender fileAppender = new RollingFileAppender(layout, "test.log");
     fileAppender.setAppend(true);
     fileAppender.setMaxFileSize("5KB");
     fileAppender.setMaxBackupIndex(0);

My problem is when the size reach 5KB. It doesn't append (roll) like I want, its overwrite the hole data. Why it doesn't append? Can anyone help me?

I think you misunderstand how the RollingFileAppender works. Javadoc: RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.

So it rolls over a number of files and not the lines/content in one file.

kind regards, soilworker

Just switch property append = false . It will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM