gpt4 book ai didi

java - 在Log4j2中,是否可以根据键从记录器中过滤掉某些键值对?

转载 作者:行者123 更新时间:2023-11-30 06:51:43 24 4
gpt4 key购买 nike

Log4j2 是否可以配置为过滤器或其他一些组件可以过滤掉在日志中打印的某些值? (但应允许同一行中的其他字段通过)

假设日志中出现以下行

 [operation=DONE, userName=junitUser, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca, 393ae7a0]], device=Device [id=12345, type=Pompom, info=Dot's Device]]], channel=null
[operation=DONE, userName=junitUser224, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca,393ae7a0]], device=Device [id=123456, type=Mamamia, info=tom's Device]]], channel=null

现在我可以过滤掉“userName”字段,使日志行现在不包含它,如下所示?

 [operation=DONE, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca, 393ae7a0]], device=Device [id=12345, type=Pompom, info=Dot's Device]]], channel=null
[operation=DONE, tenant=Tenant [tenantID=default], needDetails=1, message=BaseMsg [version=1.0, sdk=AppSDK [version=1.3, protocols=[4aac81ca,393ae7a0]], device=Device [id=123456, type=Mamamia, info=tom's Device]]], channel=null

这是我的 log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="/Users/dunston/logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".* zinger_log .*" onMatch="ACCEPT" onMismatch="DENY"/>

<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>

最佳答案

这可以通过 RewriteAppender 来完成。

您可能需要编写自定义 RewritePolicy它检查 LogEvent 的消息,如果格式化消息包含要过滤掉的正则表达式,则将消息替换为另一个实例。

您的自定义 RewitePolicy 可以像任何其他标准 Log4j2 plugin 一样在配置中进行配置。 。

关于java - 在Log4j2中,是否可以根据键从记录器中过滤掉某些键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42639332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com