[logback-dev] svn commit: r2298 - in logback-demo/trunk/src/main: java/ch/qos/logback/demo java/ch/qos/logback/demo/login java/ch/qos/logback/demo/prime resources resources/parts webapp/WEB-INF

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sun Jun 21 16:56:46 CEST 2009


Author: ceki
Date: Sun Jun 21 16:56:45 2009
New Revision: 2298

Added:
   logback-demo/trunk/src/main/java/ch/qos/logback/demo/UserServletFilter.java
Modified:
   logback-demo/trunk/src/main/java/ch/qos/logback/demo/login/LoginAction.java
   logback-demo/trunk/src/main/java/ch/qos/logback/demo/prime/PrimeAction.java
   logback-demo/trunk/src/main/resources/logback.xml
   logback-demo/trunk/src/main/resources/parts/sifting.xml
   logback-demo/trunk/src/main/webapp/WEB-INF/web.xml

Log:
- update the demo

Added: logback-demo/trunk/src/main/java/ch/qos/logback/demo/UserServletFilter.java
==============================================================================
--- (empty file)
+++ logback-demo/trunk/src/main/java/ch/qos/logback/demo/UserServletFilter.java	Sun Jun 21 16:56:45 2009
@@ -0,0 +1,63 @@
+package ch.qos.logback.demo;
+
+import java.io.IOException;
+import java.security.Principal;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.slf4j.MDC;
+
+public class UserServletFilter implements Filter {
+
+  boolean userRegistered = false;
+
+  public void destroy() {
+  }
+
+  public void doFilter(ServletRequest request, ServletResponse response,
+
+  FilterChain chain) throws IOException, ServletException {
+
+    HttpServletRequest req = (HttpServletRequest) request;
+    Principal principal = req.getUserPrincipal();
+    // Please note that we could have also used a cookie to
+    // retreive the user name
+
+    if (principal != null) {
+      String username = principal.getName();
+      registerUsername(username);
+    } else {
+      HttpSession session = req.getSession();
+      String username = (String) session
+          .getAttribute(Constants.USERID_SESSION_KEY);
+      registerUsername(username);
+    }
+
+    try {
+      chain.doFilter(request, response);
+    } finally {
+      if (userRegistered) {
+        MDC.remove(Constants.USERID_MDC_KEY);
+      }
+    }
+  }
+
+  public void init(FilterConfig arg0) throws ServletException {
+  }
+
+  private void registerUsername(String username) {
+    System.out.println("in registerUsername username="+username);
+    if (username != null && username.trim().length() > 0) {
+      MDC.put(Constants.USERID_MDC_KEY, username);
+      userRegistered = true;
+    }
+  }
+
+}

Modified: logback-demo/trunk/src/main/java/ch/qos/logback/demo/login/LoginAction.java
==============================================================================
--- logback-demo/trunk/src/main/java/ch/qos/logback/demo/login/LoginAction.java	(original)
+++ logback-demo/trunk/src/main/java/ch/qos/logback/demo/login/LoginAction.java	Sun Jun 21 16:56:45 2009
@@ -29,7 +29,6 @@
         MDC.put(Constants.USERID_MDC_KEY, username);
         logger.info("Login: " + username + " just logged in.");
         request.getSession().setAttribute(Constants.USERID_SESSION_KEY, username);
-        request.setAttribute("a", username);
       }
     }
 

Modified: logback-demo/trunk/src/main/java/ch/qos/logback/demo/prime/PrimeAction.java
==============================================================================
--- logback-demo/trunk/src/main/java/ch/qos/logback/demo/prime/PrimeAction.java	(original)
+++ logback-demo/trunk/src/main/java/ch/qos/logback/demo/prime/PrimeAction.java	Sun Jun 21 16:56:45 2009
@@ -7,6 +7,7 @@
 import org.apache.struts.action.ActionMapping;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -26,6 +27,9 @@
       if(number == 99) {
     	  logger.info("99 is not a valid value", new Exception("99 is invalid"));
       }
+      
+      System.out.println("*** userid="+MDC.get(Constants.USERID_MDC_KEY));
+      
       NumberCruncher nc = new NumberCruncherImpl();
       Long start = System.currentTimeMillis();
       Long[] result = nc.factor(number);

Modified: logback-demo/trunk/src/main/resources/logback.xml
==============================================================================
--- logback-demo/trunk/src/main/resources/logback.xml	(original)
+++ logback-demo/trunk/src/main/resources/logback.xml	Sun Jun 21 16:56:45 2009
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-
+  
 <configuration>
 
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
@@ -35,7 +35,7 @@
     </appender>
   -->
 
-  <!-- Cyclic buffer with Evaluator --> 
+  <!-- Cyclic buffer with Evaluator -->
   <appender name="CYCLIC"
     class="ch.qos.logback.core.read.CyclicBufferAppender">
     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
@@ -53,13 +53,13 @@
   
   
 
-  <!-- TurboFilter: by MDC value 
+  <!-- TurboFilter: by MDC value  -->
   <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
     <MDCKey>userid</MDCKey>
     <Value>sebastien</Value>
     <OnMatch>ACCEPT</OnMatch>
   </turboFilter>
-  -->
+  
 
   <!-- TurboFilter: by Marker   
   <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
@@ -72,31 +72,12 @@
   <!-- JMX Configurator  -->
   <jmxConfigurator />
 
-  <appender name="SIFT"
-            class="ch.qos.logback.classic.sift.SiftingAppender">
-    <discriminator>
-      <Key>userid</Key>
-      <DefaultValue>unknown</DefaultValue>
-    </discriminator>
-    <sift>
-      <appender name="FILE-${userid}"
-                class="ch.qos.logback.core.FileAppender">
-        <File>${userid}.log</File>
-        <Append>false</Append>
-        <layout>
-          <Pattern>
-            %d [%thread] %-55(%-5level %10mdc %logger{35}) - %msg%n
-          </Pattern>
-        </layout>
-      </appender>
-    </sift>
-  </appender>
 
 
   <root level="${lb_level:-ERROR}">
     <appender-ref ref="STDOUT" />
-    <appender-ref ref="SIFT" />
-    <appender-ref ref="CYCLIC" />
+    <appender-ref ref="FILE" />
+    <appender-ref ref="CYCLIC" /> 
   </root>
 
 </configuration>

Modified: logback-demo/trunk/src/main/resources/parts/sifting.xml
==============================================================================
--- logback-demo/trunk/src/main/resources/parts/sifting.xml	(original)
+++ logback-demo/trunk/src/main/resources/parts/sifting.xml	Sun Jun 21 16:56:45 2009
@@ -17,4 +17,3 @@
       </appender>
     </sift>
   </appender>
-

Modified: logback-demo/trunk/src/main/webapp/WEB-INF/web.xml
==============================================================================
--- logback-demo/trunk/src/main/webapp/WEB-INF/web.xml	(original)
+++ logback-demo/trunk/src/main/webapp/WEB-INF/web.xml	Sun Jun 21 16:56:45 2009
@@ -5,6 +5,19 @@
 
   <display-name>logback-demo</display-name>
   
+   <filter>
+      <filter-name>mdc_filter</filter-name>
+      <filter-class>
+       ch.qos.logback.demo.UserServletFilter
+      </filter-class>
+   </filter>
+   <filter-mapping>
+   <filter-name>mdc_filter</filter-name>
+     <url-pattern>/*</url-pattern>
+    </filter-mapping>
+
+  
+  
   <!-- Used when testing multiple contexts -->
   <env-entry>
    <description>JNDI logging context for this app</description>


More information about the logback-dev mailing list