[logback-dev] svn commit: r2120 - in logback/trunk: logback-access/src/main/java/ch/qos/logback/access logback-access/src/main/java/ch/qos/logback/access/jetty logback-access/src/main/java/ch/qos/logback/access/servlet logback-access/src/main/java/ch/qos/logback/access/spi logback-access/src/main/java/ch/qos/logback/access/tomcat logback-access/src/test/java/ch/qos/logback/access/dummy logback-classic/src/main/java/ch/qos/logback/classic logback-core/src/main/java/ch/qos/logback/core/status
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Tue Dec 30 21:44:01 CET 2008
Author: ceki
Date: Tue Dec 30 21:44:01 2008
New Revision: 2120
Added:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/AccessConstants.java
- copied, changed from r1859, /logback/trunk/logback-access/src/main/java/ch/qos/logback/access/Constants.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/ViewStatusMessagesServlet.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessagesServlet.java
- copied, changed from r2119, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessageServlet.java
Removed:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/Constants.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessageServlet.java
Modified:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/Util.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/tomcat/LogbackValve.java
logback/trunk/logback-access/src/test/java/ch/qos/logback/access/dummy/DummyRequest.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/ViewStatusMessagesServletBase.java
Log:
Added ViewStatusMessageServlet in logback-access. In relation with LBCORE-82
Copied: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/AccessConstants.java (from r1859, /logback/trunk/logback-access/src/main/java/ch/qos/logback/access/Constants.java)
==============================================================================
--- /logback/trunk/logback-access/src/main/java/ch/qos/logback/access/Constants.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/AccessConstants.java Tue Dec 30 21:44:01 2008
@@ -1,6 +1,8 @@
package ch.qos.logback.access;
-public class Constants {
+public class AccessConstants {
+
+ public final static String LOGBACK_STATUS_MANAGER_KEY = "LOGBACK_STATUS_MANAGER";
public static final String LB_INPUT_BUFFER = "LB_INPUT_BUFFER";
public static final String LB_OUTPUT_BUFFER = "LB_OUTPUT_BUFFER";
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/ViewStatusMessagesServlet.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/ViewStatusMessagesServlet.java Tue Dec 30 21:44:01 2008
@@ -0,0 +1,31 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * Copyright (C) 2000-2008, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.access;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import ch.qos.logback.core.status.StatusManager;
+import ch.qos.logback.core.status.ViewStatusMessagesServletBase;
+
+public class ViewStatusMessagesServlet extends ViewStatusMessagesServletBase {
+
+ private static final long serialVersionUID = 443878494348593337L;
+
+ @Override
+ protected StatusManager getStatusManager(HttpServletRequest req, HttpServletResponse resp) {
+ return (StatusManager) req.getAttribute(AccessConstants.LOGBACK_STATUS_MANAGER_KEY);
+ }
+
+ @Override
+ protected String getPageTitle(HttpServletRequest req, HttpServletResponse resp) {
+ return "<h2>Status messages for logback-access</h2>\r\n";
+ }
+}
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/jetty/RequestLogImpl.java Tue Dec 30 21:44:01 2008
@@ -4,10 +4,13 @@
import java.util.HashMap;
import java.util.Iterator;
+import javax.servlet.ServletContext;
+
import org.mortbay.jetty.Request;
import org.mortbay.jetty.RequestLog;
import org.mortbay.jetty.Response;
+import ch.qos.logback.access.AccessConstants;
import ch.qos.logback.access.joran.JoranConfigurator;
import ch.qos.logback.access.spi.AccessEvent;
import ch.qos.logback.core.Appender;
@@ -26,14 +29,12 @@
import ch.qos.logback.core.util.OptionHelper;
/**
- * This class is logback's implementation of jetty's RequestLog interface.
- * <p>
+ * This class is logback's implementation of jetty's RequestLog interface. <p>
* It can be seen as logback classic's LoggerContext. Appenders can be attached
* directly to RequestLogImpl and RequestLogImpl uses the same StatusManager as
- * LoggerContext does. It also provides containers for properties.
- * <p>
- * To configure jetty in order to use RequestLogImpl, the following lines must
- * be added to the jetty configuration file, namely <em>etc/jetty.xml</em>:
+ * LoggerContext does. It also provides containers for properties. <p> To
+ * configure jetty in order to use RequestLogImpl, the following lines must be
+ * added to the jetty configuration file, namely <em>etc/jetty.xml</em>:
*
* <pre>
* <Ref id="requestLog">
@@ -48,11 +49,9 @@
* <em>etc/logback-access.xml</em>. The logback-access.xml file is slightly
* different than the usual logback classic configuration file. Most of it is
* the same: Appenders and Layouts are declared the exact same way. However,
- * loggers elements are not allowed.
- * <p>
- * It is possible to put the logback configuration file anywhere, as long as
- * it's path is specified. Here is another example, with a path to the
- * logback-access.xml file.
+ * loggers elements are not allowed. <p> It is possible to put the logback
+ * configuration file anywhere, as long as it's path is specified. Here is
+ * another example, with a path to the logback-access.xml file.
*
* <pre>
* <Ref id="requestLog">
@@ -63,8 +62,7 @@
* </Ref>
* </pre>
*
- * <p>
- * Here is a sample logback-access.xml file that can be used right away:
+ * <p> Here is a sample logback-access.xml file that can be used right away:
*
* <pre>
* <configuration>
@@ -78,8 +76,7 @@
* </configuration>
* </pre>
*
- * <p>
- * Another configuration file, using SMTPAppender, could be:
+ * <p> Another configuration file, using SMTPAppender, could be:
*
* <pre>
* <configuration>
@@ -95,17 +92,7 @@
* <appender-ref ref="SMTP" />
* </configuration>
* </pre>
- *
- * <p>
- * A special, module-specific implementation of PatternLayout was implemented to
- * allow http-specific patterns to be used. The
- * {@link ch.qos.logback.access.PatternLayout} provides a way to format the
- * logging output that is just as easy and flexible as the usual PatternLayout.
- * For more information about the general use of a PatternLayout, please refer
- * to logback classic's {@link ch.qos.logback.classic.PatternLayout}. For
- * information about logback access' specific PatternLayout, please refer to
- * it's javadoc.
- *
+
* @author Ceki Gülcü
* @author Sébastien Pennec
*/
@@ -119,7 +106,8 @@
FilterAttachableImpl<AccessEvent> fai = new FilterAttachableImpl<AccessEvent>();
String filename;
boolean started = false;
-
+ boolean alreadySetLogbackStatusManager = false;
+
public RequestLogImpl() {
putObject(CoreConstants.EVALUATOR_MAP, new HashMap());
}
@@ -130,6 +118,18 @@
AccessEvent accessEvent = new AccessEvent(jettyRequest, jettyResponse,
adapter);
+ if (!alreadySetLogbackStatusManager) {
+ alreadySetLogbackStatusManager = true;
+ ServletContext sc = jettyRequest.getContext();
+ // servlet context is always null
+ if (sc != null) {
+ sc.setAttribute(AccessConstants.LOGBACK_STATUS_MANAGER_KEY,
+ getStatusManager());
+ }
+ }
+
+ jettyRequest.setAttribute(
+ AccessConstants.LOGBACK_STATUS_MANAGER_KEY, getStatusManager());
if (getFilterChainDecision(accessEvent) == FilterReply.DENY) {
return;
}
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeFilter.java Tue Dec 30 21:44:01 2008
@@ -11,7 +11,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import ch.qos.logback.access.Constants;
+import ch.qos.logback.access.AccessConstants;
public class TeeFilter implements Filter {
@@ -36,7 +36,7 @@
teeResponse.finish();
// let the output contents be available for later use by
// logback-access-logging
- teeRequest.setAttribute(Constants.LB_OUTPUT_BUFFER, teeResponse
+ teeRequest.setAttribute(AccessConstants.LB_OUTPUT_BUFFER, teeResponse
.getOutputBuffer());
} catch (IOException e) {
e.printStackTrace();
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/TeeHttpServletRequest.java Tue Dec 30 21:44:01 2008
@@ -8,7 +8,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
-import ch.qos.logback.access.Constants;
+import ch.qos.logback.access.AccessConstants;
/**
* As the "tee" program on Unix, duplicate the request's input stream.
@@ -29,7 +29,7 @@
inStream = new TeeServletInputStream(request);
// add the contents of the input buffer as an attribute of the request
request
- .setAttribute(Constants.LB_INPUT_BUFFER, inStream.getInputBuffer());
+ .setAttribute(AccessConstants.LB_INPUT_BUFFER, inStream.getInputBuffer());
reader = new BufferedReader(new InputStreamReader(inStream));
}
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/Util.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/Util.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/servlet/Util.java Tue Dec 30 21:44:01 2008
@@ -3,13 +3,13 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import ch.qos.logback.access.Constants;
+import ch.qos.logback.access.AccessConstants;
public class Util {
public static boolean isFormUrlEncoded(HttpServletRequest request) {
if ("POST".equals(request.getMethod())
- && Constants.X_WWW_FORM_URLECODED.equals(request.getContentType())) {
+ && AccessConstants.X_WWW_FORM_URLECODED.equals(request.getContentType())) {
return true;
} else {
return false;
@@ -20,7 +20,7 @@
String responseType = response.getContentType();
- if (responseType != null && responseType.startsWith(Constants.IMAGE_CONTENT_TYPE)) {
+ if (responseType != null && responseType.startsWith(AccessConstants.IMAGE_CONTENT_TYPE)) {
return true;
} else {
return false;
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/spi/AccessEvent.java Tue Dec 30 21:44:01 2008
@@ -12,7 +12,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import ch.qos.logback.access.Constants;
+import ch.qos.logback.access.AccessConstants;
import ch.qos.logback.access.pattern.AccessConverter;
import ch.qos.logback.access.servlet.Util;
@@ -385,7 +385,7 @@
// retreive the byte array placed by TeeFilter
byte[] inputBuffer = (byte[]) httpRequest
- .getAttribute(Constants.LB_INPUT_BUFFER);
+ .getAttribute(AccessConstants.LB_INPUT_BUFFER);
if (inputBuffer != null) {
requestContent = new String(inputBuffer);
@@ -410,7 +410,7 @@
// retreive the byte array previously placed by TeeFilter
byte[] outputBuffer = (byte[]) httpRequest
- .getAttribute(Constants.LB_OUTPUT_BUFFER);
+ .getAttribute(AccessConstants.LB_OUTPUT_BUFFER);
if (outputBuffer != null) {
responseContent = new String(outputBuffer);
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/tomcat/LogbackValve.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/tomcat/LogbackValve.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/tomcat/LogbackValve.java Tue Dec 30 21:44:01 2008
@@ -6,6 +6,7 @@
import java.util.Iterator;
import java.util.Map;
+import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.catalina.Lifecycle;
@@ -14,6 +15,7 @@
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
+import ch.qos.logback.access.AccessConstants;
import ch.qos.logback.access.joran.JoranConfigurator;
import ch.qos.logback.access.spi.AccessEvent;
import ch.qos.logback.core.Appender;
@@ -64,6 +66,7 @@
String filename;
boolean quiet;
boolean started;
+ boolean alreadySetLogbackStatusManager = false;
public LogbackValve() {
putObject(CoreConstants.EVALUATOR_MAP, new HashMap());
@@ -120,17 +123,35 @@
public void invoke(Request request, Response response) throws IOException,
ServletException {
- getNext().invoke(request, response);
+ try {
- TomcatServerAdapter adapter = new TomcatServerAdapter(request, response);
- AccessEvent accessEvent = new AccessEvent(request, response, adapter);
+ if (!alreadySetLogbackStatusManager) {
+ alreadySetLogbackStatusManager = true;
+ org.apache.catalina.Context tomcatContext = request.getContext();
+ if (tomcatContext != null) {
+ ServletContext sc = tomcatContext.getServletContext();
+ if (sc != null) {
+ sc.setAttribute(AccessConstants.LOGBACK_STATUS_MANAGER_KEY,
+ getStatusManager());
+ }
+ }
+ }
- if (getFilterChainDecision(accessEvent) == FilterReply.DENY) {
- return;
- }
+ getNext().invoke(request, response);
+
+ TomcatServerAdapter adapter = new TomcatServerAdapter(request, response);
+ AccessEvent accessEvent = new AccessEvent(request, response, adapter);
- // TODO better exception handling
- aai.appendLoopOnAppenders(accessEvent);
+ if (getFilterChainDecision(accessEvent) == FilterReply.DENY) {
+ return;
+ }
+
+ // TODO better exception handling
+ aai.appendLoopOnAppenders(accessEvent);
+ } finally {
+ request
+ .removeAttribute(AccessConstants.LOGBACK_STATUS_MANAGER_KEY);
+ }
}
public void stop() {
Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/dummy/DummyRequest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/dummy/DummyRequest.java (original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/dummy/DummyRequest.java Tue Dec 30 21:44:01 2008
@@ -15,7 +15,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
-import ch.qos.logback.access.Constants;
+import ch.qos.logback.access.AccessConstants;
public class DummyRequest implements HttpServletRequest {
@@ -139,9 +139,9 @@
public Object getAttribute(String key) {
if (key.equals("testKey")) {
return "testKey";
- } else if (Constants.LB_INPUT_BUFFER.equals(key)) {
+ } else if (AccessConstants.LB_INPUT_BUFFER.equals(key)) {
return DUMMY_CONTENT_BYTES;
- } else if (Constants.LB_OUTPUT_BUFFER.equals(key)) {
+ } else if (AccessConstants.LB_OUTPUT_BUFFER.equals(key)) {
return DUMMY_RESPONSE_CONTENT_BYTES;
} else {
return null;
Copied: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessagesServlet.java (from r2119, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessageServlet.java)
==============================================================================
--- /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessageServlet.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ViewStatusMessagesServlet.java Tue Dec 30 21:44:01 2008
@@ -9,25 +9,28 @@
*/
package ch.qos.logback.classic;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
import org.slf4j.LoggerFactory;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.status.ViewStatusMessagesServletBase;
-public class ViewStatusMessageServlet extends ViewStatusMessagesServletBase {
+public class ViewStatusMessagesServlet extends ViewStatusMessagesServletBase {
private static final long serialVersionUID = 443878494348593337L;
//
@Override
- protected StatusManager getStatusManager() {
+ protected StatusManager getStatusManager(HttpServletRequest req, HttpServletResponse resp) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
return lc.getStatusManager();
}
@Override
- protected String getPageTitle() {
+ protected String getPageTitle(HttpServletRequest req, HttpServletResponse resp) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
return "<h2>Status messages for LoggerContext named ["
+ lc.getName() + "]</h2>\r\n";
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/ViewStatusMessagesServletBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/ViewStatusMessagesServletBase.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/ViewStatusMessagesServletBase.java Tue Dec 30 21:44:01 2008
@@ -31,17 +31,18 @@
private static SimpleDateFormat SDF = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
- protected abstract StatusManager getStatusManager();
+ protected abstract StatusManager getStatusManager(HttpServletRequest req, HttpServletResponse resp);
- protected abstract String getPageTitle();
+ protected abstract String getPageTitle(HttpServletRequest req, HttpServletResponse resp);
int count;
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
+ System.out.println("service called");
count = 0;
- StatusManager sm = getStatusManager();
+ StatusManager sm = getStatusManager(req, resp);
resp.setContentType("text/html");
PrintWriter output = resp.getWriter();
@@ -51,11 +52,15 @@
printCSS(req.getContextPath(), output);
output.append("</head>\r\n");
output.append("<body>\r\n");
- output.append(getPageTitle());
+ output.append(getPageTitle(req, resp));
output.append("<table>");
StringBuilder buf = new StringBuilder();
- printList(buf, sm);
+ if(sm != null) {
+ printList(buf, sm);
+ } else {
+ output.append("Could not find status manager");
+ }
output.append(buf);
output.append("</table>");
output.append("</body>\r\n");
@@ -161,12 +166,12 @@
private void printThrowable(StringBuilder buf, Throwable t) {
buf.append(" <tr>\r\n");
- buf.append(" <td colspan=\"4\" class=\"exception\">");
+ buf.append(" <td colspan=\"4\" class=\"exception\"><pre>");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
buf.append(Transform.escapeTags(sw.getBuffer()));
- buf.append(" </td>\r\n");
+ buf.append(" </pre></td>\r\n");
buf.append(" </tr>\r\n");
}
More information about the logback-dev
mailing list