Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

A needed feature of logging is to be able to identify which user caused the line of logging in the log file. Logback implements SLF4j and allows for tagging each log entry with information that the developer deems useful. Create a filter class to add the value to the MDC:

package com.bhsi;


import com.bhsi.dao.user.UserUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;

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 java.io.IOException;


/** @author Norris Shelton */
public class UserLoggingFilter implements Filter {

    private final String -- USER_KEY = "userId";

    /**
     * Called by the web container to indicate to a filter that it is being placed into service. The servlet container
     * calls the init method exactly once after instantiating the filter. The init method must complete successfully before
     * the filter is asked to do any filtering work. <br><br>
     * <p/>
     * The web container cannot place the filter into service if the init method either<br> 1.Throws a ServletException
     * <br> 2.Does not return within a time period defined by the web container
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    /**
     * The <code>doFilter</code> method of the Filter is called by the container each time a request/response pair is
     * passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in
     * to this method allows the Filter to pass on the request and response to the next entity in the chain.<p> A typical
     * implementation of this method would follow the following pattern:- <br> 1. Examine the request<br> 2. Optionally
     * wrap the request object with a custom implementation to filter content or headers for input filtering <br> 3.
     * Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
     * <br> 4. a) <strong>Either</strong> invoke the next entity in the chain using the FilterChain object
     * (<code>chain.doFilter()</code>), <br> * 4. b) <strong>or</strong> not pass on the request/response pair to the next
     * entity in the filter chain to block the request processing<br> * 5. Directly set headers on the response after
     * invocation of the next entity in the filter chain.
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
        boolean successfulRegistration = false;

        HttpServletRequest req = (HttpServletRequest) request;

        successfulRegistration = registerUsername(req.getRemoteUser());

        try {
            chain.doFilter(request, response);
        } finally {
            if (successfulRegistration) {
                MDC.remove(-- USER_KEY);
            }
        }

    }

    /**
     * Register the user in the MDC under -- USER_KEY.
     * @param userId the id of the user
     * @return true id the user was successfully registered
     */
    private boolean registerUsername(String userId) {
        boolean isRegisterUser = false;
        if (StringUtils.isNotBlank(userId)) {
            MDC.put(-- USER_KEY, UserUtil.getSimpleUserName(userId));
            isRegisterUser = true;
        }
        return isRegisterUser;
    }


    /**
     * Called by the web container to indicate to a filter that it is being taken out of service. This method is only
     * called once all threads within the filter's doFilter method have exited or after a timeout period has passed. After
     * the web container calls this method, it will not call the doFilter method again on this instance of the filter.
     * <br><br>
     * <p/>
     * This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file
     * handles, threads) and make sure that any persistent state is synchronized with the filter's current state in
     * memory.
     */
    public void destroy() {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

Add the filter class to the web.xml

    <filter>
        <filter-name>UserLoggingFilter</filter-name>
        <filter-class>com.bhsi.UserLoggingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UserLoggingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Modify the logback pattern to use the value added by the filter to the current logging thread.

<configuration>
    <!--
        WARN or higher goes to stdout (catalina.out)
    -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} intranet-templating:%X{userId}|%-5level-%msg %class.%method:%line %xException{full} %n</Pattern>
        </encoder>
    </appender>
    <!--
        INFO or higher goes to the log file that is in the tomcat logs directory.
        The files are rolled daily to the tomcat/logs/intranet-templating. 
    -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/opt/vignette/software/tomcat/logs/intranet-templating.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <if condition='property("HOSTNAME").contains("309")'>
                <then>
                    <level>DEBUG</level>
                </then>
                <else>
                    <level>INFO</level>
                </else>
            </if>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/opt/vignette/software/tomcat/logs/intranet-templating/intranet-templating.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss}|%X{userId}|%-5level-%msg %class.%method:%line %xException{full} %n</Pattern>
        </encoder>
    </appender>

    <logger name="org.hibernate" level="WARN"/>
    <!--<logger name="org.springframework" level="INFO"/>-->
    <logger name="org.apache.commons" level="INFO"/>
    <logger name="org.apache.velocity" level="INFO"/>
    <logger name="org.displaytag" level="INFO"/>
    <logger name="org.apache.axis" level="INFO"/>
    <root level="DEBUG">
        <appender-ref ref="FILE"/>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

December 6th, 2010

Posted In: logback

Leave a Reply

Your email address will not be published. Required fields are marked *

WP to LinkedIn Auto Publish Powered By : XYZScripts.com