1 16 17 package org.apache.log4j.helpers; 18 19 import org.apache.log4j.Layout; 20 import org.apache.log4j.helpers.RelativeTimeDateFormat; 21 import org.apache.log4j.helpers.AbsoluteTimeDateFormat; 22 import org.apache.log4j.helpers.DateTimeDateFormat; 23 import org.apache.log4j.helpers.ISO8601DateFormat; 24 import org.apache.log4j.spi.LoggingEvent; 25 import java.text.DateFormat ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Date ; 28 import java.util.TimeZone ; 29 import java.text.FieldPosition ; 30 31 32 39 abstract public class DateLayout extends Layout { 40 41 46 public final static String NULL_DATE_FORMAT = "NULL"; 47 48 52 public final static String RELATIVE_TIME_DATE_FORMAT = "RELATIVE"; 53 54 protected FieldPosition pos = new FieldPosition (0); 55 56 61 final static public String DATE_FORMAT_OPTION = "DateFormat"; 62 63 68 final static public String TIMEZONE_OPTION = "TimeZone"; 69 70 private String timeZoneID; 71 private String dateFormatOption; 72 73 protected DateFormat dateFormat; 74 protected Date date = new Date (); 75 76 80 public 81 String [] getOptionStrings() { 82 return new String [] {DATE_FORMAT_OPTION, TIMEZONE_OPTION}; 83 } 84 85 89 public 90 void setOption(String option, String value) { 91 if(option.equalsIgnoreCase(DATE_FORMAT_OPTION)) { 92 dateFormatOption = value.toUpperCase(); 93 } else if(option.equalsIgnoreCase(TIMEZONE_OPTION)) { 94 timeZoneID = value; 95 } 96 } 97 98 99 104 public 105 void setDateFormat(String dateFormat) { 106 if (dateFormat != null) { 107 dateFormatOption = dateFormat; 108 } 109 setDateFormat(dateFormatOption, TimeZone.getDefault()); 110 } 111 112 115 public 116 String getDateFormat() { 117 return dateFormatOption; 118 } 119 120 124 public 125 void setTimeZone(String timeZone) { 126 this.timeZoneID = timeZone; 127 } 128 129 132 public 133 String getTimeZone() { 134 return timeZoneID; 135 } 136 137 public 138 void activateOptions() { 139 setDateFormat(dateFormatOption); 140 if(timeZoneID != null && dateFormat != null) { 141 dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID)); 142 } 143 } 144 145 public 146 void dateFormat(StringBuffer buf, LoggingEvent event) { 147 if(dateFormat != null) { 148 date.setTime(event.timeStamp); 149 dateFormat.format(date, buf, this.pos); 150 buf.append(' '); 151 } 152 } 153 154 158 public 159 void setDateFormat(DateFormat dateFormat, TimeZone timeZone) { 160 this.dateFormat = dateFormat; 161 this.dateFormat.setTimeZone(timeZone); 162 } 163 164 178 public 179 void setDateFormat(String dateFormatType, TimeZone timeZone) { 180 if(dateFormatType == null) { 181 this.dateFormat = null; 182 return; 183 } 184 185 if(dateFormatType.equalsIgnoreCase(NULL_DATE_FORMAT)) { 186 this.dateFormat = null; 187 } else if (dateFormatType.equalsIgnoreCase(RELATIVE_TIME_DATE_FORMAT)) { 188 this.dateFormat = new RelativeTimeDateFormat(); 189 } else if(dateFormatType.equalsIgnoreCase( 190 AbsoluteTimeDateFormat.ABS_TIME_DATE_FORMAT)) { 191 this.dateFormat = new AbsoluteTimeDateFormat(timeZone); 192 } else if(dateFormatType.equalsIgnoreCase( 193 AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT)) { 194 this.dateFormat = new DateTimeDateFormat(timeZone); 195 } else if(dateFormatType.equalsIgnoreCase( 196 AbsoluteTimeDateFormat.ISO8601_DATE_FORMAT)) { 197 this.dateFormat = new ISO8601DateFormat(timeZone); 198 } else { 199 this.dateFormat = new SimpleDateFormat (dateFormatType); 200 this.dateFormat.setTimeZone(timeZone); 201 } 202 } 203 } 204 | Popular Tags |