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

查看:130
本文介绍了如何将uuid添加到log4j以登录文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个log4j2.xml文件:

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>

我的目标是使用uuid在RestEndpoint中添加唯一的ID,但是我不知道如何添加到xml文件中...或者我必须不在xml文件中进行配置?

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?

推荐答案

在您的应用程序中,将uuid放入ThreadContext:

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.

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

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>

这篇关于如何将uuid添加到log4j以登录文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