Java – values cannot be placed in MDC

I tried to record several values in onbeginrequest () of requestcycle () in the ticket

Here are the codes:

getRequestCycleListeners().add(new AbstractRequestCycleListener()
{       
public void onBeginRequest(RequestCycle cycle) 
{                   
  if( cycle.getRequest().getContainerRequest() instanceof HttpServletRequest )
  {
    HttpServletRequest containerRequest = 
        (HttpServletRequest)cycle.getRequest().getContainerRequest();

    MDC.put("serverName",containerRequest.getServerName());
    MDC.put("sessionId",containerRequest.getSession().getId());

    LOGGER.debug("logging from RequestCycleListeners() !!!");
    WebClientInfo webClientInfo = new WebClientInfo(RequestCycle.get());
    System.out.println(webClientInfo.getUserAgent());
    System.out.println("webClientInfo.getProperties().getBrowserVersionMajor() " +containerRequest.getRemoteAddr());
}

};

I expect 'servername' and 'sessionid' to be recorded in the debug file

I have added this listener to the class that extends webapplication

I use log4j XML, debug appender is as follows:

<appender name="DEBUG" class="org.apache.log4j.rolling.RollingFileAppender">
  <param name="Append" value="true"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{ISO8601} %t %5p] %m -- %X{serverName} -- %X{sessionId} -- %X{portNumber}%n"/>
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="DEBUG"/>
    <param name="LevelMax" value="WARN"/>
  </filter>
</appender>

We are defining the scope in the root tag:

<root>
   <priority value="INFO" />
   <appender-ref ref="CONSOLE" />
   <appender-ref ref="DEBUG" />
   <appender-ref ref="ERROR" />
</root>

Solution

In general, if you configure to include MDC keys in recording mode, MDC values are only output to logs Since slf4j is only an elevation, you need to use framework specific support and configuration under slf4j to use MDC Read slf4j the notes on here

So, for example, if you use log4j as the impl under slf4j, you will need log4j config (conversionpattern), such as:

%d %-5p [%c] [%X{serverName} %X{sessionId}] %m%n

Where% X {servername}% X {sessionid} is the relevant part of the value extracted from MDC

Here is a very good example, using log4j without sl4j Please refer to the notes of X conversion characters in log4j Javadoc here

Note that the schema syntax of logback is the same See details of logback here

Also note that the best practice for MDC (an engine that uses ThreadLocal) is to clear the context when it is no longer in scope (delete the values you placed in the map) This usually means calling remove or clear in the finally block, such as:

try {
    //...
    MDC.put("key1",value1);
    MDC.put("key2",value2);
    //...
} finally {
    //this
    MDC.remove("key1");
    MDC.remove("key2");
    //or this
    MDC.clear();
}

This is particularly important if the thread that holds the MDC belongs to a pool for later reuse You certainly don't want to inadvertently record invalid context values, as this will lead to confusion

edit

Your log4j configuration seems a little strange for the following reasons:

>You are naming your appender log level, which may cause confusion > your rollingfileappender does not define a file > your root logger will record to 3 different Appenders, one of which is called debug, but it is configured to record only info level and higher level (based on priority tag), so debug statements will not be recorded

Unless you have a specific category that is not displayed configured separately, I guess your logger Debug statements are not logged, no matter you try to use MDC

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>