org.apache.log4j.MDC.put()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(5.4k)|赞(0)|评价(0)|浏览(661)

本文整理了Java中org.apache.log4j.MDC.put()方法的一些代码示例,展示了MDC.put()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MDC.put()方法的具体详情如下:
包路径:org.apache.log4j.MDC
类名称:MDC
方法名:put

MDC.put介绍

[英]Put a context value (the o parameter) as identified with the key parameter into the current thread's context map.

If the current thread does not have a context map it is created as a side effect.
[中]将用key参数标识的上下文值(o参数)放入当前线程的上下文映射中。
如果当前线程没有上下文映射,它将作为副作用创建。

代码示例

代码示例来源:origin: org.slf4j/slf4j-log4j12

/**
 * Put a context value (the <code>val</code> parameter) as identified with
 * the <code>key</code> parameter into the current thread's context map. The
 * <code>key</code> parameter cannot be null. Log4j does <em>not</em>
 * support null for the <code>val</code> parameter.
 * 
 * <p>
 * This method delegates all work to log4j's MDC.
 * 
 * @throws IllegalArgumentException
 *             in case the "key" or <b>"val"</b> parameter is null
 */
public void put(String key, String val) {
  org.apache.log4j.MDC.put(key, val);
}

代码示例来源:origin: Alluxio/alluxio

@Override
 public int decide(LoggingEvent event) {
  MDC.put(REMOTE_LOG_MDC_PROCESS_TYPE_KEY, mProcessType);
  return ACCEPT;
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * @param currentFilePath
 *          the currentFilePath to set
 */
public void setCurrentFilePath( String currentFilePath ) {
 this.currentFilePath = currentFilePath;
 if ( currentFilePath != null ) {
  MDC.put( FILE_KEY, currentFilePath );
 }
}

代码示例来源:origin: openzipkin/brave

@Override protected void put(String key, String value) {
 MDC.put(key, value);
}

代码示例来源:origin: org.slf4j/log4j-over-slf4j

public static void put(String key, Object value) {
  if (value != null) {
    put(key, value.toString());
  } else {
    put(key, null);
  }
}

代码示例来源:origin: org.slf4j/slf4j-log4j12

@SuppressWarnings({ "rawtypes", "unchecked" })
  public void setContextMap(Map contextMap) {
    Map old = org.apache.log4j.MDC.getContext();
    if (old == null) {
      Iterator entrySetIterator = contextMap.entrySet().iterator();
      while (entrySetIterator.hasNext()) {
        Map.Entry mapEntry = (Map.Entry) entrySetIterator.next();
        org.apache.log4j.MDC.put((String) mapEntry.getKey(), mapEntry.getValue());
      }
    } else {
      old.clear();
      old.putAll(contextMap);
    }
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

private void setCodeLine() {
 for ( int stackLevel = 1; stackLevel < Thread.currentThread().getStackTrace().length; stackLevel++ ) {
  StackTraceElement ste = Thread.currentThread().getStackTrace()[stackLevel];
  if ( !ste.getClassName().equals( this.getClass().getName() ) ) {
   MDC.put( CODE_LINE, ste.getClassName() + "." + ste.getMethodName() + ":" + ste.getLineNumber() );
   break;
  }
 }
}

代码示例来源:origin: hibernate/hibernate-orm

public Object putMdc(String key, Object val) {
  try {
    return MDC.get( key );
  }
  finally {
    MDC.put( key, val );
  }
}

代码示例来源:origin: wildfly/wildfly

public Object putMdc(String key, Object val) {
  try {
    return MDC.get(key);
  } finally {
    MDC.put(key, val);
  }
}

代码示例来源:origin: org.freemarker/freemarker

/**
 * Returns if Log4j-over-SLF4J is actually working. Sometimes the API classes are present, but there's no SLF4J
 * implementation around.
 */
public static final boolean test() {
  org.apache.log4j.MDC.put(MDC_KEY, "");
  try {
    return org.slf4j.MDC.get(MDC_KEY) != null;
  } finally {
    org.apache.log4j.MDC.remove(MDC_KEY);
  }
}

代码示例来源:origin: cloudfoundry/uaa

MDC.put("context", servletContext.getContextPath());

代码示例来源:origin: mercyblitz/segmentfault-lessons

public static void main(String[] args) throws Exception {

    System.setProperty(DEFAULT_CONFIGURATION_KEY, "log4j-conf.xml");

    Logger logger = Logger.getLogger(Log4jTest.class.getName());

    logger.setLevel(Level.INFO);

    logger.info("Hello,World");

    // 重新加载 log4j的配置

    URL url = Thread.currentThread().getContextClassLoader().getResource("log4j-api.xml");

    DOMConfigurator.configure(url);

    MDC.put("requestURI","https://segmemtfault.com");

    logger = Logger.getLogger(Log4jTest.class.getName());

    // 调整级别后输出
    logger.info("Hello,World");

    logger.error("大家好");

  }
}

代码示例来源:origin: org.apache.knox/gateway-util-common

@Override
public void attachContext(AuditContext context) {
 if ( context != null ) {
  MDC.put( MDC_AUDIT_CONTEXT_KEY, context );
 }
}

代码示例来源:origin: apache/activemq-artemis

public Object putMdc(String key, Object val) {
  try {
    return MDC.get(key);
  } finally {
    MDC.put(key, val);
  }
}

代码示例来源:origin: org.jboss.weld.se/weld-se

public Object putMdc(String key, Object val) {
  try {
    return MDC.get(key);
  } finally {
    MDC.put(key, val);
  }
}

代码示例来源:origin: xwjie/ElementVueSpringbootCodeTemplate

public static void setUser(User user) {
  tlUser.set(user);
  // 把用户信息放到log4j
  MDC.put(KEY_USER, user.getName());
}

代码示例来源:origin: apache/activemq-artemis

public Object putMdc(String key, Object val) {
  try {
    return MDC.get(key);
  } finally {
    MDC.put(key, val);
  }
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

public Object putMdc(String key, Object val) {
  try {
    return MDC.get(key);
  } finally {
    MDC.put(key, val);
  }
}

代码示例来源:origin: org.jboss.weld.servlet/weld-servlet-shaded

public Object putMdc(String key, Object val) {
  try {
    return MDC.get(key);
  } finally {
    MDC.put(key, val);
  }
}

代码示例来源:origin: tony19/logback-android

private LoggingEvent createLoggingEvent() {
  MDC.put(MDC_KEY, MDC_VALUE);
  LoggingEvent event = new LoggingEvent("com.example.XMLLayoutTest-<&>'\"]]>", root, Level.DEBUG, MESSAGE, new RuntimeException(
      "Dummy exception: <&>'\"]]>"), null);
  event.setThreadName("Dummy thread <&>'\"");
  StackTraceElement ste1 = new StackTraceElement("c1", "m1", "f1", 1);
  StackTraceElement ste2 = new StackTraceElement("c2", "m2", "f2", 2);
  event.setCallerData(new StackTraceElement[] { ste1, ste2 });
  return event;
}

相关文章