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.text.SimpleDateFormat ; 30 import java.util.ArrayList ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.infoglue.cms.util.CmsPropertyHandler; 39 40 45 46 public class CommonLogger extends org.infoglue.deliver.util.webloggers.Logger 47 { 48 private final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CommonLogger.class.getName()); 49 50 private static String hostAddress = null; 51 private static String hostName = null; 52 53 56 57 public CommonLogger() 58 { 59 } 60 61 65 66 public void logRequest(HttpServletRequest request, HttpServletResponse response, String pagePath, long duration) 67 { 68 StringBuffer sb = new StringBuffer (); 69 70 sb.append(defaultValueIfNull(request.getRemoteAddr())); sb.append(" "); 72 sb.append("-"); sb.append(" "); 74 sb.append(defaultValueIfNull(request.getRemoteUser())); sb.append(" "); 76 sb.append("[" + defaultValueIfNull(getCurrentDate("dd/MMM/yyyy:HH:mm:ss")) + " " + getOffset() + "]"); sb.append(" "); 78 sb.append("\"" + request.getMethod() + " " + pagePath + " " + request.getProtocol() + "\""); sb.append(" "); 80 sb.append("304"); sb.append(" "); 82 sb.append("-"); sb.append(" "); 84 sb.append("\"" + defaultValueIfNull(request.getHeader("Referer")) + "\""); sb.append(" "); 86 sb.append("\"" + defaultValueIfNull(request.getHeader("User-Agent")) + "\""); 88 writeRequest(getCurrentDate("yyyy-MM-dd"), sb.toString()); 89 } 90 91 92 93 99 100 public void initialize() 101 { 102 } 103 104 private List logBuffer = new ArrayList (); 105 106 109 110 protected synchronized void writeRequest(String date, String row) 111 { 112 synchronized(logBuffer) 113 { 114 logBuffer.add(row); 115 116 if(logBuffer.size() > 100) 117 { 118 String logPath = CmsPropertyHandler.getStatisticsLogPath(); 119 String statisticsLogOneFilePerDay = CmsPropertyHandler.getStatisticsLogOneFilePerDay(); 120 File file = new File (logPath + File.separator + "statistics.log"); 121 if(statisticsLogOneFilePerDay != null && statisticsLogOneFilePerDay.equalsIgnoreCase("true")) 122 file = new File (logPath + File.separator + "stat" + date + ".log"); 123 124 boolean isFileCreated = file.exists(); 125 126 PrintWriter pout = null; 127 try 128 { 129 pout = new PrintWriter (new FileOutputStream (file, true)); 130 if(!isFileCreated) 131 { 132 } 133 134 Iterator i = logBuffer.iterator(); 135 while(i.hasNext()) 136 { 137 pout.println(i.next().toString()); 138 } 139 140 pout.close(); 141 } 142 catch(Exception e) 143 { 144 logger.error(e.getMessage(), e); 145 } 146 finally 147 { 148 try 149 { 150 pout.close(); 151 } 152 catch(Exception e) 153 { 154 } 155 } 156 157 logBuffer = new ArrayList (); 158 } 159 } 160 } 161 162 163 166 167 public String getCurrentDate(String pattern) 168 { 169 174 Date date = new Date (); 176 SimpleDateFormat formatter = new SimpleDateFormat (pattern); 177 String dateString = formatter.format(date); 178 return dateString; 179 } 180 181 182 private String getOffset() 183 { 184 java.util.Calendar cal = java.util.Calendar.getInstance(); 185 String offsetString = ""; 186 int offset = (cal.get(java.util.Calendar.ZONE_OFFSET) + cal.get(java.util.Calendar.DST_OFFSET)) / (60*1000*60); 187 if(offset < 10 && offset > -10) 188 { 189 if(offset > 0) 190 offsetString = "+0" + offset + "00"; 191 else 192 offsetString = "-0" + offset + "00"; 193 } 194 else 195 { 196 if(offset > 0) 197 offsetString = "+" + offset + "00"; 198 else 199 offsetString = "-" + offset + "00"; 200 } 201 202 return offsetString; 203 } 204 205 public String getHostAddress() 206 { 207 if(hostAddress != null) 208 return hostAddress; 209 210 String address = null; 211 212 try 213 { 214 address = java.net.InetAddress.getLocalHost().getHostAddress(); 215 } 216 catch(Exception e) 217 { 218 logger.error(e.getMessage(), e); 219 } 220 221 hostAddress = address; 222 223 return address; 224 } 225 226 public String getHostName() 227 { 228 if(hostName != null) 229 return hostName; 230 231 String name = null; 232 233 try 234 { 235 name = java.net.InetAddress.getLocalHost().getHostName(); 236 } 237 catch(Exception e) 238 { 239 logger.error(e.getMessage(), e); 240 } 241 242 hostName = name; 243 244 return name; 245 } 246 247 248 public String defaultValueIfNull(String value) 249 { 250 if(value == null || value.equals("")) 251 return "-"; 252 253 return value; 254 } 255 } 256 257 258 259 | Popular Tags |