slf4jlog4j2threadcontext

Setting & Getting Values from ThreadContext in log4j2


I am using log4j-api-2.0-beta4.jar along with sl4j in my web-app. I am trying to insert some values into ThreadContext.

ThreadContext.put("user",userName);           ThreadContext.put("thread",thread);
ThreadContext.put("url",url);

All I have done to get ThreadContext is to import the class.

At the end of the class, I am doing the following:

ThreadContext.clear();

The pattern layout that I am using in my log4j2.xml is:

<RollingFile name="Default" fileName="${logDir}/${filePrefix}.log"
                 filePattern="${logDir}/${filePrefix}/${filePrefix}.log.gz">
      <PatternLayout pattern="%d %-5p [%t] %37c %X{user} %x %m%n"/>
      <Policies>`enter code here`
        <SizeBasedTriggeringPolicy size="10MB"/>
      </Policies>
    </RollingFile>

I am not getting any value as part of %X{user} and %x gives me []. I need to get these values in my log file. Please help!!!


Solution

  • OK. I figured it out myself.

    It is that the flow is not reaching that part of the code at all ;).

    Sorry for the trouble guys, but Thanks anyways.