File Source: combinedlogformatlogger.java
/*
P/P * Method: net.sourceforge.pebble.logging.CombinedLogFormatLogger__static_init
*/
1 /*
2 * Copyright (c) 2003-2006, Simon Brown
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * - Neither the name of Pebble nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.sourceforge.pebble.logging;
33
34 import net.sourceforge.pebble.domain.Blog;
35 import net.sourceforge.pebble.Constants;
36
/*
P/P * Method: void net.sourceforge.pebble.logging.CombinedLogFormatLogger(Blog)
*
* Preconditions:
* blog != null
*
* Postconditions:
* this.blog == blog
* this.blog != null
* this.entries == &new ArrayList(CombinedLogFormatLogger#2)
* this.filenameFormat == &new SimpleDateFormat(CombinedLogFormatLogger#1)
* new ArrayList(CombinedLogFormatLogger#2) num objects == 1
* new SimpleDateFormat(CombinedLogFormatLogger#1) num objects == 1
*/
37 import javax.servlet.http.HttpServletRequest;
38 import java.io.*;
39 import java.text.SimpleDateFormat;
40 import java.util.ArrayList;
41 import java.util.Calendar;
42 import java.util.Iterator;
43 import java.util.List;
44
45 /**
46 * Supports the <a href="http://httpd.apache.org/docs/logs.html#combined">Combined Log Format</a>.
47 *
48 * @author Simon Brown
49 */
50 public class CombinedLogFormatLogger extends AbstractLogger {
51
52 private static final String REFERER_HEADER = "Referer";
53 private static final String USER_AGENT_HEADER = "User-Agent";
54 private static final int FLUSH_SIZE = 0;
55
56 /** the format of the log filenames */
57 private SimpleDateFormat filenameFormat = new SimpleDateFormat("'blog-'yyyyMMdd'.log'");
58
59 private List entries = new ArrayList();
60
61 public CombinedLogFormatLogger(Blog blog) {
62 super(blog);
63 filenameFormat.setTimeZone(blog.getTimeZone());
64 }
65
66 /**
67 * Logs a HTTP request.
68 *
69 * @param request a HttpServletRequest
70 */
71 public synchronized void log(HttpServletRequest request, int status) {
/*
P/P * Method: void log(HttpServletRequest, int)
*
* Preconditions:
* request != null
* this.blog != null
* this.entries != null
* (soft) this.filenameFormat != null
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getCalendar(...)@75 != null
*
* Test Vectors:
* java.util.List:size(...)@86: {-231..-1}, {0..232-1}
*/
72 String externalUri = (String)request.getAttribute(Constants.EXTERNAL_URI);
73 LogEntry entry = new LogEntry();
74 entry.setHost(request.getRemoteAddr());
75 entry.setDate(blog.getCalendar().getTime());
76 entry.setStatusCode(status);
77 StringBuffer buf = new StringBuffer();
78 buf.append(request.getMethod());
79 buf.append(" ");
80 buf.append(externalUri);
81 entry.setRequest(buf.toString());
82 entry.setReferer(request.getHeader(REFERER_HEADER));
83 entry.setAgent(request.getHeader(USER_AGENT_HEADER));
84 entries.add(entry);
85
86 if (entries.size() >= FLUSH_SIZE) {
87 flush();
88 }
89 }
90
/*
P/P * Method: void flush()
*
* Preconditions:
* (soft) this.blog != null
* (soft) this.entries != null
* (soft) this.filenameFormat != null
*/
91 private void flush() {
92 try {
93 write(entries);
94 entries.clear();
95 } catch (IOException ioe) {
96 ioe.printStackTrace();
97 }
98 }
99
100 /**
101 * Called to start this logger.
102 */
/*
P/P * Method: void start()
*/
103 public void start() {
104 }
105
106 /**
107 * Called to stop this logger.
108 */
/*
P/P * Method: void stop()
*
* Preconditions:
* (soft) this.blog != null
* (soft) this.entries != null
* (soft) this.filenameFormat != null
*/
109 public synchronized void stop() {
110 flush();
111 }
112
113 /**
114 * Gets a copy of the log file for a given year, month and day.
115 *
116 * @param year the year to get entries for
117 * @param month the month to get entries for
118 * @param day the day to get entries for
119 * @return a String containing the contents of the requested log file
120 */
/*
P/P * Method: String getLogFile(int, int, int)
*
* Preconditions:
* (soft) month >= -231+1
* (soft) this.blog != null
* (soft) this.filenameFormat != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.io.File:exists(...)@126: {0}, {1}
*/
121 public String getLogFile(int year, int month, int day) {
122 StringBuffer buf = new StringBuffer();
123 try {
124 // read the file a line at a time, creating a String as we go
125 File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
126 if (file.exists()) {
127 BufferedReader reader = new BufferedReader(new FileReader(file));
128 String line = reader.readLine();
129 while (line != null) {
130 buf.append(line);
131 buf.append(System.getProperty("line.separator"));
132 line = reader.readLine();
133 }
134 reader.close();
135 }
136 } catch (Exception e) {
137 e.printStackTrace();
138 } finally {
139 return buf.toString();
140 }
141 }
142
143 /**
144 * Gets the log for a given year, month and day.
145 *
146 * @param year the year to get entries for
147 * @param month the month to get entries for
148 * @param day the day to get entries for
149 * @return a Log object
150 */
/*
P/P * Method: Log getLog(int, int, int)
*
* Preconditions:
* this.blog != null
* (soft) month >= -231+1
* (soft) this.filenameFormat != null
*
* Postconditions:
* return_value == &new Log(getLog#6)
* new ArrayList(Log#1) num objects == 0
* new ArrayList(getLog#1) num objects == 1
* new Log(getLog#6) num objects == 1
* return_value.blog == this.blog
* return_value.blog != null
* return_value.logEntries == &new ArrayList(getLog#1)
*
* Test Vectors:
* java.io.File:exists(...)@158: {0}, {1}
*/
151 public Log getLog(int year, int month, int day) {
152 List logEntries = new ArrayList();
153 CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog);
154
155 try {
156 // read the file a line at a time, parsing into LogEntry objects
157 File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
158 if (file.exists()) {
159 BufferedReader reader = new BufferedReader(new FileReader(file));
160 String line = reader.readLine();
161 while (line != null) {
+ 162 logEntries.add(format.parse(line));
163 line = reader.readLine();
164 }
165 reader.close();
166 }
167 } catch (Exception e) {
168 e.printStackTrace();
169 } finally {
170 return new Log(blog, logEntries);
171 }
172 }
173
174 /**
175 * Gets the log summary information for the given year, month and day.
176 *
177 * @param year the year to get entries for
178 * @param month the month to get entries for
179 * @param day the day to get entries for
180 * @return a LogSummary object
181 */
/*
P/P * Method: LogSummary getLogSummary(int, int, int)
*
* Preconditions:
* month >= -231+1
* this.blog != null
* (soft) this.filenameFormat != null
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getCalendar(...)@183 != null
*
* Postconditions:
* return_value == &new LogSummaryItem(getLogSummary#4)
* new LogSummaryItem(getLogSummary#4) num objects == 1
* return_value.blog == this.blog
* return_value.blog != null
* init'ed(return_value.date)
* return_value.totalRequests >= 0
*
* Test Vectors:
* java.io.File:exists(...)@192: {0}, {1}
*/
182 public LogSummary getLogSummary(int year, int month, int day) {
183 Calendar cal = blog.getCalendar();
184 cal.set(Calendar.YEAR, year);
185 cal.set(Calendar.MONTH, month-1);
186 cal.set(Calendar.DAY_OF_MONTH, day);
187 int totalRequests = 0;
188
189 try {
190 // read the file a line at a time
191 File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
192 if (file.exists()) {
193 BufferedReader reader = new BufferedReader(new FileReader(file));
194 String line = reader.readLine();
195 while (line != null) {
+ 196 totalRequests++;
197 line = reader.readLine();
198 }
199 reader.close();
200 }
201 } catch (Exception e) {
202 e.printStackTrace();
203 }
204
205 return new LogSummaryItem(blog, cal.getTime(), totalRequests);
206 }
207
208 /**
209 * Determines the name of the log file.
210 *
211 * @param year the year to get entries for
212 * @param month the month to get entries for
213 * @param day the day to get entries for
214 * @return the name of the log file for the given year, month and day
215 */
/*
P/P * Method: String getFilename(int, int, int)
*
* Preconditions:
* month >= -231+1
* this.blog != null
* this.filenameFormat != null
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getCalendar(...)@217 != null
*
* Postconditions:
* init'ed(return_value)
*/
216 private String getFilename(int year, int month, int day) {
217 Calendar cal = blog.getCalendar();
218 cal.set(Calendar.YEAR, year);
219 cal.set(Calendar.MONTH, month-1);
220 cal.set(Calendar.DAY_OF_MONTH, day);
221
222 return filenameFormat.format(cal.getTime());
223 }
224
225 /**
226 * Writes the given list of entries to the log file, creating it if
227 * necessary.
228 *
229 * @param entries the list of entries to write
230 */
/*
P/P * Method: void write(List)
*
* Preconditions:
* entries != null
* this.blog != null
* (soft) this.filenameFormat != null
*
* Presumptions:
* java.text.SimpleDateFormat:format(...)@240 != null
* java.util.Iterator:next(...)@239 != null
*
* Test Vectors:
* java.lang.String:equals(...)@241: {1}, {0}
* java.util.Iterator:hasNext(...)@238: {1}, {0}
*/
231 private void write(List entries) throws IOException {
232 CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog);
233 File file;
234 BufferedWriter writer = null;
235 String currentFilename = "";
236 String filename;
237 Iterator it = entries.iterator();
238 while (it.hasNext()) {
239 LogEntry entry = (LogEntry)it.next();
240 filename = filenameFormat.format(entry.getDate());
241 if (!filename.equals(currentFilename)) {
242 // close the old file (if there is one)
243 if (writer != null) {
244 writer.flush();
245 writer.close();
246 }
247
248 // and open a new file
249 currentFilename = filename;
250 file = new File(blog.getLogsDirectory(), currentFilename);
251 writer = new BufferedWriter(new FileWriter(file, true));
252 }
253
+ 254 writer.write(format.format(entry));
255 writer.newLine();
256 }
257
258 if (writer != null) {
259 writer.flush();
260 writer.close();
261 }
262 }
263
264 }
SofCheck Inspector Build Version : 2.22510
| combinedlogformatlogger.java |
2010-Jun-25 19:40:32 |
| combinedlogformatlogger.class |
2010-Jul-19 20:23:38 |