[logback-dev] svn commit: r609 - in logback/trunk/logback-access/src: main/java/ch/qos/logback/access/jetty main/java/ch/qos/logback/access/spi test/java/ch/qos/logback/access/jetty

noreply.seb at qos.ch noreply.seb at qos.ch
Thu Sep 28 12:28:08 CEST 2006


Author: seb
Date: Thu Sep 28 12:28:07 2006
New Revision: 609

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/spi/AccessEvent.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyTestSetup.java

Log:
- modified RequestLogImpl javadoc
- AccessEvent now remembers if the postContent has already been accessed
- updated tests

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	Thu Sep 28 12:28:07 2006
@@ -39,6 +39,17 @@
  * 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.xml file.
+ * <pre>
+ * &lt;Ref id="requestLog"&gt; 
+ *   &lt;Set name="requestLog"&gt; 
+ *     &lt;New id="requestLogImpl" class="ch.qos.logback.access.jetty.RequestLogImpl"&gt;&lt;/New&gt;
+ *       &lt;Set name="fileName"&gt;path/to/logback.xml&lt;/Set&gt;
+ *   &lt;/Set&gt; 
+ * &lt;/Ref&gt;
+ * </pre>
+ * <p>
  * Here is a sample logback.xml file that can be used right away:
  * <pre>
  * &lt;configuration&gt; 
@@ -51,6 +62,9 @@
  *   &lt;appender-ref ref="STDOUT" /&gt; 
  * &lt;/configuration&gt;
  * </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 

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	Thu Sep 28 12:28:07 2006
@@ -11,6 +11,15 @@
 
 import ch.qos.logback.access.pattern.AccessConverter;
 
