如何将UUID添加到log4j中以登录文件?[英] How to add uuid to log4j for logging into file?

本文是小编为大家收集整理的关于如何将UUID添加到log4j中以登录文件?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有这个log4j2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Appenders>
    <File name="FILE" fileName="logfile.log" append="true">
        <PatternLayout pattern="%p | [%t] %l | message : %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%p | [%t] %l | message : %m%n"/>
    </Console>
</Appenders>
</Configuration>

我的目标是在uuid中添加RetendEndPoint唯一ID,但我不知道如何添加到XML文件...或我必须在XML文件中配置它?

推荐答案

在您的应用程序中,将UUID放在threadContext:

ThreadContext.put("myUuid", new UUID());

我假设您知道应用程序中的输入点在哪里放置和删除这些位置.

在配置中,使用%X模式转换器从线程context提取UUID :(还将记录器添加到您的配置中)

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
    <File name="FILE" fileName="logfile.log" append="true">
        <PatternLayout pattern="%p | [%t] %l | id: %X{myUuid} | message : %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%p | [%t] %l | id: %X{myUuid} | message : %m%n"/>
    </Console>
</Appenders>

<Loggers>
  <Root level ="trace">
    <AppenderRef ref="STDOUT" />
    <AppenderRef ref="FILE" />
  </Root>
</Loggers>
</Configuration>

其他推荐答案

感觉就像还有另一个解决方案,如果您需要的只是每个日志记录的唯一ID,则更容易.在 log4j doc
中检查UUID. 唯一的更新是更改XML文件中的布局是添加%u{"RANDOM"}.

示例XML配置

<PatternLayout>
    <pattern>%d{DATE} [%p] UUID:%u{"RANDOM"} (%t) %c: %m%n</pattern>
</PatternLayout>

本文地址:https://www.itbaoku.cn/post/1574957.html

问题描述

I have this log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Appenders>
    <File name="FILE" fileName="logfile.log" append="true">
        <PatternLayout pattern="%p | [%t] %l | message : %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%p | [%t] %l | message : %m%n"/>
    </Console>
</Appenders>
</Configuration>

And my goal is to add in the RestEndpoint a unique id with uuid, but i dont know how to add to the xml file...or i have to configure it not in an xml file?

推荐答案

In your application, put the uuid in the ThreadContext:

ThreadContext.put("myUuid", new UUID());

I assume you know where the entry points in your application are where to put and remove these.

In configuration, extract the UUID from the ThreadContext with the %X pattern converter: (also added Loggers to your configuration)

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
    <File name="FILE" fileName="logfile.log" append="true">
        <PatternLayout pattern="%p | [%t] %l | id: %X{myUuid} | message : %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%p | [%t] %l | id: %X{myUuid} | message : %m%n"/>
    </Console>
</Appenders>

<Loggers>
  <Root level ="trace">
    <AppenderRef ref="STDOUT" />
    <AppenderRef ref="FILE" />
  </Root>
</Loggers>
</Configuration>

其他推荐答案

Feel like there is another solution, which is easier, if what you need is just a unique id for each log record. Check UUID in Log4j doc
The only update is to change the layout in xml file is to add %u{"RANDOM"}.

sample xml configuration

<PatternLayout>
    <pattern>%d{DATE} [%p] UUID:%u{"RANDOM"} (%t) %c: %m%n</pattern>
</PatternLayout>