首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

log4j MDC NDC使用场景

2012-08-26 
log4j MDC NDC应用场景NDC(Nested Diagnostic Context)和MDC(Mapped Diagnostic Context)是log4j种非常有

log4j MDC NDC应用场景

NDC(Nested Diagnostic Context)和MDC(Mapped Diagnostic Context)是log4j种非常有用的两个类,它们用于存储应用程序的上下文信息(context infomation),从而便于在log中使用这些上下文信息。

?

NDC的实现是用hashtable来存储每个线程的stack信息,这个stack是每个线程可以设置当前线程的request的相关信息,然后当前线程在处理过程中只要在log4j配置打印出%x的信息,那么当前线程的整个stack信息就会在log4j打印日志的时候也会都打印出来,这样可以很好的跟踪当前request的用户行为功能。

MDC的实现是使用threadlocal来保存每个线程的Hashtable的类似map的信息,其他功能类似。

?

NDC的实现代码:

?

public class NDC {  static Hashtable ht = new Hashtable();    private static Stack getCurrentStack() {      if (ht != null) {          return (Stack) ht.get(Thread.currentThread());      }      return null;  }  public  static  String pop() {    Stack stack = getCurrentStack();    if(stack != null && !stack.isEmpty())       return ((DiagnosticContext) stack.pop()).message;    else      return "";  }  public  static  String peek() {    Stack stack = getCurrentStack();    if(stack != null && !stack.isEmpty())      return ((DiagnosticContext) stack.peek()).message;    else      return "";  }  public  static  void push(String message) {    Stack stack = getCurrentStack();          if(stack == null) {      DiagnosticContext dc = new DiagnosticContext(message, null);            stack = new Stack();      Thread key = Thread.currentThread();      ht.put(key, stack);      stack.push(dc);    } else if (stack.isEmpty()) {      DiagnosticContext dc = new DiagnosticContext(message, null);                  stack.push(dc);    } else {      DiagnosticContext parent = (DiagnosticContext) stack.peek();      stack.push(new DiagnosticContext(message, parent));    }      }

?

?

MDC的实现:

?

public class MDC {    final static MDC mdc = new MDC();    static final int HT_SIZE = 7;  boolean java1;    Object tlm;  private Method removeMethod;  private  MDC() {    java1 = Loader.isJava1();    if(!java1) {      tlm = new ThreadLocalMap();    }    try {      removeMethod = ThreadLocal.class.getMethod("remove", null);    } catch (NoSuchMethodException e) {      // don't do anything - java prior 1.5    }  }    */  static  public  void put(String key, Object o) {     if (mdc != null) {         mdc.put0(key, o);     }  }  static   public  Object get(String key) {    if (mdc != null) {        return mdc.get0(key);    }    return null;  }  static   public  void remove(String key) {    if (mdc != null) {        mdc.remove0(key);    }  }  public static Hashtable getContext() {    if (mdc != null) {        return mdc.getContext0();    } else {        return null;    }  }  public static void clear() {    if (mdc != null) {        mdc.clear0();    }  }  private  void put0(String key, Object o) {    if(java1 || tlm == null) {      return;    } else {      Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();      if(ht == null) {        ht = new Hashtable(HT_SIZE);        ((ThreadLocalMap)tlm).set(ht);      }          ht.put(key, o);    }  }    private  Object get0(String key) {    if(java1 || tlm == null) {      return null;    } else {             Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();      if(ht != null && key != null) {        return ht.get(key);      } else {        return null;      }    }  }  private  void remove0(String key) {    if(!java1 && tlm != null) {      Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();      if(ht != null) {        ht.remove(key);        // clean up if this was the last key        if (ht.isEmpty()) {          clear0();        }      }     }  }  private  Hashtable getContext0() {     if(java1 || tlm == null) {      return null;    } else {             return (Hashtable) ((ThreadLocalMap)tlm).get();    }  }  private  void clear0() {    if(!java1 && tlm != null) {      Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();      if(ht != null) {        ht.clear();      }      if(removeMethod != null) {          // java 1.3/1.4 does not have remove - will suffer from a memory leak          try {            removeMethod.invoke(tlm, null);          } catch (IllegalAccessException e) {            // should not happen          } catch (InvocationTargetException e) {            // should not happen          }      }    }  }}

?

?

?

在webx框架中对于log4j的MDC的处理:

先配置一个filter,这个filter是放置web.xml的最前面

?

?

