org.apache.logging.log4j.ThreadContext.getContext()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(187)

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

ThreadContext.getContext介绍

[英]Returns a mutable copy of current thread's context Map.
[中]返回当前线程上下文映射的可变副本。

代码示例

代码示例来源:origin: org.apache.logging.log4j/log4j-slf4j-impl

@Override
public Map<String, String> getCopyOfContextMap() {
  return ThreadContext.getContext();
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the
 * {@link ThreadContext} will be replaced with the supplied values, and restored back to their original value when
 * the instance is closed.
 *
 * @param values The map of key/value pairs to be added
 * @return a new instance that will back out the changes when closed.
 * @since 2.8
 */
public Instance putAll(final Map<String, String> values) {
  final Map<String, String> currentValues = ThreadContext.getContext();
  ThreadContext.putAll(values);
  for (final String key : values.keySet()) {
    if (!originalValues.containsKey(key)) {
      originalValues.put(key, currentValues.get(key));
    }
  }
  return this;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

public static void testGetContextReturnsEmptyMapIfEmpty() {
  ThreadContext.clearMap();
  assertTrue(ThreadContext.getContext().isEmpty());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

public static void testGetContextReturnsMutableCopy() {
  ThreadContext.clearMap();
  final Map<String, String> map1 = ThreadContext.getContext();
  assertTrue(map1.isEmpty());
  map1.put("K", "val"); // no error
  assertEquals("val", map1.get("K"));
  // adding to copy does not affect thread context map
  assertTrue(ThreadContext.getContext().isEmpty());
  ThreadContext.put("key", "val2");
  final Map<String, String> map2 = ThreadContext.getContext();
  assertEquals(1, map2.size());
  assertEquals("val2", map2.get("key"));
  map2.put("K", "val"); // no error
  assertEquals("val", map2.get("K"));
  // first copy is not affected
  assertNotSame(map1, map2);
  assertEquals(1, map1.size());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

private void testContextDataInjector() {
  ReadOnlyThreadContextMap readOnlythreadContextMap = getThreadContextMap();
  assertThat("thread context map class name",
        (readOnlythreadContextMap == null) ? null : readOnlythreadContextMap.getClass().getName(),
        is(equalTo(readOnlythreadContextMapClassName)));
  ContextDataInjector contextDataInjector = createInjector();
  StringMap stringMap = contextDataInjector.injectContextData(null, new SortedArrayStringMap());
  assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
  assertThat("context map", stringMap.toMap(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
  if (!stringMap.isFrozen()) {
    stringMap.clear();
    assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
    assertThat("context map", stringMap.toMap().entrySet(), is(empty()));
  }
  ThreadContext.put("foo", "bum");
  ThreadContext.put("baz", "bam");
  assertThat("thread context map", ThreadContext.getContext(), allOf(hasEntry("foo", "bum"), hasEntry("baz", "bam")));
  if (stringMap.isFrozen()) {
    assertThat("context map", stringMap.toMap(), allOf(hasEntry("foo", "bar"), not(hasKey("baz"))));
  } else {
    assertThat("context map", stringMap.toMap().entrySet(), is(empty()));
  }
}

代码示例来源:origin: zstackio/zstack

private Runnable saveThreadContext() {
  ThreadContextMapSaved savedThread = new ThreadContextMapSaved();
  savedThread.contextMap = ThreadContext.getContext();
  savedThread.contextStack = ThreadContext.cloneStack();
  return () -> {
    ThreadContext.clearAll();
    ThreadContext.putAll(savedThread.contextMap);
    ThreadContext.setStack(savedThread.contextStack.asList());
  };
}

代码示例来源:origin: org.apache.logging.log4j/log4j-slf4j18-impl

@Override
public Map<String, String> getCopyOfContextMap() {
  return ThreadContext.getContext();
}

代码示例来源:origin: com.sqlapp/sqlapp-core

/**
 * MDCに保存されている全オブジェクトを取得します
 * 
 * @return MDCに保存されている全オブジェクト
 */
@SuppressWarnings("rawtypes")
public static Map getMdcContext() {
  return org.apache.logging.log4j.ThreadContext.getContext();
}

代码示例来源:origin: com.github.emc-mongoose/mongoose-api-model

public LogContextThreadFactory(final String threadNamePrefix) {
  super(threadNamePrefix, ThreadContext.getContext());
}

代码示例来源:origin: com.github.emc-mongoose/mongoose-api-model

public LogContextThreadFactory(final String threadNamePrefix, final boolean daemonFlag) {
  super(threadNamePrefix, daemonFlag, ThreadContext.getContext());
}

代码示例来源:origin: emc-mongoose/mongoose

public LogContextThreadFactory(final String threadNamePrefix, final boolean daemonFlag) {
 super(threadNamePrefix, daemonFlag, ThreadContext.getContext());
}

代码示例来源:origin: emc-mongoose/mongoose

public LogContextThreadFactory(final String threadNamePrefix) {
 super(threadNamePrefix, ThreadContext.getContext());
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the
 * {@link ThreadContext} will be replaced with the supplied values, and restored back to their original value when
 * the instance is closed.
 *
 * @param values The map of key/value pairs to be added
 * @return a new instance that will back out the changes when closed.
 * @since 2.8
 */
public Instance putAll(final Map<String, String> values) {
  final Map<String, String> currentValues = ThreadContext.getContext();
  ThreadContext.putAll(values);
  for (final String key : values.keySet()) {
    if (!originalValues.containsKey(key)) {
      originalValues.put(key, currentValues.get(key));
    }
  }
  return this;
}

相关文章