1 23 24 package org.infoglue.deliver.util.webloggers; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.PrintWriter ; 29 import java.io.RandomAccessFile ; 30 import java.text.SimpleDateFormat ; 31 import java.util.ArrayList ; 32 import java.util.Calendar ; 33 import java.util.Date ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import org.infoglue.cms.util.CmsPropertyHandler; 41 42 47 48 public class IISLogger extends Logger 49 { 50 private final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(IISLogger.class.getName()); 51 52 protected static final String monthnames[] = 53 { 54 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 55 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 56 }; 57 58 protected static String noUrl = "*NOURL*"; 59 60 61 private byte msgbuf[] = null ; 62 protected RandomAccessFile log = null ; 63 protected RandomAccessFile errlog = null ; 64 protected RandomAccessFile trace = null ; 65 protected int bufsize = 8192 ; 66 protected int bufptr = 0 ; 67 protected int rotateLevel = 0 ; 68 protected byte buffer[] = null ; 69 protected int year = -1 ; 70 protected int month = -1 ; 71 protected int day = -1 ; 72 protected int hour = -1 ; 73 private Calendar cal = null ; 74 private long datestamp = -1 ; 75 private char datecache[] = { 'D','D','/','M','M','M', 76 '/','Y','Y','Y','Y',':', 77 'H','H',':','M','M',':', 78 'S','S',' ','+','0','0', 79 '0','0'}; 80 81 private static String hostAddress = null; 82 private static String hostName = null; 83 84 85 89 90 public void logRequest(HttpServletRequest request, HttpServletResponse response, String pagePath, long duration) 91 { 92 173 } 174 175 176 177 183 184 public void initialize() 185 { 186 } 187 188 191 192 IISLogger() 193 { 194 this.msgbuf = new byte[128] ; 195 } 196 197 private String headerLine1 = "#Software: Microsoft Internet Information Services 5.0"; 198 private String headerLine2 = "#Version: 1.0"; 199 private String headerLine3 = "#Date: " + new Date (); 200 private String headerLine4 = "#Fields: date time c-ip cs-username s-sitename s-computername s-ip s-port cs-method cs-uri-stem cs-uri-query sc-status sc-win32-status sc-bytes cs-bytes time-taken cs-version cs-host cs(User-Agent) cs(Cookie) cs(Referer)"; 201 202 203 private List logBuffer = new ArrayList (); 204 205 208 209 protected void writeRequest(String date, String row) 210 { 211 logBuffer.add(row); 212 213 if(logBuffer.size() > 20) 214 { 215 String logPath = CmsPropertyHandler.getStatisticsLogPath(); 216 File file = new File (logPath + File.separator + "stat" + date + ".log"); 217 boolean isFileCreated = file.exists(); 218 219 PrintWriter pout = null; 220 try 221 { 222 pout = new PrintWriter (new FileOutputStream (file, true)); 223 if(!isFileCreated) 224 { 225 pout.println(headerLine1); 226 pout.println(headerLine2); 227 pout.println(headerLine3); 228 pout.println(headerLine4); 229 } 230 231 Iterator i = logBuffer.iterator(); 232 while(i.hasNext()) 233 { 234 pout.println(i.next().toString()); 235 } 236 pout.close(); 237 } 238 catch(Exception e) 239 { 240 logger.error(e.getMessage(), e); 241 } 242 finally 243 { 244 try 245 { 246 pout.close(); 247 } 248 catch(Exception e) 249 { 250 } 251 } 252 253 logBuffer = new ArrayList (); 254 } 255 } 256 257 258 261 262 public String getCurrentDate(String pattern) 263 { 264 269 Date date = new Date (); 271 SimpleDateFormat formatter = new SimpleDateFormat (pattern); 272 String dateString = formatter.format(date); 273 return dateString; 274 } 275 276 277 public String getHostAddress() 278 { 279 if(hostAddress != null) 280 return hostAddress; 281 282 String address = null; 283 284 try 285 { 286 address = java.net.InetAddress.getLocalHost().getHostAddress(); 287 } 288 catch(Exception e) 289 { 290 logger.error(e.getMessage(), e); 291 } 292 293 hostAddress = address; 294 295 return address; 296 } 297 298 public String getHostName() 299 { 300 if(hostName != null) 301 return hostName; 302 303 String name = null; 304 305 try 306 { 307 name = java.net.InetAddress.getLocalHost().getHostName(); 308 } 309 catch(Exception e) 310 { 311 logger.error(e.getMessage(), e); 312 } 313 314 hostName = name; 315 316 return name; 317 } 318 319 320 public String defaultValueIfNull(String value) 321 { 322 if(value == null || value.equals("")) 323 return "-"; 324 325 return value; 326 } 327 } 328 329 330 331 | Popular Tags |