+/**
+ * The Access module's internal representation of logging events. When the
+ * RequestLogImpl instance is called to log then a <code>AccessEvent</code>
+ * instance is created. This instance is passed around to the different logback
+ * components.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ * @author S&eacute;bastien Pennec
+ */
 public class AccessEvent implements Serializable {
 
   private static final long serialVersionUID = -3118194368414470960L;
@@ -29,6 +38,7 @@
   String protocol;
   String method;
   String serverName;
+  String postContent;
 
   Map<String, Object> requestHeaderMap;
 
@@ -280,17 +290,20 @@
   }
 
   public String getPostContent() {
-    String content = null;
+    if (postContent != null) {
+      return postContent;
+    }
+
     try {
-      content = Util.readToString(httpRequest.getInputStream());
+      postContent = Util.readToString(httpRequest.getInputStream());
     } catch (Exception ex) {
       // do nothing
     }
-    if (content != null && content.length() > 0) {
-      return content;
-    } else {
-      return NA;
+    if (postContent == null || postContent.length() == 0) {
+      postContent = NA;
     }
+
+    return postContent;
   }
 
   public int getLocalPort() {

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java	Thu Sep 28 12:28:07 2006
@@ -1,6 +1,5 @@
 package ch.qos.logback.access.jetty;
 
-
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.HttpURLConnection;
@@ -15,57 +14,81 @@
 import ch.qos.logback.core.appender.ListAppender;
 
 public class JettyBasicTest extends TestCase {
-  
+
   static RequestLogImpl requestLogImpl;
-  
+
   public static Test suite() {
     TestSuite suite = new TestSuite();
     suite.addTestSuite(JettyBasicTest.class);
     requestLogImpl = new RequestLogImpl();
     return new JettyTestSetup(suite, requestLogImpl);
   }
-  
-//  public void testGetRequest() throws Exception {
-//    URL url = new URL("http://localhost:8080/");
-//    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
-//    connection.setDoInput(true);
-//    
-//    String result = Util.readToString(connection.getInputStream());
-//    
-//    assertEquals("hello world", result); 
-//    
-//    ListAppender appender = (ListAppender)requestLogImpl.getAppender("list");
-//    appender.list.clear();
-//  }
-  
+
+  public void testGetRequest() throws Exception {
+    URL url = new URL("http://localhost:8080/");
+    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+    connection.setDoInput(true);
+
+    String result = Util.readToString(connection.getInputStream());
+
+    assertEquals("hello world", result);
+
+    ListAppender appender = (ListAppender) requestLogImpl.getAppender("list");
+    appender.list.clear();
+  }
+
+  public void testEventGoesToAppenders() throws Exception {
+    URL url = new URL("http://localhost:8080/");
+    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+    connection.setDoInput(true);
+
+    String result = Util.readToString(connection.getInputStream());
+
+    assertEquals("hello world", result);
+
+    ListAppender appender = (ListAppender) requestLogImpl.getAppender("list");
+    AccessEvent event = (AccessEvent) appender.list.get(0);
+    assertEquals("127.0.0.1", event.getRemoteHost());
+    assertEquals("localhost", event.getServerName());
+    appender.list.clear();
+  }
+
   public void testPostContentConverter() throws Exception {
     System.out.println("into test");
     URL url = new URL("http://localhost:8080/");
-    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
-    ((HttpURLConnection)connection).setRequestMethod("POST");
+    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+    ((HttpURLConnection) connection).setRequestMethod("POST");
+
     connection.setDoOutput(true);
     connection.setDoInput(true);
     connection.setUseCaches(false);
     connection.setRequestProperty("Content-Type", "text/plain");
-    
+    // connection.setRequestProperty("Content-Type",
+    // "application/x-www-form-urlencoded");
+
     String msg = "test message";
-    PrintWriter output = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
+    PrintWriter output = new PrintWriter(new OutputStreamWriter(connection
+        .getOutputStream()));
     output.print(msg);
     output.flush();
     output.close();
-    
+    // System.out.println("length: " + connection.getContentLength());
+
+    // Reading the response
     String result = Util.readToString(connection.getInputStream());
+    assertEquals("hello world", result);
+
+    // StatusPrinter.print(requestLogImpl.getStatusManager());
+
+    ListAppender listAppender = (ListAppender) requestLogImpl
+        .getAppender("list");
+    AccessEvent event = (AccessEvent) listAppender.list.get(0);
+    assertEquals(msg, event.getPostContent());
     
-    ListAppender appender = (ListAppender)requestLogImpl.getAppender("list");
-    //assertEquals(1, appender.list.size());
-    Iterator it = appender.list.iterator();
-    int i = 0;
-    while(it.hasNext()) {
-      AccessEvent event = (AccessEvent)it.next();
-      System.out.println(i++ + ": " + event.getPostContent());
+    Iterator it = listAppender.list.iterator();
+    while (it.hasNext()) {
+      AccessEvent event2 = (AccessEvent) it.next();
+      System.out.println("Event PostContent: " + event2.getPostContent());
     }
-    //System.out.println("0: " + event.getPostContent());
-    
   }
-
 }

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyTestSetup.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyTestSetup.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyTestSetup.java	Thu Sep 28 12:28:07 2006
@@ -27,7 +27,6 @@
 
 public class JettyTestSetup extends TestSetup {
 
-  ListAppender appender;
   RequestLogImpl requestLogImpl;
 
   public JettyTestSetup(Test suite, RequestLogImpl impl) {
@@ -68,32 +67,30 @@
     System.out.println("into tearDown");
     super.tearDown();
     server.stop();
-//    Thread.sleep(1000);
-//    server = null;
-//    appender = null;
-//    requestLogImpl = null;
+    Thread.sleep(1000);
+    server = null;
+    requestLogImpl = null;
   }
 
   private void buildContext() {
 
-    appender = new ListAppender();
+    ListAppender appender = new ListAppender();
     appender.setContext(requestLogImpl);
     appender.setName("list");
     appender.start();
 
-//    ConsoleAppender console = new ConsoleAppender();
-//    console.setContext(requestLogImpl);
-//    console.setName("console");
-//    PatternLayout layout = new PatternLayout();
-//    layout.setContext(requestLogImpl);
-//    layout
-//        .setPattern("%date %server %remoteIP %clientHost %user %requestURL %post");
-//    console.setLayout(layout);
-//    layout.start();
-//    console.start();
+    ConsoleAppender console = new ConsoleAppender();
+    console.setContext(requestLogImpl);
+    console.setName("console");
+    PatternLayout layout = new PatternLayout();
+    layout.setContext(requestLogImpl);
+    layout.setPattern("%date %server %clientHost %post");
+    console.setLayout(layout);
+    layout.start();
+    console.start();
 
-    //requestLogImpl.addAppender(console);
     requestLogImpl.addAppender(appender);
+    requestLogImpl.addAppender(console);
   }
 
 }
@@ -103,6 +100,9 @@
       HttpServletResponse response, int dispatch) throws IOException,
       ServletException {
 
+    // String requestContent = Util.readToString(request.getInputStream());
+    // System.out.println("request content: " + requestContent);
+
     OutputStream out = response.getOutputStream();
     ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
     writer.write("hello world");



More information about the logback-dev mailing list