Log4J Appender to create per-request logs
[java-idp.git] / src / edu / internet2 / middleware / commons / log4j / ShowLog.java
1 /*
2  * ShowLog.java
3  * 
4  * Servlet that extracts the ThreadLocal log data from the HttpSession and 
5  * returns it to the user's browser.
6  * 
7  * Dependencies: The session attribute name must match the name used by the Filter.
8  * 
9  * --------------------
10  * Copyright 2002, 2004 
11  * Yale University
12  * University Corporation for Advanced Internet Development, Inc. 
13  * All rights reserved
14  * Your permission to use this code is governed by "The Shibboleth License".
15  * A copy may be found at http://shibboleth.internet2.edu/license.html
16  */
17 package edu.internet2.middleware.commons.log4j;
18
19 import java.io.IOException;
20 import java.io.Writer;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27
28 /**
29  * @author Howard Gilbert
30  */
31 public class ShowLog extends HttpServlet {
32     public static final String REQUESTLOG_ATTRIBUTE = "edu.internet2.middleware.commons.log4j.requestlog";
33
34     public void doGet(HttpServletRequest request, HttpServletResponse response)
35             throws ServletException, IOException {
36         HttpSession session = request.getSession();
37         if (session!=null) {
38             WrappedLog logBuffer = (WrappedLog) session.getAttribute(REQUESTLOG_ATTRIBUTE);
39             response.setContentType("text/plain");
40             Writer out = response.getWriter();
41             if (logBuffer==null)
42                 out.write("No Log Data");
43             else
44                 out.write(logBuffer.getLogData());
45         }
46         
47     }
48 }