view src/goodjava/logger/DailyRollingFileAppender.java @ 1594:329197048ff0

add DailyRollingFileAppender
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 21 Mar 2021 00:59:58 -0600
parents
children 8a717f0b7f45
line wrap: on
line source

package goodjava.logger;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;


public final class DailyRollingFileAppender extends WriterAppender {
	private final DateFormat format;
	private String fileName;

	public DailyRollingFileAppender(Layout layout,DateFormat format) {
		super(layout,null);
		this.format = format;
	}

	public synchronized void append(LoggingEvent event) {
		String newFileName = format.format(new Date());
		if( !newFileName.equals(fileName) ) {
			fileName = newFileName;
			if( writer != null )
				close();
			try {
				writer = new FileWriter(fileName,true);
			} catch(IOException e) {
				throw new RuntimeException(e);
			}
		}
		super.append(event);
	}
}