1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
package net.sourceforge.pebble.logging; |
33 | |
|
34 | |
import net.sourceforge.pebble.domain.Blog; |
35 | |
import net.sourceforge.pebble.Constants; |
36 | |
|
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 | |
|
47 | |
|
48 | |
|
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 | |
|
57 | 2724 | private SimpleDateFormat filenameFormat = new SimpleDateFormat("'blog-'yyyyMMdd'.log'"); |
58 | |
|
59 | 2724 | private List entries = new ArrayList(); |
60 | |
|
61 | |
public CombinedLogFormatLogger(Blog blog) { |
62 | 2724 | super(blog); |
63 | 2724 | filenameFormat.setTimeZone(blog.getTimeZone()); |
64 | 2724 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public synchronized void log(HttpServletRequest request, int status) { |
72 | 0 | String externalUri = (String)request.getAttribute(Constants.EXTERNAL_URI); |
73 | 0 | LogEntry entry = new LogEntry(); |
74 | 0 | entry.setHost(request.getRemoteAddr()); |
75 | 0 | entry.setDate(blog.getCalendar().getTime()); |
76 | 0 | entry.setStatusCode(status); |
77 | 0 | StringBuffer buf = new StringBuffer(); |
78 | 0 | buf.append(request.getMethod()); |
79 | 0 | buf.append(" "); |
80 | 0 | buf.append(externalUri); |
81 | 0 | entry.setRequest(buf.toString()); |
82 | 0 | entry.setReferer(request.getHeader(REFERER_HEADER)); |
83 | 0 | entry.setAgent(request.getHeader(USER_AGENT_HEADER)); |
84 | 0 | entries.add(entry); |
85 | |
|
86 | 0 | if (entries.size() >= FLUSH_SIZE) { |
87 | 0 | flush(); |
88 | |
} |
89 | 0 | } |
90 | |
|
91 | |
private void flush() { |
92 | |
try { |
93 | 2696 | write(entries); |
94 | 2696 | entries.clear(); |
95 | 0 | } catch (IOException ioe) { |
96 | 0 | ioe.printStackTrace(); |
97 | 2696 | } |
98 | 2696 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public void start() { |
104 | 2692 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public synchronized void stop() { |
110 | 2696 | flush(); |
111 | 2696 | } |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public String getLogFile(int year, int month, int day) { |
122 | 0 | StringBuffer buf = new StringBuffer(); |
123 | |
try { |
124 | |
|
125 | 0 | File file = new File(blog.getLogsDirectory(), getFilename(year, month, day)); |
126 | 0 | if (file.exists()) { |
127 | 0 | BufferedReader reader = new BufferedReader(new FileReader(file)); |
128 | 0 | String line = reader.readLine(); |
129 | 0 | while (line != null) { |
130 | 0 | buf.append(line); |
131 | 0 | buf.append(System.getProperty("line.separator")); |
132 | 0 | line = reader.readLine(); |
133 | |
} |
134 | 0 | reader.close(); |
135 | |
} |
136 | 0 | } catch (Exception e) { |
137 | 0 | e.printStackTrace(); |
138 | |
} finally { |
139 | 0 | return buf.toString(); |
140 | |
} |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public Log getLog(int year, int month, int day) { |
152 | 0 | List logEntries = new ArrayList(); |
153 | 0 | CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog); |
154 | |
|
155 | |
try { |
156 | |
|
157 | 0 | File file = new File(blog.getLogsDirectory(), getFilename(year, month, day)); |
158 | 0 | if (file.exists()) { |
159 | 0 | BufferedReader reader = new BufferedReader(new FileReader(file)); |
160 | 0 | String line = reader.readLine(); |
161 | 0 | while (line != null) { |
162 | 0 | logEntries.add(format.parse(line)); |
163 | 0 | line = reader.readLine(); |
164 | |
} |
165 | 0 | reader.close(); |
166 | |
} |
167 | 0 | } catch (Exception e) { |
168 | 0 | e.printStackTrace(); |
169 | |
} finally { |
170 | 0 | return new Log(blog, logEntries); |
171 | |
} |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public LogSummary getLogSummary(int year, int month, int day) { |
183 | 1460 | Calendar cal = blog.getCalendar(); |
184 | 1460 | cal.set(Calendar.YEAR, year); |
185 | 1460 | cal.set(Calendar.MONTH, month-1); |
186 | 1460 | cal.set(Calendar.DAY_OF_MONTH, day); |
187 | 1460 | int totalRequests = 0; |
188 | |
|
189 | |
try { |
190 | |
|
191 | 1460 | File file = new File(blog.getLogsDirectory(), getFilename(year, month, day)); |
192 | 1460 | if (file.exists()) { |
193 | 0 | BufferedReader reader = new BufferedReader(new FileReader(file)); |
194 | 0 | String line = reader.readLine(); |
195 | 0 | while (line != null) { |
196 | 0 | totalRequests++; |
197 | 0 | line = reader.readLine(); |
198 | |
} |
199 | 0 | reader.close(); |
200 | |
} |
201 | 0 | } catch (Exception e) { |
202 | 0 | e.printStackTrace(); |
203 | 1460 | } |
204 | |
|
205 | 1460 | return new LogSummaryItem(blog, cal.getTime(), totalRequests); |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
private String getFilename(int year, int month, int day) { |
217 | 1460 | Calendar cal = blog.getCalendar(); |
218 | 1460 | cal.set(Calendar.YEAR, year); |
219 | 1460 | cal.set(Calendar.MONTH, month-1); |
220 | 1460 | cal.set(Calendar.DAY_OF_MONTH, day); |
221 | |
|
222 | 1460 | return filenameFormat.format(cal.getTime()); |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
private void write(List entries) throws IOException { |
232 | 2696 | CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog); |
233 | |
File file; |
234 | 2696 | BufferedWriter writer = null; |
235 | 2696 | String currentFilename = ""; |
236 | |
String filename; |
237 | 2696 | Iterator it = entries.iterator(); |
238 | 2696 | while (it.hasNext()) { |
239 | 0 | LogEntry entry = (LogEntry)it.next(); |
240 | 0 | filename = filenameFormat.format(entry.getDate()); |
241 | 0 | if (!filename.equals(currentFilename)) { |
242 | |
|
243 | 0 | if (writer != null) { |
244 | 0 | writer.flush(); |
245 | 0 | writer.close(); |
246 | |
} |
247 | |
|
248 | |
|
249 | 0 | currentFilename = filename; |
250 | 0 | file = new File(blog.getLogsDirectory(), currentFilename); |
251 | 0 | writer = new BufferedWriter(new FileWriter(file, true)); |
252 | |
} |
253 | |
|
254 | 0 | writer.write(format.format(entry)); |
255 | 0 | writer.newLine(); |
256 | 0 | } |
257 | |
|
258 | 2696 | if (writer != null) { |
259 | 0 | writer.flush(); |
260 | 0 | writer.close(); |
261 | |
} |
262 | 2696 | } |
263 | |
|
264 | |
} |