本文整理了Java中org.apache.logging.log4j.ThreadContext
类的一些代码示例,展示了ThreadContext
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadContext
类的具体详情如下:
包路径:org.apache.logging.log4j.ThreadContext
类名称:ThreadContext
[英]The ThreadContext allows applications to store information either in a Map or a Stack.
The MDC is managed on a per thread basis. To enable automatic inheritance of copies of the MDC to newly created threads, enable the DefaultThreadContextMap#INHERITABLE_MAP Log4j system property.
[中]ThreadContext允许应用程序将信息存储在映射或堆栈中。
MDC是按线程管理的。要将MDC副本自动继承到新创建的线程,请启用DefaultThreadContextMap#INHERITABLE_MAP Log4j系统属性。
代码示例来源: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
/**
* Clears the context map and stack.
*/
public static void clearAll() {
clearMap();
clearStack();
}
代码示例来源: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-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-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-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
ThreadContext.push("stackvalue");
ThreadContext.put("KEY", "mapvalue");
ThreadContext.put("count", String.valueOf(i));
} else {
ThreadContext.remove("count");
ThreadContext.pop();
CoreLoggerContexts.stopLoggerContext(false, files[0]); // stop async thread
代码示例来源: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 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: 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 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-core
@Before
public void setup() {
ThreadContext.put("subject", "I");
ThreadContext.put("verb", "love");
ThreadContext.put("object", "Log4j");
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testFilter() {
ThreadContext.put("userid", "testuser");
ThreadContext.put("organization", "Apache");
final KeyValuePair[] pairs = new KeyValuePair[] { new KeyValuePair("userid", "JohnDoe"),
new KeyValuePair("organization", "Apache")};
assertTrue(filter.isStarted());
assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
ThreadContext.remove("userid");
assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
ThreadContext.put("userid", "JohnDoe");
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.ERROR, null, (Object) null, (Throwable) null));
ThreadContext.put("organization", "ASF");
assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
ThreadContext.clearMap();
filter = ThreadContextMapFilter.createFilter(pairs, "or", null, null);
filter.start();
assertTrue(filter.isStarted());
ThreadContext.put("userid", "testuser");
ThreadContext.put("organization", "Apache");
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
ThreadContext.put("organization", "ASF");
assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
ThreadContext.remove("organization");
assertSame(Filter.Result.DENY, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
final KeyValuePair[] single = new KeyValuePair[] {new KeyValuePair("userid", "testuser")};
assertTrue(filter.isStarted());
assertSame(Filter.Result.NEUTRAL, filter.filter(null, Level.DEBUG, null, (Object) null, (Throwable) null));
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testAsyncLogWritesToLog() throws Exception {
final File file = new File("target", "AsyncLoggerTest.log");
// System.out.println(f.getAbsolutePath());
file.delete();
ThreadContext.push("stackvalue");
ThreadContext.put("KEY", "mapvalue");
final Logger log = LogManager.getLogger("com.foo.Bar");
final String msg = "Async logger msg";
log.info(msg, new InternalError("this is not a real error"));
CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread
final BufferedReader reader = new BufferedReader(new FileReader(file));
final String line1 = reader.readLine();
reader.close();
file.delete();
assertNotNull("line1", line1);
assertTrue("line1 correct", line1.contains(msg));
assertTrue("ThreadContext.map", line1.contains("mapvalue"));
assertTrue("ThreadContext.stack", line1.contains("stackvalue"));
}
代码示例来源: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 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
@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-core
context.put("user", "pass");
ThreadContext.push("message1");
ThreadContext.push("stack2");
final ThreadContext.ContextStack stack = ThreadContext.getImmutableStack();
ThreadContext.clearStack();
代码示例来源:origin: wildfly/wildfly
@Override
public void removeMdc(String key) {
ThreadContext.remove(key);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-api
private void closeStack() {
for (int i = 0; i < pushCount; i++) {
ThreadContext.pop();
}
pushCount = 0;
}
}
内容来源于网络,如有侵权,请联系作者删除!