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

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

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

ThreadContext.peek介绍

[英]Looks at the last diagnostic context at the top of this NDC without removing it.

The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.
[中]查看此NDC顶部的最后一个诊断上下文,但不删除它。
返回的值是上次推送的值。如果没有可用的上下文,则返回空字符串“”。

代码示例

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

@Override
public String peekNdc() {
  return ThreadContext.peek();
}

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

@Override
public String getNdc() {
  return ThreadContext.peek();
}

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

@Test
public void shouldPushAndPopAParameterizedEntryToTheStack() throws Exception {
  final String parameterizedMessage = "message {}";
  final String parameterizedMessageParameter = "param";
  final String formattedMessage = parameterizedMessage.replace("{}", parameterizedMessageParameter);
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.push(parameterizedMessage,
      parameterizedMessageParameter)) {
    assertThat(ThreadContext.peek(), is(formattedMessage));
  }
  assertThat(ThreadContext.peek(), is(""));
}

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

@Test
public void shouldPushAndPopAnEntryToTheStack() throws Exception {
  final String message = "message";
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.push(message)) {
    assertThat(ThreadContext.peek(), is(message));
  }
  assertThat(ThreadContext.peek(), is(""));
}

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

@Test
public void shouldPushAndPopTwoEntriesToTheStack() throws Exception {
  final String message1 = "message1";
  final String message2 = "message2";
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.push(message1).push(message2)) {
    assertThat(ThreadContext.peek(), is(message2));
  }
  assertThat(ThreadContext.peek(), is(""));
}

代码示例来源: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(""));
}

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

@Test
public void pushAllWillPushAllValues() throws Exception {
  ThreadContext.push(key);
  final List<String> messages = ThreadContext.getImmutableStack().asList();
  ThreadContext.pop();
  try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.pushAll(messages)) {
    assertThat(ThreadContext.peek(), is(key));
  }
  assertThat(ThreadContext.peek(), is(""));
}

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

@Test
public void canReuseCloseableThreadContext() throws Exception {
  final String stackValue = "something";
  // Create a ctc and close it
  final CloseableThreadContext.Instance ctc = CloseableThreadContext.push(stackValue).put(key, value);
  assertThat(ThreadContext.get(key), is(value));
  assertThat(ThreadContext.peek(), is(stackValue));
  ctc.close();
  assertThat(ThreadContext.containsKey(key), is(false));
  assertThat(ThreadContext.peek(), is(""));
  final String anotherKey = "key2";
  final String anotherValue = "value2";
  final String anotherStackValue = "something else";
  // Use it again
  ctc.push(anotherStackValue).put(anotherKey, anotherValue);
  assertThat(ThreadContext.get(anotherKey), is(anotherValue));
  assertThat(ThreadContext.peek(), is(anotherStackValue));
  ctc.close();
  assertThat(ThreadContext.containsKey(anotherKey), is(false));
  assertThat(ThreadContext.peek(), is(""));
}

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

@Test
public void closeIsIdempotent() throws Exception {
  final String originalMapValue = "map to keep";
  final String originalStackValue = "stack to keep";
  ThreadContext.put(key, originalMapValue);
  ThreadContext.push(originalStackValue);
  final String newMapValue = "temp map value";
  final String newStackValue = "temp stack to keep";
  final CloseableThreadContext.Instance ctc = CloseableThreadContext.push(newStackValue).put(key, newMapValue);
  ctc.close();
  assertThat(ThreadContext.get(key), is(originalMapValue));
  assertThat(ThreadContext.peek(), is(originalStackValue));
  ctc.close();
  assertThat(ThreadContext.get(key), is(originalMapValue));
  assertThat(ThreadContext.peek(), is(originalStackValue));
}

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

private static String getTaskUuid() {
  return ThreadContext.peek();
}

代码示例来源:origin: weld/core

@Override
public String peekNdc() {
  return ThreadContext.peek();
}

代码示例来源:origin: org.jboss.weld.se/weld-se-shaded

@Override
public String peekNdc() {
  return ThreadContext.peek();
}

代码示例来源:origin: apache/activemq-artemis

@Override
public String getNdc() {
  return ThreadContext.peek();
}

代码示例来源:origin: apache/activemq-artemis

@Override
public String getNdc() {
  return ThreadContext.peek();
}

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

/**
 * <strong style="color:#FF4040">Never use this method directly.</strong>
 *
 * @return The string value of the specified key.
 */
public static String get() {
  return org.apache.logging.log4j.ThreadContext.peek();
}

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

@Override
public String getNdc() {
  return ThreadContext.peek();
}

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

/**
 * Looks at the last diagnostic context at the top of this NDC
 * without removing it.
 * <p>
 * The returned value is the value that was pushed last. If no
 * context is available, then the empty string "" is returned.
 * </p>
 * @return String The innermost diagnostic context.
 */
public static String peek() {
  return org.apache.logging.log4j.ThreadContext.peek();
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

@Override
public String peekNdc() {
  return ThreadContext.peek();
}

代码示例来源:origin: org.jboss.weld.servlet/weld-servlet-shaded

@Override
public String getNdc() {
  return ThreadContext.peek();
}

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

/**
 * Looks at the last diagnostic context at the top of this NDC
 * without removing it.
 * <p/>
 * <p>The returned value is the value that was pushed last. If no
 * context is available, then the empty string "" is returned.
 *
 * @return String The innermost diagnostic context.
 */
public static String peek() {
  return org.apache.logging.log4j.ThreadContext.peek();
}

相关文章