    protected void populateMDC(Map<String, String> mdc) {        // GET or POST        putMDC(mdc, MDC_METHOD, request.getMethod());                StringBuffer requestURL = request.getRequestURL();        String queryString = trimToNull(request.getQueryString());        putMDC(mdc, MDC_REQUEST_URL, getRequestURL(requestURL, null));        putMDC(mdc, MDC_REQUEST_URL_WITH_QUERY_STRING, getRequestURL(requestURL, queryString));                String requestURI = request.getRequestURI();        String requestURIWithQueryString = queryString == null ? requestURI : requestURI + "?" + queryString;        putMDC(mdc, MDC_REQUEST_URI, requestURI);        putMDC(mdc, MDC_REQUEST_URI_WITH_QUERY_STRING, requestURIWithQueryString);        putMDC(mdc, MDC_QUERY_STRING, queryString);        // client info        putMDC(mdc, MDC_REMOTE_HOST, request.getRemoteHost());        putMDC(mdc, MDC_REMOTE_ADDR, request.getRemoteAddr());        // user agent        putMDC(mdc, MDC_USER_AGENT, request.getHeader("User-Agent"));        // referrer        putMDC(mdc, MDC_REFERRER, request.getHeader("Referer"));        // cookies        Cookie[] cookies = request.getCookies();        List<String> names = emptyList();        if (cookies != null) {            names = createArrayList(cookies.length);            for (Cookie cookie : cookies) {                names.add(cookie.getName());                putMDC(mdc, MDC_COOKIE_PREFIX + cookie.getName(), cookie.getValue());            }            sort(names);        }        putMDC(mdc, MDC_COOKIES, names.toString());    }

?

在finally中记住cleanMDC,否则可能会造成OOM。

?

?

        ?
??try {   helper.setLoggingContext();            chain.doFilter(request, response);        } finally {            helper.clearLoggingContext();        }

?

在 NDC 简介部分,我们曾经说过,%x 表示会在每个日志行上打印当前 NDC 上下文。

MDC? %X{remoteAddr}?{remoteAddr}?表示对应map中的remoteAddr的值

配置log4j:

?

 <param name="ConversionPattern" value="%d{yyyy-MM-dd HH\:mm\:ss} %X{remoteAddr} %X{requestURI} %X{referrer} %X{userAgent} %c %c - %m%n "/>

?

?

打印日志如下:2012-05-22 22:16:30 127.0.0.1 /cta/index.htm Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19 com.alibaba.citrus.webx.impl.WebxRootControllerImpl com.alibaba.citrus.webx.impl.WebxRootControllerImpl - Error occurred while process request /cta/index.htm

java.lang.NullPointerExceptionat com.alibaba.citrus.webx.impl.WebxControllerImpl.service(WebxControllerImpl.java:42)at com.alibaba.citrus.webx.impl.WebxRootControllerImpl.handleRequest(WebxRootControllerImpl.java:53)at com.alibaba.citrus.webx.support.AbstractWebxRootController.service(AbstractWebxRootController.java:156)at com.alibaba.citrus.webx.servlet.WebxFrameworkFilter.doFilter(WebxFrameworkFilter.java:141)at com.alibaba.citrus.webx.servlet.FilterBean.doFilter(FilterBean.java:164)at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1322)at com.alibaba.citrus.webx.servlet.SetLoggingContextFilter.doFilter(SetLoggingContextFilter.java:62)at com.alibaba.citrus.webx.servlet.FilterBean.doFilter(FilterBean.java:164)at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1322)
?

?

热点排行