As a continuation of my previous post about Log4j MDC (Mapped Diagnostic Context), here's a fully working code sample explaining the concept. This is a simple example where we have a one servlet and one filter. The filter intercepts each and every request and put the user name in the MDC.

The Servlet Class:

package com.veerasundar.code.log4jmdc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

 * A good-for-nothing servlet which is just writing few messages to the logger
 * object. Since we've configured in the file to include the
 * userName, taken from MDC, every message will be appended with the user name
 * that is set from the AuthenticationFilter
public class Log4jMdcDemo extends HttpServlet {

    private static Logger logger = Logger.getLogger(Log4jMdcDemo.class);

    public Log4jMdcDemo() {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doService(request, response);

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doService(request, response);

    protected void doService(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {"This is  demo for the Log4j MDC concept");"From");
        logger.debug("Just some sample messages");

A Filter to put the user name in MDC for every request call

package com.veerasundar.code.log4jmdc;


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 org.apache.log4j.MDC;

 * An example authentication filter which is used to intercept all the requests
 * for fetching the user name from it and put the user name to the Log4j Mapped
 * Diagnostic Context (MDC), so that the user name could be used for
 * differentiating log messages.
 * @author
public class AuthenticationFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        try {
            * This code puts the value "userName" to the Mapped Diagnostic
            * context. Since MDc is a static class, we can directly access it
            * with out creating a new object from it. Here, instead of hard
            * coding the user name, the value can be retrieved from a HTTP
            * Request object.
            MDC.put("userName", "veera");

            chain.doFilter(request, response);

        } finally {



Web.xml file to glue together Servlet and Filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"   xmlns="" xmlns:xsi=""   xsi:schemaLocation="">
</web-app> file which uses the data present in MDC

# sample file, explaining log4j MDC concept
# author:

log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout

#note the %X{userName} - this is how you fetch data from Mapped Diagnostic Context (MDC)
log4j.appender.consoleAppender.layout.ConversionPattern = %-4r [%t] %5p %c %x - %m - %X{userName}%n

log4j.rootLogger = DEBUG, consoleAppender

I hope that code explains the concept of MDC. Do let me know if you have any questions/suggestions in the comments.

Output from the log file:

0    [http-8084-2]  INFO Log4jMDCDemo  - This is  demo for the Log4j MDC concept - veera
0    [http-8084-2]  INFO Log4jMDCDemo  - From - veera
0    [http-8084-2] DEBUG Log4jMDCDemo  - Just some sample messages - veera