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

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

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

ThreadContext.put介绍

[英]Puts a context value (the value 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参数标识的上下文值(value参数)放入当前线程的上下文映射中。
如果当前线程没有上下文映射,它将作为副作用创建。

代码示例

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

private void closeMap() {
  for (final Iterator<Map.Entry<String, String>> it = originalValues.entrySet().iterator(); it.hasNext(); ) {
    final Map.Entry<String, String> entry = it.next();
    final String key = entry.getKey();
    final String originalValue = entry.getValue();
    if (null == originalValue) {
      ThreadContext.remove(key);
    } else {
      ThreadContext.put(key, originalValue);
    }
    it.remove();
  }
}

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

@Override
  @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
  public void setContextMap(@SuppressWarnings("rawtypes") final Map map) {
    ThreadContext.clearMap();
    for (final Map.Entry<String, String> entry : ((Map<String, String>) map).entrySet()) {
      ThreadContext.put(entry.getKey(), entry.getValue());
    }
  }
}

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

@Before
public void setup() {
  ThreadContext.put("subject", "I");
  ThreadContext.put("verb", "love");
  ThreadContext.put("object", "Log4j");
}

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

public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() {
  ThreadContext.clearMap();
  ThreadContext.put("key", "val");
  final Map<String, String> immutable = ThreadContext.getImmutableContext();
  immutable.put("otherkey", "otherval");
}

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

@Test
public void mdc() {
  ThreadContext.put("TestYear", "2010");
  logger.debug("Debug message");
  ThreadContext.clearMap();
  logger.debug("Debug message");
  final List<LogEvent> events = app.getEvents();
  assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}

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

protected void sendInfoStructuredMessage() {
  ThreadContext.put("loginId", "JohnDoe");
  ThreadContext.put("ipAddress", "192.168.0.120");
  ThreadContext.put("locale", Locale.US.getDisplayName());
  final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
  msg.put("ToAccount", "123456");
  msg.put("FromAccount", "123457");
  msg.put("Amount", "200.00");
  // the msg.toString() doesn't contain the parameters of the ThreadContext, so we must use the line1 string
  final String str = msg.asString(null, null);
  sentMessages.add(str);
  root.info(MarkerManager.getMarker("EVENT"), msg);
}

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

@Test
public void mdc() {
  ThreadContext.put("TestYear", "2010");
  logger.debug("Debug message");
  ThreadContext.clearMap();
  logger.debug("Debug message");
  final List<LogEvent> events = app.getEvents();
  assertEventCount(events, 2);
}

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

private void prepareThreadContext(boolean isThreadContextMapInheritable) {
  System.setProperty("log4j2.isThreadContextMapInheritable", Boolean.toString(isThreadContextMapInheritable));
  PropertiesUtil.getProperties().reload();
  ThreadContextTest.reinitThreadContext();
  ThreadContext.remove("baz");
  ThreadContext.put("foo", "bar");
}

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

@Test
public void putAllWillPutAllValues() throws Exception {
  final String oldValue = "oldValue";
  ThreadContext.put(key, oldValue);
  final Map<String, String> valuesToPut = new HashMap<>();
  valuesToPut.put(key, value);
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.putAll(valuesToPut)) {
    assertThat(ThreadContext.get(key), is(value));
  }
  assertThat(ThreadContext.get(key), is(oldValue));
}

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

@Test
public void testNoop() {
  ThreadContext.put("Test", "Test");
  final String value = ThreadContext.get("Test");
  assertNull("value was saved", value);
}

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

@Test
public void testHeaderFooterThreadContext() throws Exception {
  final PatternLayout layout = PatternLayout.newBuilder().withPattern("%d{UNIX} %m")
      .withConfiguration(ctx.getConfiguration()).withHeader("${ctx:header}").withFooter("${ctx:footer}")
      .build();
  ThreadContext.put("header", "Hello world Header");
  ThreadContext.put("footer", "Hello world Footer");
  final byte[] header = layout.getHeader();
  assertNotNull("No header", header);
  assertTrue("expected \"Hello world Header\", actual " + Strings.dquote(new String(header)),
      new String(header).equals(new String("Hello world Header")));
}

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

@Test
public void shouldPreserveOldEntriesFromTheMapWhenAutoClosed() throws Exception {
  final String oldValue = "oldValue";
  ThreadContext.put(key, oldValue);
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value)) {
    assertThat(ThreadContext.get(key), is(value));
  }
  assertThat(ThreadContext.get(key), is(oldValue));
}

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

@Test
  public void testDefault() {
    final Map<String, String> map = new HashMap<>();
    map.put(TESTKEY, TESTVAL);
    final StrLookup lookup = new Interpolator(new MapLookup(map));
    final StrSubstitutor subst = new StrSubstitutor(lookup);
    ThreadContext.put(TESTKEY, TESTVAL);
    //String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}");
    final String value = subst.replace("${sys:TestKey1:-${ctx:TestKey}}");
    assertEquals("TestValue", value);
  }
}

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

@Test
public void ifTheSameKeyIsAddedTwiceTheOriginalShouldBeUsed() throws Exception {
  final String oldValue = "oldValue";
  final String secondValue = "innerValue";
  ThreadContext.put(key, oldValue);
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value).put(key, secondValue)) {
    assertThat(ThreadContext.get(key), is(secondValue));
  }
  assertThat(ThreadContext.get(key), is(oldValue));
}

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

public static void testPut() {
    ThreadContext.clearMap();
    assertNull(ThreadContext.get("testKey"));
    ThreadContext.put("testKey", "testValue");
    assertEquals("testValue", ThreadContext.get("testKey"));
  }
}

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

@Test
public void testLookup() {
  ThreadContext.put(TESTKEY, TESTVAL);
  final StrLookup lookup = new ContextMapLookup();
  String value = lookup.lookup(TESTKEY);
  assertEquals(TESTVAL, value);
  value = lookup.lookup("BadKey");
  assertNull(value);
}

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

@Test
public void testContainsKey() {
  ThreadContext.clearMap();
  assertFalse(ThreadContext.containsKey("testKey"));
  ThreadContext.put("testKey", "testValue");
  assertTrue(ThreadContext.containsKey("testKey"));
  ThreadContext.remove("testKey");
  assertFalse(ThreadContext.containsKey("testKey"));
}

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

@Test
public void testContainsKey() {
  ThreadContext.clearMap();
  assertFalse(ThreadContext.containsKey("testKey"));
  ThreadContext.put("testKey", "testValue");
  assertTrue(ThreadContext.containsKey("testKey"));
  ThreadContext.remove("testKey");
  assertFalse(ThreadContext.containsKey("testKey"));
}

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

@Test
public void testRemove() {
  ThreadContext.clearMap();
  assertNull(ThreadContext.get("testKey"));
  ThreadContext.put("testKey", "testValue");
  assertEquals("testValue", ThreadContext.get("testKey"));
  ThreadContext.remove("testKey");
  assertNull(ThreadContext.get("testKey"));
  assertTrue(ThreadContext.isEmpty());
}

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

@Test
public void testRemove() {
  ThreadContext.clearMap();
  assertNull(ThreadContext.get("testKey"));
  ThreadContext.put("testKey", "testValue");
  assertEquals("testValue", ThreadContext.get("testKey"));
  ThreadContext.remove("testKey");
  assertNull(ThreadContext.get("testKey"));
  assertTrue(ThreadContext.isEmpty());
}

相关文章