Using MDC for setting context variables in Log4J

Sometimes you might want to configure your logs so that it adds some context specific attributes on every line. Ex. you might want to print name of the logged in user in the log statement. So that if you want to see what went wrong for say user John you can find all the log statements for John by grep and analyze the problem Apache Log4j has MDC.java class that can be used for this type of use case. Basic idea is you set a Map of parameters on the current thread and that would be available to all the methods downstream. I wanted to try this feature out so i used the following steps.
  1. First call MDC.put("USER","Sunil") method to set USER context variable at current thread level
    
    package com.test.mq;
    
    import org.apache.log4j.Logger;
    import org.apache.log4j.MDC;
    public class HelloMDC {
        public static void main(String[] argv){
            Logger logger = Logger.getLogger(HelloMDC.class);
            MDC.put("USER","Sunil");
            logger.debug("Sample debug message");
        }
    }
    
  2. Then you can configure the message pattern layout to include the USER variable like %X{USER} at the start of the message
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        <appender name="console" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%X{USER} - %-5p %c{1} - %m%n"/>
            </layout>
        </appender>
        <root>
            <priority value ="debug" />
            <appender-ref ref="console" />
        </root>
    </log4j:configuration>
    
Now when the code executes you can see the user name printed at the start of the message like this Sunil - DEBUG HelloSender - Sample debug message