當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。