1 18 package org.objectweb.util.monolog.wrapper.log4j; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.text.DateFormat ; 23 import java.util.Calendar ; 24 import java.util.Date ; 25 26 import org.apache.log4j.helpers.LogLog; 27 import org.apache.log4j.spi.LoggingEvent; 28 29 36 public class DayFileHandler extends FileHandler { 37 38 private int lastDay; 39 40 Calendar calendar = Calendar.getInstance(); 41 42 public DayFileHandler() { 43 super(); 44 } 45 46 public DayFileHandler(String name) { 47 super(name); 48 } 49 50 protected void subAppend(LoggingEvent event) { 51 super.subAppend(event); 52 int currentDay = calendar.get(Calendar.DATE); 53 if (fileName != null && currentDay != lastDay) { 54 this.closeWriter(); Date d = calendar.getTime(); 58 StringBuffer sb = new StringBuffer (); 59 sb.append("_"); 60 sb.append(DateFormat.getDateInstance( 61 DateFormat.YEAR_FIELD).format(d)); 62 sb.append("_"); 63 sb.append(DateFormat.getDateInstance( 64 DateFormat.MONTH_FIELD).format(d)); 65 sb.append("_"); 66 sb.append(DateFormat.getDateInstance( 67 DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD).format(d)); 68 sb.append("_"); 69 sb.append(fileName); 70 File file = new File (sb.toString()); 71 try { 72 this.setFile(fileName, false, bufferedIO, bufferSize); 75 } catch (IOException e) { 76 LogLog.error("setFile(" + fileName + ", false) call failed.", e); 77 } 78 lastDay = currentDay; 79 } 80 } 81 82 public void activateOptions() { 83 lastDay = calendar.get(Calendar.DATE); 84 super.activateOptions(); 85 } 86 } 87 | Popular Tags |