KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > webloggers > CommonLogger


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util.webloggers;
25
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.infoglue.cms.util.CmsPropertyHandler;
39
40 /**
41  * The CommonLogger class implements the abstract Logger class.
42  * The resulting log will conform to the
43  * <a HREF="http://www.w3.org/Daemon/User/Config/Logging.html#common-logfile-format">common log format</a>).
44  */

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 JavaDoc hostAddress = null;
51     private static String JavaDoc hostName = null;
52     
53     /**
54      * Construct a new Logger instance.
55      */

56
57     public CommonLogger()
58     {
59     }
60     
61     /**
62      * Log the given HTTP transaction.
63      * Not implemented yet!!!
64      */

65
66     public void logRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc pagePath, long duration)
67     {
68         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
69         
70         sb.append(defaultValueIfNull(request.getRemoteAddr())); //c-ip
71
sb.append(" ");
72         sb.append("-"); //???
73
sb.append(" ");
74         sb.append(defaultValueIfNull(request.getRemoteUser())); //cs-username
75
sb.append(" ");
76         sb.append("[" + defaultValueIfNull(getCurrentDate("dd/MMM/yyyy:HH:mm:ss")) + " " + getOffset() + "]"); //date + time
77
sb.append(" ");
78         sb.append("\"" + request.getMethod() + " " + pagePath + " " + request.getProtocol() + "\""); //date + time
79
sb.append(" ");
80         sb.append("304"); //sc-status
81
sb.append(" ");
82         sb.append("-"); //sc-bytes
83
sb.append(" ");
84         sb.append("\"" + defaultValueIfNull(request.getHeader("Referer")) + "\""); //cs(Referer)
85
sb.append(" ");
86         sb.append("\"" + defaultValueIfNull(request.getHeader("User-Agent")) + "\""); //cs(User-Agent)
87

88         writeRequest(getCurrentDate("yyyy-MM-dd"), sb.toString());
89     }
90
91
92         
93     /**
94      * Initialize this logger for the given server.
95      * This method gets the server properties describe above to
96      * initialize its various log files.
97      * @param server The server to which thiss logger should initialize.
98      */

99
100     public void initialize()
101     {
102     }
103         
104     private List JavaDoc logBuffer = new ArrayList JavaDoc();
105     
106     /**
107      * This method writes a request to the logfile
108      */

109     
110     protected synchronized void writeRequest(String JavaDoc date, String JavaDoc row)
111     {
112         synchronized(logBuffer)
113         {
114             logBuffer.add(row);
115             
116             if(logBuffer.size() > 100)
117             {
118                 String JavaDoc logPath = CmsPropertyHandler.getStatisticsLogPath();
119                 String JavaDoc statisticsLogOneFilePerDay = CmsPropertyHandler.getStatisticsLogOneFilePerDay();
120                 File JavaDoc file = new File JavaDoc(logPath + File.separator + "statistics.log");
121                 if(statisticsLogOneFilePerDay != null && statisticsLogOneFilePerDay.equalsIgnoreCase("true"))
122                     file = new File JavaDoc(logPath + File.separator + "stat" + date + ".log");
123     
124                 boolean isFileCreated = file.exists();
125                     
126                 PrintWriter JavaDoc pout = null;
127                 try
128                 {
129                     pout = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(file, true));
130                     if(!isFileCreated)
131                     {
132                     }
133                 
134                     Iterator JavaDoc i = logBuffer.iterator();
135                     while(i.hasNext())
136                     {
137                         pout.println(i.next().toString());
138                     }
139                 
140                     pout.close();
141                 }
142                 catch(Exception JavaDoc e)
143                 {
144                     logger.error(e.getMessage(), e);
145                 }
146                 finally
147                 {
148                     try
149                     {
150                         pout.close();
151                     }
152                     catch(Exception JavaDoc e)
153                     {
154                     }
155                 }
156                 
157                 logBuffer = new ArrayList JavaDoc();
158             }
159         }
160     }
161     
162     
163     /**
164      * This method returns a date as a string.
165      */

166     
167     public String JavaDoc getCurrentDate(String JavaDoc pattern)
168     {
169         /*
170         SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
171         pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
172         pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
173         */

174         // Format the current time.
175
Date JavaDoc date = new Date JavaDoc();
176         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern);
177         String JavaDoc dateString = formatter.format(date);
178         return dateString;
179     }
180     
181     
182     private String JavaDoc getOffset()
183     {
184         java.util.Calendar JavaDoc cal = java.util.Calendar.getInstance();
185         String JavaDoc 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 JavaDoc getHostAddress()
206     {
207         if(hostAddress != null)
208             return hostAddress;
209             
210         String JavaDoc address = null;
211         
212         try
213         {
214             address = java.net.InetAddress.getLocalHost().getHostAddress();
215         }
216         catch(Exception JavaDoc e)
217         {
218             logger.error(e.getMessage(), e);
219         }
220         
221         hostAddress = address;
222         
223         return address;
224     }
225
226     public String JavaDoc getHostName()
227     {
228         if(hostName != null)
229             return hostName;
230
231         String JavaDoc name = null;
232         
233         try
234         {
235             name = java.net.InetAddress.getLocalHost().getHostName();
236         }
237         catch(Exception JavaDoc e)
238         {
239             logger.error(e.getMessage(), e);
240         }
241         
242         hostName = name;
243         
244         return name;
245     }
246
247     
248     public String JavaDoc defaultValueIfNull(String JavaDoc value)
249     {
250         if(value == null || value.equals(""))
251             return "-";
252     
253         return value;
254     }
255 }
256
257
258
259
Popular Tags