[logback-dev] [GIT] Logback: the generic, reliable, fast and flexible logging framework. branch, master, updated. v0.9.18-88-g8e4097f

Ralph Goers rgoers at apache.org
Thu Mar 18 01:26:46 CET 2010


I thought we discussed this. Putting this class into classic is pointless as it will almost never be used as is. It belongs in examples as it is a good starting point. For example, you could have included the ipAddress and current host name as shown at http://www.slf4j.org/extensions.html#event_logger.

Ralph

On Mar 17, 2010, at 4:24 PM, added by portage for gitosis-gentoo wrote:

> This is an automated email from the git hooks/post-receive script. It was
> generated because a ref change was pushed to the repository containing
> the project "Logback: the generic, reliable, fast and flexible logging framework.".
> 
> The branch, master has been updated
>       via  8e4097f3a23fc9ddc57846ad55e5c7100d2a4a63 (commit)
>      from  1acd46c45fe243953ebebe85c20541b9a4b08e94 (commit)
> 
> Those revisions listed above that are new to this repository have
> not appeared on any other notification email; so we list those
> revisions in full, below.
> 
> - Log -----------------------------------------------------------------
> http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=8e4097f3a23fc9ddc57846ad55e5c7100d2a4a63
> http://github.com/ceki/logback/commit/8e4097f3a23fc9ddc57846ad55e5c7100d2a4a63
> 
> commit 8e4097f3a23fc9ddc57846ad55e5c7100d2a4a63
> Author: Ceki Gulcu <ceki at qos.ch>
> Date:   Thu Mar 18 00:22:30 2010 +0100
> 
>    - MDCInsertingServletFilter:
> 
>    Within web-applications, it often proves helpful to know the hostname,
>    request uri and user-agent associated with a given http
>    request. MDCInsertingServletFilter inserts such data into the MDC.
> 
> diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java b/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java
> index 8f06e53..8a19e96 100644
> --- a/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java
> +++ b/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java
> @@ -17,8 +17,8 @@ public class ClassicConstants {
>   static public final String USER_MDC_KEY = "user";
> 
>   public static final String LOGBACK_CONTEXT_SELECTOR = "logback.ContextSelector";
> -  public static String JNDI_CONFIGURATION_RESOURCE = "java:comp/env/logback/configuration-resource";
> -  public static String JNDI_CONTEXT_NAME = "java:comp/env/logback/context-name";
> +  public static final String JNDI_CONFIGURATION_RESOURCE = "java:comp/env/logback/configuration-resource";
> +  public static final String JNDI_CONTEXT_NAME = "java:comp/env/logback/context-name";
> 
>   /**
>    * The maximum number of package separators (dots) that abbreviation
> @@ -32,4 +32,9 @@ public class ClassicConstants {
>    * The default stack data depth computed during caller data extraction.
>    */
>   public static final int DEFAULT_MAX_CALLEDER_DATA_DEPTH = 8;
> +  
> +  public final static String REQUEST_REMOTE_HOST_MDC_KEY = "request.remoteHost";
> +  public final static String REQUEST_USER_AGENT_MDC_KEY = "request.userAgent";
> +  public final static String REQUEST_REQUST_URI = "request.requestURI";
> +  
> }
> diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.java b/logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.java
> new file mode 100644
> index 0000000..250c5c4
> --- /dev/null
> +++ b/logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.java
> @@ -0,0 +1,80 @@
> +/**
> + * Logback: the reliable, generic, fast and flexible logging framework.
> + * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
> + *
> + * This program and the accompanying materials are dual-licensed under
> + * either the terms of the Eclipse Public License v1.0 as published by
> + * the Eclipse Foundation
> + *
> + *   or (per the licensee's choosing)
> + *
> + * under the terms of the GNU Lesser General Public License version 2.1
> + * as published by the Free Software Foundation.
> + */
> +package ch.qos.logback.classic.helpers;
> +
> +import java.io.IOException;
> +
> +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 org.slf4j.MDC;
> +
> +import ch.qos.logback.classic.ClassicConstants;
> +
> +/**
> + * A servlet filter that inserts various values retrieved from the incoming http
> + * request into the MDC.
> + * 
> + * <p>The values are removed after the request is processed.
> + *
> + * @author Ceki G&uuml;lc&uuml;
> + */
> +public class MDCInsertingServletFilter implements Filter {
> +
> +  public void destroy() {
> +    // do nothing
> +  }
> +
> +  public void doFilter(ServletRequest request, ServletResponse response,
> +      FilterChain chain) throws IOException, ServletException {
> +
> +    insertIntoMDC(request);
> +
> +    try {
> +      chain.doFilter(request, response);
> +    } finally {
> +      clearMDC();
> +    }
> +  }
> +
> +  void insertIntoMDC(ServletRequest request) {
> +
> +    MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request
> +        .getRemoteHost());
> +    
> +    if (request instanceof HttpServletRequest) {
> +      HttpServletRequest httpServletRequest = (HttpServletRequest) request;
> +      MDC.put(ClassicConstants.REQUEST_REQUST_URI, httpServletRequest
> +          .getRequestURI());
> +      MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest
> +          .getHeader("User-Agent"));
> +    }
> +
> +  }
> +
> +  void clearMDC() {
> +    MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
> +    MDC.remove(ClassicConstants.REQUEST_REQUST_URI);
> +    MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
> +  }
> +
> +  public void init(FilterConfig arg0) throws ServletException {
> +    // do nothing
> +  }
> +}
> diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java b/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
> index ddfe5af..9152f46 100644
> --- a/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
> +++ b/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
> @@ -16,6 +16,7 @@ package ch.qos.logback.classic.html;
> import java.util.Map;
> 
> import ch.qos.logback.classic.PatternLayout;
> +import ch.qos.logback.classic.pattern.MDCConverter;
> import ch.qos.logback.classic.spi.ILoggingEvent;
> import ch.qos.logback.classic.html.DefaultCssBuilder;
> import ch.qos.logback.core.html.HTMLLayoutBase;
> @@ -123,4 +124,19 @@ public class HTMLLayout extends HTMLLayoutBase<ILoggingEvent> {
>   public void setThrowableRenderer(IThrowableRenderer<ILoggingEvent> throwableRenderer) {
>     this.throwableRenderer = throwableRenderer;
>   }
> +  
> +  @Override
> +  protected String computeConverterName(Converter c) {
> +    if(c instanceof MDCConverter) {
> +      MDCConverter mc = (MDCConverter) c;
> +      String key = mc.getFirstOption();
> +      if(key != null) {
> +        return key;
> +      } else {
> +        return "MDC";
> +      }
> +    } else {    
> +      return super.computeConverterName(c);
> +    }
> +  }
> }
> diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java b/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java
> index e9e7af6..3b18bd1 100644
> --- a/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java
> +++ b/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java
> @@ -30,17 +30,15 @@ import ch.qos.logback.classic.selector.ContextJNDISelector;
> import ch.qos.logback.classic.selector.ContextSelector;
> 
> /**
> - * A servlet filter that puts the environment-dependend
> - * LoggerContext in a Threadlocal variable.
> - * 
> - * It removes it after the request is processed.
> - *
> - * To use it, add the following lines to a web.xml file
> + * A servlet filter that puts the environment dependent LoggerContext in a
> + * ThreadLocal variable, removing it after the request is processed.
>  * 
> + * <p>To use it, add the following lines to a web.xml file
> + *  
>  * <filter>
>  *   <filter-name>LoggerContextFilter</filter-name>
>  *   <filter-class>
> - *     ch.qos.userApp.LoggerContextFilter
> + *     ch.qos.logback.classic.selector.servlet.LoggerContextFilter
>  *   </filter-class>
>  * </filter>
>  * <filter-mapping>
> diff --git a/logback-core/src/main/java/ch/qos/logback/core/pattern/DynamicConverter.java b/logback-core/src/main/java/ch/qos/logback/core/pattern/DynamicConverter.java
> index 9e0f516..53eb3b8 100644
> --- a/logback-core/src/main/java/ch/qos/logback/core/pattern/DynamicConverter.java
> +++ b/logback-core/src/main/java/ch/qos/logback/core/pattern/DynamicConverter.java
> @@ -56,7 +56,7 @@ abstract public class DynamicConverter<E> extends FormattingConverter<E>
>    * 
>    * @return First option, may be null.
>    */
> -  protected String getFirstOption() {
> +  public String getFirstOption() {
>     if (optionList == null || optionList.size() == 0) {
>       return null;
>     } else {
> diff --git a/logback-site/src/site/pages/manual/mdc.html b/logback-site/src/site/pages/manual/mdc.html
> index 8b0bf37..143b4ff 100644
> --- a/logback-site/src/site/pages/manual/mdc.html
> +++ b/logback-site/src/site/pages/manual/mdc.html
> @@ -615,7 +615,8 @@ public class UserServletFilter implements Filter {
> 		
> 
> 
> -  <h3>MDC And Managed Threads</h3>
> +  <h3><a name="managedThreads" href="#managedThreads">MDC And Managed
> +  Threads</a></h3>
> 
>   <p>A copy of the mapped diagnostic context can not always be
>   inherited by worker threads from the initiating thread. This is the
> @@ -634,6 +635,74 @@ public class UserServletFilter implements Filter {
>   managed thread.
>   </p>
> 
> +  <h2><a name="mis"
> +  href="#mis"><code>MDCInsertingServletFilter</code></a></h2>
> +
> +  <p>Within web-applications, it often proves helpful to know the
> +  hostname, request uri and user-agent associated with a given http
> +  request. <a
> +  href="../xref/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.html"><code>MDCInsertingServletFilter</code></a>
> +  inserts such data into the MDC under the following keys.
> +  </p>
> +
> +  <table class="bodyTable">
> +    <tr>
> +      <th>MDC key</th>
> +      <th>MDC value</th>
> +    </tr>
> +
> +    <tr  class="alt">
> +      <td>request.remoteHost</td>
> +      <td>as returned by the <a
> +      href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getRemoteHost%28%29">getRemoteHost90</a>
> +      method
> +      </td>
> +    </tr>
> +
> +    <tr>
> +      <td>request.requestURI</td>
> +      <td>
> +        as returned by <a
> +        href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html#getRequestURI%28%29">getRequestURI()</a>
> +        method
> +      </td>
> +    </tr>
> +
> +    <tr  class="alt">
> +      <td>request.userAgent</td>
> +      <td>value of the "User-Agent" header
> +      </td>
> +    </tr>
> +
> +  </table>
> +
> +  <p>To install <code>MDCInsertingServletFilter</code> add the
> +  following lines to your web-application's <em>web.xml</em> file</p>
> +
> +  <pre class="prettyprint source">&lt;filter>
> +  &lt;filter-name>MDCInsertingServletFilter&lt;/filter-name>
> +  &lt;filter-class>
> +    ch.qos.logback.classic.helpers.MDCInsertingServletFilter
> +  &lt;/filter-class>
> +&lt;/filter>
> +&lt;filter-mapping>
> +  &lt;filter-name>MDCInsertingServletFilter&lt;/filter-name>
> +  &lt;url-pattern>/*&lt;/url-pattern>
> +&lt;/filter-mapping> </pre>
> +
> +  <p><b>If your web-app has multiple filter, make sure that
> +  <code>MDCInsertingServletFilter</code> is declared before other
> +  filters.</b> For example, assuming the main processing in your
> +  web-app is done in filter 'F', the MDC values set by
> +  <code>MDCInsertingServletFilter</code> will not be seen by the code
> +  invoked by 'F' if <code>MDCInsertingServletFilter</code> comes after
> +  'F'.
> +  </p>
> +
> +  <p>Once the filter is installed, values corresponding to each MDC
> +  key will be output by the %X <a
> +  href="layouts.html#conversionWord">conversion word</a>.
> +  </p>
> 
>   <script src="../templates/footer.js" type="text/javascript"></script>
> </div>
> 
> -----------------------------------------------------------------------
> 
> Summary of changes:
> .../ch/qos/logback/classic/ClassicConstants.java   |    9 ++-
> .../classic/helpers/MDCInsertingServletFilter.java |   80 ++++++++++++++++++++
> .../ch/qos/logback/classic/html/HTMLLayout.java    |   16 ++++
> .../selector/servlet/LoggerContextFilter.java      |   12 +--
> .../qos/logback/core/pattern/DynamicConverter.java |    2 +-
> logback-site/src/site/pages/manual/mdc.html        |   71 +++++++++++++++++-
> 6 files changed, 179 insertions(+), 11 deletions(-)
> create mode 100644 logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.java
> 
> 
> hooks/post-receive
> -- 
> Logback: the generic, reliable, fast and flexible logging framework.
> _______________________________________________
> logback-dev mailing list
> logback-dev at qos.ch
> http://qos.ch/mailman/listinfo/logback-dev



More information about the logback-dev mailing list