Mercurial Hosting > luan
view src/goodjava/logger/RollingFileAppender.java @ 1699:e6750146faa1
no restart on backup
author | Vadim Filimonov <fffilimonov@yandex.ru> |
---|---|
date | Tue, 28 Jun 2022 16:13:37 +0300 |
parents | 97cc73664ca8 |
children |
line wrap: on
line source
package goodjava.logger; import java.io.File; import java.io.FileWriter; import java.io.IOException; import goodjava.io.IoUtils; public class RollingFileAppender extends WriterAppender { protected final String[] fileNames; protected final File file; public long maxFileSize = 10*1024*1024; public RollingFileAppender(Layout layout,String[] fileNames) throws IOException { super(layout,null); this.fileNames = fileNames; this.file = new File(fileNames[0]); open(); } protected void open() throws IOException { this.writer = new FileWriter(file,true); } public synchronized void append(LoggingEvent event) { super.append(event); if( file.length() > maxFileSize ) { rollOver(); } } protected void rollOver() { close(); File backup = new File(fileNames[fileNames.length-1]); try { IoUtils.delete(backup); for( int i=fileNames.length-2; i>=1; i-- ) { File f = backup; backup = new File(fileNames[i]); IoUtils.move(backup,f); } IoUtils.move(file,backup); open(); } catch(IOException e) { throw new RuntimeException(e); } } }