1 /*
2 * $Id: ServletLogger.java 1419 2010-11-01 14:12:17Z hoheisel $
3 *
4 * Copyright 2008 Fraunhofer Gesellschaft, Munich, Germany,
5 * for its Fraunhofer Institute for Computer Architecture and Software Technology (FIRST), Berlin, Germany
6 * All rights reserved.
7 *
8 * See http://www.first.fraunhofer.de and http://www.gridworkflow.org/gwes for more details.
9 */
10
11 package net.kwfgrid.gwes.servlet;
12
13 import org.apache.log4j.Logger;
14
15 import javax.servlet.http.HttpServletRequest;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18
19 /**
20 * @author Andreas Hoheisel
21 * (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
22 * @version $Id: ServletLogger.java 1419 2010-11-01 14:12:17Z hoheisel $
23 */
24 public class ServletLogger {
25
26 final static Logger logger = Logger.getLogger(ServletLogger.class);
27
28 static final SimpleDateFormat dateAndTime=new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z");
29
30 public static void log(HttpServletRequest req) {
31 try {
32 // Log format: "NCSA Combined"
33 StringBuffer buf = new StringBuffer();
34 // h = IP address of the client
35 buf.append(check(req.getRemoteAddr()));
36 buf.append(" ");
37 // l = RFC 1413 identity of the client determined by identd on the clients machine.
38 buf.append("- ");
39 // u = userid of the person requesting the document as determined by HTTP authentication.
40 if (req.getUserPrincipal()==null) buf.append("- ");
41 else buf.append(check(req.getUserPrincipal().getName())).append(" ");
42 // t = The time that the request was received. [day/month/year:hour:minute:second zone]
43 Date date = new Date(System.currentTimeMillis());
44 buf.append("[");
45 buf.append(dateAndTime.format(date));
46 buf.append("] ");
47 // r = request line from the client
48 buf.append("\"");
49 buf.append(req.getMethod());
50 buf.append(" ");
51 buf.append(req.getRequestURI());
52 buf.append(" ");
53 buf.append(req.getProtocol());
54 buf.append("\" ");
55 // s = status code that the server sends back to the client.
56 buf.append("- ");
57 // b = size of the object returned to the client
58 buf.append("- ");
59
60 logger.info(buf.toString());
61 } catch (Exception e) {
62 logger.warn("not able to log: "+e,e);
63 }
64
65 }
66
67 private static String check(String str) {
68 return (str == null ? "-" : str);
69 }
70
71
72 }