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

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

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

ThreadContext.get介绍

[英]Gets the context value identified by the key parameter.

This method has no side effects.
[中]获取由key参数标识的上下文值。
这种方法没有副作用。

代码示例

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

@Override
public String get(final String key) {
  return ThreadContext.get(key);
}

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

@Override
public Object getMdc(String key) {
  return ThreadContext.get(key);
}

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

@Override protected String get(String key) {
 return ThreadContext.get(key);
}

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

@Override
public Object putMdc(String key, Object value) {
  try {
    return ThreadContext.get(key);
  } finally {
    ThreadContext.put(key, String.valueOf(value));
  }
}

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

/**
 * Populates the Thread Context Map with the supplied key/value pair. Any existing key in the
 * {@link ThreadContext} will be replaced with the supplied value, and restored back to their original value when
 * the instance is closed.
 *
 * @param key   The  key to be added
 * @param value The value to be added
 * @return a new instance that will back out the changes when closed.
 */
public Instance put(final String key, final String value) {
  // If there are no existing values, a null will be stored as an old value
  if (!originalValues.containsKey(key)) {
    originalValues.put(key, ThreadContext.get(key));
  }
  ThreadContext.put(key, value);
  return this;
}

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

@Override
  public void run() {
    final String greeting = ThreadContext.get("Greeting");
    if (greeting == null) {
      sb.append("null");
    } else {
      sb.append(greeting);
    }
    ThreadContext.clearMap();
  }
}

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

@Override
  public void run() {
    final String greeting = ThreadContext.get("Greeting");
    if (greeting == null) {
      sb.append("null");
    } else {
      sb.append(greeting);
    }
    ThreadContext.clearMap();
  }
}

代码示例来源: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 shouldNestEntries() throws Exception {
  final String oldValue = "oldValue";
  final String innerValue = "innerValue";
  ThreadContext.put(key, oldValue);
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value)) {
    assertThat(ThreadContext.get(key), is(value));
    try (final CloseableThreadContext.Instance ignored2 = CloseableThreadContext.put(key, innerValue)) {
      assertThat(ThreadContext.get(key), is(innerValue));
    }
    assertThat(ThreadContext.get(key), is(value));
  }
  assertThat(ThreadContext.get(key), is(oldValue));
}

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

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

代码示例来源: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-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-api

@Test
public void shouldAddAnEntryToTheMap() throws Exception {
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value)) {
    assertThat(ThreadContext.get(key), is(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

@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 shouldRemoveAnEntryFromTheMapWhenAutoClosed() throws Exception {
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value)) {
    assertThat(ThreadContext.get(key), is(value));
  }
  assertThat(ThreadContext.containsKey(key), is(false));
}

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

@Test
public void testRemoveAll() {
  ThreadContext.clearMap();
  ThreadContext.put("testKey1", "testValue1");
  ThreadContext.put("testKey2", "testValue2");
  assertEquals("testValue1", ThreadContext.get("testKey1"));
  assertEquals("testValue2", ThreadContext.get("testKey2"));
  assertFalse(ThreadContext.isEmpty());
  ThreadContext.removeAll(Arrays.asList("testKey1", "testKey2"));
  assertNull(ThreadContext.get("testKey1"));
  assertNull(ThreadContext.get("testKey2"));
  assertTrue(ThreadContext.isEmpty());
}

代码示例来源: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-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 shouldAddEntriesToBothStackAndMap() throws Exception {
  final String stackValue = "something";
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key, value).push(stackValue)) {
    assertThat(ThreadContext.get(key), is(value));
    assertThat(ThreadContext.peek(), is(stackValue));
  }
  assertThat(ThreadContext.containsKey(key), is(false));
  assertThat(ThreadContext.peek(), is(""));
}

相关文章