LOG4J MDC use

Introduction to MDC

MDC [ http://logback.qos.ch/manual/mdc.html ] is to establish an independent storage space for each thread, and developers can store information into it as needed. MDC uses the Map mechanism to store information, and the information is stored in the Map in the form of key/value pairs.

Use the Servlet Filter to generate a UUID to populate the MDC when a request is received. In the log4j configuration, you can use %X{key} to print the content in the MDC to identify the log in the same request.

The following is an example of a simple MDCFilter implementation that populates the MDC with UUID, host IP and hostname. UUID is used to mark the log in the same request. The host IP and host name are convenient for checking the log on the corresponding machine when an exception occurs.

MDCFilter implementation in log4j 1.x

//MDC填充的KEY

public static final String REQUEST_UUID = "REQUEST_UUID";

public static final String HOST_IP = "HOST_IP";

public static final String HOST_NAME = "HOST_NAME";

public static final String APPKEY = "APPKEY";

private String appkey = null;

 

@Override

public void init(FilterConfig filterConfig) throws ServletException {

    appkey = filterConfig.getInitParameter("appkey");//appkey可以在filter  init-param中配置

    if(appkey == null) {

        appkey = "";

    }

} 

@Override

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

    try {

        insertIntoMDC(request);//填充MDC

        chain.doFilter(request, response);

    } finally {

        clearMDC();//请求结束,注意清除MDC中的内容,否则会造成内存泄露问题

    }

}

// 默认实现中填充了UUID,主机IP和主机地址

protected void insertIntoMDC(ServletRequest request) {

    MDC.put(MDCFilter.REQUEST_UUID, UUID.randomUUID().toString());

    MDC.put(MDCFilter.HOST_IP, getHostIP());

    MDC.put(MDCFilter.HOST_NAME, getHostName());

    MDC.put(MDCFilter.APPKEY, appkey);

}

protected void clearMDC() {

    MDC.remove(MDCFilter.REQUEST_UUID);

    MDC.remove(MDCFilter.HOST_IP);

    MDC.remove(MDCFilter.HOST_NAME);

    MDC.remove(MDCFilter.APPKEY);

}

MDCFilter implementation in log4j2.0

In log4j 2.0, ThreadContext is used instead of MDC and

NDC[http://logging.apache.org/log4j/2.x/manual/thread-context.html]。

In log4j 2.x, an implementation similar to MDCFilter:

ThreadContextFilter

public static final String REQUEST_UUID = "REQUEST_UUID";

public static final String HOST_IP = "HOST_IP";

public static final String HOST_NAME = "HOST_NAME";

public static final String APPKEY = "APPKEY";

private String appkey = null;

 

 

@Override

public void init(FilterConfig filterConfig) throws ServletException {

    appkey = filterConfig.getInitParameter("appkey");//appkey可以在filter  init-param中配置

    if(appkey == null) {

        appkey = "";

    }

}

@Override

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

    try {

        insertIntoMDC(request);

        chain.doFilter(request, response);

    } finally {

        clearMDC();

    }

}

     

protected void insertIntoMDC(ServletRequest request) {

    ThreadContext.put(ThreadContextFilter.REQUEST_UUID, UUID.randomUUID().toString());

    ThreadContext.put(ThreadContextFilter.HOST_IP, getHostIP());

    ThreadContext.put(ThreadContextFilter.HOST_NAME, getHostName());        

    ThreadContext.put(ThreadContextFilter.APPKEY, appkey);

}

protected void clearMDC() {

    ThreadContext.remove(ThreadContextFilter.REQUEST_UUID);

    ThreadContext.remove(ThreadContextFilter.HOST_IP);

    ThreadContext.remove(ThreadContextFilter.HOST_NAME);

    ThreadContext.remove(ThreadContextFilter.APPKEY);

}

In log4j configuration, use MDC

In log4j configuration, use mdc: (This configuration can be used in both log4j 1.x and 2.x)

log4j configuration



%X{REQUEST_UUID} %X{HOST_IP} %X{HOST_NAME} %X{APPKEY} 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325139799&siteId=291194637