1 16 17 18 19 package org.apache.log4j; 20 21 import java.io.IOException ; 22 import java.io.Writer ; 23 import java.io.FileWriter ; 24 import java.io.File ; 25 import java.io.OutputStream ; 26 import java.io.OutputStreamWriter ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 import org.apache.log4j.helpers.OptionConverter; 30 import org.apache.log4j.helpers.QuietWriter; 31 import org.apache.log4j.helpers.CountingQuietWriter; 32 import org.apache.log4j.spi.LoggingEvent; 33 import org.apache.log4j.spi.ErrorCode; 34 35 57 public class DailyFileAppender extends FileAppender { 58 59 64 static final public String FILE_NAME_PATTERN_OPTION = "FilePattern"; 65 66 69 private String fileNamePattern = null; 70 71 74 private String currentFileName = null; 75 76 79 private long nextFilenameComputingMillis = System.currentTimeMillis () - 1; 80 81 84 public 85 DailyFileAppender() { 86 } 87 88 97 public DailyFileAppender (Layout layout,String filename,boolean append) throws IOException { 98 super(layout, filename, append); 99 } 100 101 107 public DailyFileAppender (Layout layout,String filename) throws IOException { 108 super(layout, filename); 109 } 110 111 120 121 public 122 synchronized 123 void setFile(String fileName, boolean append) throws IOException { 124 125 126 if (fileNamePattern == null) { 127 errorHandler.error("Missing file pattern (" + FILE_NAME_PATTERN_OPTION + ") in setFile()."); 128 return; 129 } 130 131 Date now = new Date (); 132 133 fileName = new SimpleDateFormat (fileNamePattern).format (now); 134 if (fileName.equals(currentFileName)) 135 return; 136 137 138 DailyFileAppenderCalendar c = new DailyFileAppenderCalendar(); 139 c.rollToNextDay (); 140 nextFilenameComputingMillis = c.getTimeInMillis (); 141 142 currentFileName = fileName; 143 144 super.setFile(fileName, append); 145 } 146 147 152 protected 153 void subAppend(LoggingEvent event) { 154 155 if (System.currentTimeMillis () >= nextFilenameComputingMillis) { 156 try { 157 setFile (super.fileName, super.fileAppend); 158 } 159 catch(IOException e) { 160 System.err.println("setFile(null, false) call failed."); 161 e.printStackTrace(); 162 } 163 } 164 165 super.subAppend(event); 166 } 167 168 174 public 175 String [] getOptionStrings() { 176 177 return OptionConverter.concatanateArrays(super.getOptionStrings(), 178 new String [] {FILE_NAME_PATTERN_OPTION}); 179 } 180 181 184 public 185 void setOption(String key, String value) { 186 super.setOption(key, value); 187 if(key.equalsIgnoreCase(FILE_NAME_PATTERN_OPTION)) { 188 fileNamePattern = value; 189 } 190 } 191 192 198 public 199 void activateOptions() { 200 try { 201 setFile(null, super.fileAppend); 202 } 203 catch(java.io.IOException e) { 204 errorHandler.error("setFile(null,"+fileAppend+") call failed.", 205 e, ErrorCode.FILE_OPEN_FAILURE); 206 } 207 } 208 } 209 210 221 class DailyFileAppenderCalendar extends java.util.GregorianCalendar 222 { 223 226 public long getTimeInMillis() { 227 return super.getTimeInMillis(); 228 } 229 230 234 public void rollToNextDay () { 235 this.add(java.util.Calendar.DATE, 0); 236 this.add(java.util.Calendar.HOUR, 0); 237 this.set(java.util.Calendar.MINUTE, 0); 238 this.set(java.util.Calendar.SECOND, 0); 239 this.set(java.util.Calendar.MILLISECOND, 0); 240 } 241 } | Popular Tags |