本文整理汇总了Java中org.apache.logging.log4j.ThreadContext.push方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadContext.push方法的具体用法?Java ThreadContext.push怎么用?Java ThreadContext.push使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.logging.log4j.ThreadContext
的用法示例。
在下文中一共展示了ThreadContext.push方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAsyncLogWritesToLog
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@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"));
}
示例2: createSubTaskProgress
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
public static void createSubTaskProgress(String fmt, Object...args) {
if (!ProgressGlobalConfig.PROGRESS_ON.value(Boolean.class)) {
return;
}
if (!ThreadContext.containsKey(Constants.THREAD_CONTEXT_API)) {
if (args != null) {
logger.warn(String.format("no task uuid found for:" + fmt, args));
} else {
logger.warn("no task uuid found for:" + fmt);
}
return;
}
ThreadContext.put(Constants.THREAD_CONTEXT_PROGRESS_ENABLED, "true");
String parentUuid = getParentUuid();
String taskUuid = Platform.getUuid();
ThreadContext.push(taskUuid);
ThreadContext.push(Platform.getUuid());
TaskProgressVO vo = new TaskProgressVO();
vo.setApiId(ThreadContext.get(Constants.THREAD_CONTEXT_API));
vo.setTaskUuid(taskUuid);
vo.setParentUuid(parentUuid);
vo.setContent(fmt);
if (args != null) {
vo.setArguments(JSONObjectUtil.toJsonString(args));
}
vo.setType(TaskType.Task);
vo.setTime(System.currentTimeMillis());
vo.setManagementUuid(Platform.getManagementServerId());
vo.setTaskName(ThreadContext.get(Constants.THREAD_CONTEXT_TASK_NAME));
Platform.getComponentLoader().getComponent(DatabaseFacade.class).persist(vo);
// use content as the subtask name
ThreadContext.put(Constants.THREAD_CONTEXT_TASK_NAME, vo.getContent());
}
示例3: testThreadContext
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void testThreadContext() {
ThreadContext.push("Message only");
ThreadContext.push("int", 1);
ThreadContext.push("int-long-string", 1, 2L, "3");
ThreadContext.put("key", "value");
logger.info("Hello World");
ThreadContext.clearAll();
}
示例4: test3
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void test3() {
ThreadContext.push("foo");
ThreadContext.push("bar");
ThreadContext.push("baz");
testConverter("foo bar baz");
}
示例5: test3
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void test3() {
ThreadContext.push("foo");
ThreadContext.push("bar");
ThreadContext.push("baz");
testConverter("[foo, bar, baz]");
}
示例6: test1
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void test1() {
ThreadContext.push("foo");
testConverter("foo");
}
示例7: test2
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void test2() {
ThreadContext.push("foo");
ThreadContext.push("bar");
testConverter("foo bar");
}
示例8: test1
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void test1() {
ThreadContext.push("foo");
testConverter("[foo]");
}
示例9: test2
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void test2() {
ThreadContext.push("foo");
ThreadContext.push("bar");
testConverter("[foo, bar]");
}
示例10: testTcpAppender
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
static void testTcpAppender(final TcpSocketTestServer tcpTestServer, final Logger logger, final int bufferSize)
throws Exception {
// @formatter:off
final SocketAppender appender = SocketAppender.newBuilder()
.withHost("localhost")
.withPort(tcpTestServer.getLocalPort())
.withReconnectDelayMillis(-1)
.withName("test")
.withImmediateFail(false)
.withBufferSize(bufferSize)
.withLayout(JsonLayout.newBuilder().setProperties(true).build())
.build();
// @formatter:on
appender.start();
Assert.assertEquals(bufferSize, appender.getManager().getByteBuffer().capacity());
// set appender on root and set level to debug
logger.addAppender(appender);
logger.setAdditive(false);
logger.setLevel(Level.DEBUG);
final String tcKey = "UUID";
final String expectedUuidStr = UUID.randomUUID().toString();
ThreadContext.put(tcKey, expectedUuidStr);
ThreadContext.push(expectedUuidStr);
final String expectedExMsg = "This is a test";
try {
logger.debug("This is a test message");
final Throwable child = new LoggingException(expectedExMsg);
logger.error("Throwing an exception", child);
logger.debug("This is another test message");
} finally {
ThreadContext.remove(tcKey);
ThreadContext.pop();
}
Thread.sleep(250);
LogEvent event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
assertNotNull("No event retrieved", event);
assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message"));
assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 0);
assertEquals(expectedUuidStr, event.getContextData().getValue(tcKey));
event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
assertNotNull("No event retrieved", event);
assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("Throwing an exception"));
assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 1);
assertEquals(expectedUuidStr, event.getContextStack().pop());
assertNotNull(event.getThrownProxy());
assertEquals(expectedExMsg, event.getThrownProxy().getMessage());
}
示例11: testAsyncLogWritesToLog
import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void testAsyncLogWritesToLog() throws Exception {
final File[] files = new File[] {
new File("target", "AsyncLoggerTest.log"), //
new File("target", "SynchronousContextTest.log"), //
new File("target", "AsyncLoggerAndAsyncAppenderTest.log"), //
new File("target", "AsyncAppenderContextTest.log"), //
};
for (final File f : files) {
f.delete();
}
ThreadContext.push("stackvalue");
ThreadContext.put("KEY", "mapvalue");
final Logger log = LogManager.getLogger("com.foo.Bar");
final LoggerContext loggerContext = LogManager.getContext(false);
final String loggerContextName = loggerContext.getClass().getSimpleName();
RingBufferAdmin ring;
if (loggerContext instanceof AsyncLoggerContext) {
ring = ((AsyncLoggerContext) loggerContext).createRingBufferAdmin();
} else {
ring = ((AsyncLoggerConfig) ((org.apache.logging.log4j.core.Logger) log).get()).createRingBufferAdmin("");
}
for (int i = 0; i < LINE_COUNT; i++) {
while (i >= 128 && ring.getRemainingCapacity() == 0) { // buffer may be full
Thread.sleep(1);
}
if ((i & 1) == 1) {
ThreadContext.put("count", String.valueOf(i));
} else {
ThreadContext.remove("count");
}
log.info("{} {} {} i={}", contextImpl, contextMap(), loggerContextName, Unbox.box(i));
}
ThreadContext.pop();
CoreLoggerContexts.stopLoggerContext(false, files[0]); // stop async thread
checkResult(files[0], loggerContextName);
if (asyncMode == Mode.MIXED || asyncMode == Mode.BOTH_ALL_ASYNC_AND_MIXED) {
for (int i = 1; i < files.length; i++) {
checkResult(files[i], loggerContextName);
}
}
LogManager.shutdown();
}