package org.apache.log4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatFileAppender extends FileAppender {

	private static String undecorateFileName(String filename) {

		SimpleDateFormat sdf = new SimpleDateFormat("'"+filename+"'");
		Date GMTDate = new Date(System.currentTimeMillis());
		String decoratedName = sdf.format(GMTDate);
		return decoratedName;
	}

	/**
	 * The <b>File</b> property takes a string value which should be the name
	 * of the file to append to. This version of the {@see FileAppender} will
	 * honor special decorator characters that may include date, time and so on.
	 *
	 * <p>
	 * Note: Actual opening of the file is made when {@link #activateOptions} is
	 * called, not when the options are set.
	 */
	public void setFile(String file) {
		String val = undecorateFileName(file);
		fileName = val;
	}

	public SimpleDateFormatFileAppender() {
		super();
	}

	/**
	 * Instantiate a DecoratedNameFileAppender and open the file designated by
	 * <code>filename</code>. The opened filename will become the output
	 * destination for this appender.
	 *
	 * <p>
	 * If the <code>append</code> parameter is true, the file will be appended
	 * to. Otherwise, the file designated by <code>filename</code> will be
	 * truncated before being opened.
	 */
	public SimpleDateFormatFileAppender(Layout layout, String filename,
			boolean append) throws IOException {
		super(layout, filename, append);
	}

	/**
	 * Instantiate a FileAppender and open the file designated by
	 * <code>filename</code>. The opened filename will become the output
	 * destination for this appender.
	 *
	 * <p>
	 * The file will be appended to.
	 */
	public SimpleDateFormatFileAppender(Layout layout, String filename)
			throws IOException {
		super(layout, filename);
	}
}
