如何在日志文本上将OrderId显示为Log4j模式布局

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在日志文本上将OrderId显示为Log4j模式布局相关的知识,希望对你有一定的参考价值。

我无法获取日志消息以显示正确的方式。我正在使用这个Log4J模式布局:

pattern="ORDERID : $${ctx:ORDERID} %msg%n"

我想看看输出如下:

ORDERID:123测试上下文

但这是我得到的输出:

ORDERID:$ {ctx:ORDERID}测试上下文

这是生成日志消息的代码:

@Test
public void testThreadContext() {
    ThreadContext.push("Message only");
    ThreadContext.push("int", 1);
    ThreadContext.push("int-long-string", 1, 2L, "3");
    ThreadContext.push("ORDERID", "123");
    logger.info("Test Context");
    ThreadContext.clearAll();
}
答案

我使用ThreadContext.put而不是ThreadContext.push。是工作。

ThreadContext.put(“ORDERID”,“123”);而不是ThreadContext.push(“ORDERID”,“123”);

结果日志文本URL

enter image description here

以上是关于如何在日志文本上将OrderId显示为Log4j模式布局的主要内容,如果未能解决你的问题,请参考以下文章