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.
  • [get | view] (2005-03-22 06:35:00, 13.7 KB) [[attachment:ConfigurationServlet.java]]
  • [get | view] (2005-09-18 07:15:29, 6.0 KB) [[attachment:IRCAppender.java]]
  • [get | view] (2006-02-23 13:25:49, 1.7 KB) [[attachment:LevelPatternLayout.java]]
  • [get | view] (2006-02-23 13:25:29, 2.3 KB) [[attachment:LevelPatternParser.java]]
  • [get | view] (2006-10-02 14:54:47, 1.2 KB) [[attachment:TimestampFileAppender.java]]
  • [get | view] (2005-09-18 07:17:01, 8.5 KB) [[attachment:irc-appender.jar]]
  • [get | view] (2005-09-18 07:20:12, 0.8 KB) [[attachment:log4j-irc-appender.xml]]
  • [get | view] (2006-10-02 14:55:18, 0.5 KB) [[attachment:timestampFileAppender.configuration]]
 All files | Selected Files: delete move to page

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