Attachment 'TimestampFileAppender.java'

Download

   1 import java.io.IOException;
   2 import java.text.SimpleDateFormat;
   3 import java.util.Calendar;
   4 
   5 import org.apache.log4j.FileAppender;
   6 
   7 /**
   8  * @author Viktor Bresan
   9  *
  10  */
  11 public class TimestampFileAppender extends FileAppender {
  12 
  13     private static final String TARGET = "\\{timestamp\\}";
  14 
  15     protected String timestampPattern = null;
  16 
  17     /**
  18      *
  19      */
  20     public void setFile(String file) {
  21 
  22     	if (timestampPattern != null) {
  23     		super.setFile(file.replaceAll(TARGET, new SimpleDateFormat(timestampPattern).format(Calendar.getInstance().getTime())));
  24 		} else {
  25 			super.setFile(file);
  26 		}
  27     }
  28 
  29     /**
  30      *
  31      */
  32     public void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) throws IOException {
  33 
  34     	if (timestampPattern != null) {
  35     		super.setFile(fileName.replaceAll(TARGET, new SimpleDateFormat(timestampPattern).format(Calendar.getInstance().getTime())), append, bufferedIO, bufferSize);
  36     	} else {
  37     		super.setFile(fileName, append, bufferedIO, bufferSize);
  38     	}
  39     }
  40 
  41     /**
  42      *
  43      */
  44 	public String getTimestampPattern() {
  45 		return timestampPattern;
  46 	}
  47 
  48 	/**
  49 	 *
  50 	 */
  51 	public void setTimestampPattern(String timestampPattern) {
  52 		this.timestampPattern = timestampPattern;
  53 	}
  54 
  55 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.