KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > accesslog > SimpleAccessLogger


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.accesslog;
8
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.text.DateFormat JavaDoc;
15 import java.text.SimpleDateFormat JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import winstone.AccessLogger;
20 import winstone.Logger;
21 import winstone.WebAppConfiguration;
22 import winstone.WinstoneRequest;
23 import winstone.WinstoneResourceBundle;
24 import winstone.WinstoneResponse;
25
26 /**
27  * Simulates an apache "combined" style logger, which logs User-Agent, Referer, etc
28  *
29  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
30  * @version $Id: SimpleAccessLogger.java,v 1.5 2006/03/24 17:24:19 rickknowles Exp $
31  */

32 public class SimpleAccessLogger implements AccessLogger {
33
34     public static final WinstoneResourceBundle ACCESSLOG_RESOURCES =
35             new WinstoneResourceBundle("winstone.accesslog.LocalStrings");
36     
37     private static final DateFormat JavaDoc DF = new SimpleDateFormat JavaDoc("dd/MMM/yyyy:HH:mm:ss Z");
38     private static final String JavaDoc COMMON = "###ip### - ###user### ###time### \"###uriLine###\" ###status### ###size###";
39     private static final String JavaDoc COMBINED = COMMON + " \"###referer###\" \"###userAgent###\"";
40     private static final String JavaDoc RESIN = COMMON + " \"###userAgent###\"";
41     
42 // private WebAppConfiguration webAppConfig;
43
private OutputStream JavaDoc outStream;
44     private PrintWriter JavaDoc outWriter;
45     private String JavaDoc pattern;
46     private String JavaDoc fileName;
47     
48     public SimpleAccessLogger(WebAppConfiguration webAppConfig, Map JavaDoc startupArgs)
49             throws IOException JavaDoc {
50 // this.webAppConfig = webAppConfig;
51

52         // Get pattern
53
String JavaDoc patternType = WebAppConfiguration.stringArg(startupArgs, "simpleAccessLogger.format", "combined");
54         if (patternType.equalsIgnoreCase("combined")) {
55             this.pattern = COMBINED;
56         } else if (patternType.equalsIgnoreCase("common")) {
57             this.pattern = COMMON;
58         } else if (patternType.equalsIgnoreCase("resin")) {
59             this.pattern = RESIN;
60         } else {
61             this.pattern = patternType;
62         }
63         
64         // Get filename
65
String JavaDoc filePattern = WebAppConfiguration.stringArg(startupArgs, "simpleAccessLogger.file",
66                 "logs/###host###/###webapp###_access.log");
67         this.fileName = WinstoneResourceBundle.globalReplace(filePattern,
68                 new String JavaDoc [][] {{"###host###", webAppConfig.getOwnerHostname()},
69                     {"###webapp###", webAppConfig.getContextName()}});
70         
71         File JavaDoc file = new File JavaDoc(this.fileName);
72         file.getParentFile().mkdirs();
73         this.outStream = new FileOutputStream JavaDoc(file, true);
74         this.outWriter = new PrintWriter JavaDoc(this.outStream, true);
75         
76         Logger.log(Logger.DEBUG, ACCESSLOG_RESOURCES, "SimpleAccessLogger.Init",
77                 new String JavaDoc[] {this.fileName, patternType});
78     }
79     
80     public void log(String JavaDoc originalURL, WinstoneRequest request, WinstoneResponse response) {
81         String JavaDoc uriLine = request.getMethod() + " " + originalURL + " " + request.getProtocol();
82         int status = response.getErrorStatusCode() == null ? response.getStatus()
83                 : response.getErrorStatusCode().intValue();
84         int size = response.getWinstoneOutputStream().getBytesCommitted();
85         String JavaDoc date = null;
86         synchronized (DF) {
87             date = DF.format(new Date JavaDoc());
88         }
89         String JavaDoc logLine = WinstoneResourceBundle.globalReplace(this.pattern, new String JavaDoc[][] {
90                 {"###ip###", request.getRemoteHost()},
91                 {"###user###", nvl(request.getRemoteUser())},
92                 {"###time###", "[" + date + "]"},
93                 {"###uriLine###", uriLine},
94                 {"###status###", "" + status},
95                 {"###size###", "" + size},
96                 {"###referer###", nvl(request.getHeader("Referer"))},
97                 {"###userAgent###", nvl(request.getHeader("User-Agent"))}
98         });
99         this.outWriter.println(logLine);
100     }
101
102     private static String JavaDoc nvl(String JavaDoc input) {
103         return input == null ? "-" : input;
104     }
105     
106     public void destroy() {
107         Logger.log(Logger.DEBUG, ACCESSLOG_RESOURCES, "SimpleAccessLogger.Close", this.fileName);
108         if (this.outWriter != null) {
109             this.outWriter.flush();
110             this.outWriter.close();
111             this.outWriter = null;
112         }
113         if (this.outStream != null) {
114             try {
115                 this.outStream.close();
116             } catch (IOException JavaDoc err) {}
117             this.outStream = null;
118         }
119         this.fileName = null;
120 // this.webAppConfig = null;
121
}
122 }
123
Popular Tags