当前位置: 首页>>代码示例>>Java>>正文


Java MDC.getContext方法代码示例

本文整理汇总了Java中org.apache.log4j.MDC.getContext方法的典型用法代码示例。如果您正苦于以下问题:Java MDC.getContext方法的具体用法?Java MDC.getContext怎么用?Java MDC.getContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.log4j.MDC的用法示例。


在下文中一共展示了MDC.getContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: unsetLoggingContext

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
 * Remove the elements from the Message Driven Context (MDC) of Log4J, that may have been added by the call to setLoggingContext().
 */
public static void unsetLoggingContext() {
    Stack<Map<String, Object>> stack = t_loggingContext.get();
    if (stack == null || stack.size() == 0)
        throw new UnsupportedOperationException("The unsetLoggingContext() method can only be called after a setLoggingContext()");

    // Remove the current context
    if (MDC.getContext() != null) {
        Set<String> keys = new HashSet<String>(MDC.getContext().keySet());
        for (String key : keys)
            MDC.remove(key);
    }

    // Now add the elements of the previous logging context into the MDC
    Map<String, Object> previousLoggingContext = stack.pop();
    for (Map.Entry<String, Object> me : previousLoggingContext.entrySet())
        MDC.put(me.getKey(), me.getValue());
}
 
开发者ID:jaffa-projects,项目名称:jaffa-framework,代码行数:21,代码来源:LoggingService.java

示例2: unsetLoggingContext

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/** Remove the elements from the Message Driven Context (MDC) of Log4J, that may have been added by the call to setLoggingContext().
 * @param payload Any serializable object.
 * @param messageInfo the corresponding MessageInfo object, as specified in the configuration file.
 */
public static void unsetLoggingContext(Object payload, MessageInfo messageInfo) {
    Stack<Map<String, Object>> stack = t_loggingContext.get();
    if (stack == null || stack.size() == 0)
        throw new UnsupportedOperationException("The unsetLoggingContext() method can only be called after a setLoggingContext()");

    // Remove the current context
    if (MDC.getContext() != null) {
        Set<String> keys = new HashSet<String>(MDC.getContext().keySet());
        for (String key : keys)
            MDC.remove(key);
    }

    // Now add the elements of the previous logging context into the MDC
    Map<String, Object> previousLoggingContext = stack.pop();
    for (Map.Entry<String, Object> me : previousLoggingContext.entrySet())
        MDC.put(me.getKey(), me.getValue());
}
 
开发者ID:jaffa-projects,项目名称:jaffa-framework,代码行数:22,代码来源:LoggingService.java

示例3: testMDC

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
 * Tests the format of the MDC portion of the layout to ensure
 * the key-value pairs we put in turn up in the output file.
 * @throws Exception
 */
