view src/goodjava/logger/DailyRollingFileAppender.java @ 1598:c78d6a4e2d9a

web logging
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Apr 2021 15:09:51 -0600
parents 329197048ff0
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);
	}
}