public void testMDC() throws Exception {
  XMLLayout xmlLayout = new XMLLayout();
  xmlLayout.setProperties(true);
  root.addAppender(new FileAppender(xmlLayout, TEMP, false));

  Hashtable context = MDC.getContext();
  if (context != null) {
      context.clear();
  }
  MDC.put("key1", "val1");
  MDC.put("key2", "val2");

  logger.debug("Hello");
  Transformer.transform(
    TEMP, FILTERED,
    new Filter[] { new LineNumberFilter(),
        new JunitTestRunnerFilter(),
        new XMLTimestampFilter()});
  assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.mdc.1"));
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:26,代码来源:XMLLayoutTestCase.java

示例4: testMDCEscaped

import org.apache.log4j.MDC; //导入方法依赖的package包/类
public void testMDCEscaped() throws Exception {
  XMLLayout xmlLayout = new XMLLayout();
  xmlLayout.setProperties(true);
  root.addAppender(new FileAppender(xmlLayout, TEMP, false));

  Hashtable context = MDC.getContext();
  if (context != null) {
      context.clear();
  }
  MDC.put("blahAttribute", "<blah value='blah'>");
  MDC.put("<blahKey value='blah'/>", "blahValue");

  logger.debug("Hello");
  Transformer.transform(
    TEMP, FILTERED,
    new Filter[] { new LineNumberFilter(),
        new JunitTestRunnerFilter(),
        new XMLTimestampFilter() });
  assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.mdc.2"));
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:21,代码来源:XMLLayoutTestCase.java

示例5: LoadBatchCommand

import org.apache.log4j.MDC; //导入方法依赖的package包/类
public LoadBatchCommand(
    Locus locus,
    SegmentCacheManager cacheMgr,
    Dialect dialect,
    RolapCube cube,
    List<CellRequest> cellRequests)
{
    this.locus = locus;
    this.cacheMgr = cacheMgr;
    this.dialect = dialect;
    this.cube = cube;
    this.cellRequests = cellRequests;

    if (MDC.getContext() != null) {
        this.mdc.putAll(MDC.getContext());
    }
}
 
开发者ID:OSBI,项目名称:mondrian,代码行数:18,代码来源:FastBatchingCellReader.java

示例6: resetLogContext

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
 * Remove all the object put in this thread context.
 */
@SuppressWarnings("unused")
public static void resetLogContext() {
    if (MDC.getContext() != null) {
        MDC.getContext().clear();
    }
}
 
开发者ID:ddRPB,项目名称:rpb,代码行数:10,代码来源:LogContext.java

示例7: beforeExecute

import org.apache.log4j.MDC; //导入方法依赖的package包/类
protected void beforeExecute(Thread t, Runnable r) {
	String GROUP_KEY = "GROUP_KEY";
	Object group = MDC.get(GROUP_KEY);
	// 将group名称绑定到当前log4j的线程上下文中
	Hashtable<?, ?> ht = MDC.getContext();
	if (ht != null)
		ht.clear();
	if (group != null)
		MDC.put(GROUP_KEY, group);
}
 
开发者ID:dreajay,项目名称:jcode,代码行数:11,代码来源:TcpConnector.java

示例8: setUp

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
   * Clear MDC and NDC before test.
   */
public void setUp() {
    NDC.clear();
    if (MDC.getContext() != null) {
      MDC.getContext().clear();
    }
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:10,代码来源:XMLLayoutTest.java

示例9: setUp

import org.apache.log4j.MDC; //导入方法依赖的package包/类
public void setUp() {
    context = new ThreadContext();

    Hashtable hashtable = MDC.getContext();
    if (hashtable != null) {
        hashtable.clear();
    }
}
 
开发者ID:performancecopilot,项目名称:parfait,代码行数:9,代码来源:ThreadContextTest.java

示例10: getMDCCopy

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
    Obtain a copy of this thread's MDC prior to serialization or
    asynchronous logging.  
 */
 public
 void getMDCCopy() {
   if(mdcCopyLookupRequired) {
     mdcCopyLookupRequired = false;
     // the clone call is required for asynchronous logging.
     // See also bug #5932.
     Hashtable t = (Hashtable) MDC.getContext();
     if(t != null) {
mdcCopy = (Hashtable) t.clone();
     }
   }
 }
 
开发者ID:DiamondLightSource,项目名称:daq-eclipse,代码行数:17,代码来源:LogEvent.java

示例11: call

import org.apache.log4j.MDC; //导入方法依赖的package包/类
public LoadBatchResponse call() {
    if (MDC.getContext() != null) {
        final Map<String, Object> old = MDC.getContext();
        old.clear();
        old.putAll(mdc);
    }
    return new BatchLoader(locus, cacheMgr, dialect, cube)
        .load(cellRequests);
}
 
开发者ID:OSBI,项目名称:mondrian,代码行数:10,代码来源:FastBatchingCellReader.java

示例12: copyMDC

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
 * Copy the current MDC so it can be used later
 */
public void copyMDC() {
    this.mdc.clear();
    final Map<String, Object> currentMdc =
        MDC.getContext();
    if (currentMdc != null) {
        this.mdc.putAll(currentMdc);
    }
}
 
开发者ID:OSBI,项目名称:mondrian,代码行数:12,代码来源:Execution.java

示例13: setContextMap

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
 * Set the copied mdc into the current MDC. This should be called
 * any time there will be logging in a thread handled by the
 * RolapResultShepherd where original MDC needs to be retrieved
 */
public void setContextMap() {
    final Map<String, Object> old = MDC.getContext();
    if (old != null) {
        old.clear();
        old.putAll(mdc);
    }
}
 
开发者ID:OSBI,项目名称:mondrian,代码行数:13,代码来源:Execution.java

示例14: mdcClear

import org.apache.log4j.MDC; //导入方法依赖的package包/类
static public void mdcClear() {
	Map<?, ?> context = MDC.getContext();
	if (context != null) {
		context.clear();
	}
}
 
开发者ID:convertigo,项目名称:convertigo-engine,代码行数:7,代码来源:Log4jHelper.java

示例15: testProblemCharacters

import org.apache.log4j.MDC; //导入方法依赖的package包/类
/**
 * Tests problematic characters in multiple fields.
 * @throws Exception if parser can not be constructed or source is not a valid XML document.
 */
public void testProblemCharacters() throws Exception {
  String problemName = "com.example.bar<>&\"'";
  Logger logger = Logger.getLogger(problemName);
  Level level = new ProblemLevel(problemName);
  Exception ex = new IllegalArgumentException(problemName);
  String threadName = Thread.currentThread().getName();
  Thread.currentThread().setName(problemName);
  NDC.push(problemName);
  Hashtable mdcMap = MDC.getContext();
  if (mdcMap != null) {
      mdcMap.clear();
  }
  MDC.put(problemName, problemName);
  LoggingEvent event =
    new LoggingEvent(
      problemName, logger, level, problemName, ex);
  XMLLayout layout = (XMLLayout) createLayout();
  layout.setProperties(true);
  String result = layout.format(event);
  mdcMap = MDC.getContext();
  if (mdcMap != null) {
      mdcMap.clear();
  }
  Thread.currentThread().setName(threadName);

  Element parsedResult = parse(result);
  checkEventElement(parsedResult, event);

  int childElementCount = 0;

  for (
    Node node = parsedResult.getFirstChild(); node != null;
      node = node.getNextSibling()) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
      childElementCount++;
      switch(childElementCount) {
          case 1:
          checkMessageElement((Element) node, problemName);
          break;

          case 2:
          checkNDCElement((Element) node, problemName);
          break;

          case 3:
          checkThrowableElement((Element) node, ex);
          break;

          case 4:
          checkPropertiesElement((Element) node, problemName, problemName);
          break;

          default:
          fail("Unexpected element");
          break;
      }

      break;

    case Node.COMMENT_NODE:
      break;

    case Node.TEXT_NODE:

      //  should only be whitespace
      break;

    default:
      fail("Unexpected node type");

      break;
    }
  }
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:80,代码来源:XMLLayoutTest.java


注:本文中的org.apache.log4j.MDC.getContext方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。