如何在记录log4j2消息之前对其进行修改? [英] How to modify a log4j2 message before it is logged?

查看:82
本文介绍了如何在记录log4j2消息之前对其进行修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够为每个日志消息执行一个方法,该方法将使我能够在记录该消息之前对其进行动态修改.我知道有一些过滤器可以让我指示是否应该记录一条消息,但是我不想停止记录该消息,我只想在记录之前对其进行修改.我该怎么做呢?

I would like to be able to have a method execute for every log message that will allow me to dynamically modify that message before it is logged. I know there are Filters that allow me to indicate whether a message should be logged or not, but I don't want to stop the message from being logged, I just want to modify it before it is logged. How can I go about doing this?

注意:我问了有关log4j2的问题,但是我也对其他流行的Java日志记录框架持开放态度.

Note: I asked this question about log4j2 but I am open to other popular Java logging frameworks as well.

推荐答案

您可以实现自己的附加程序.

You can implement your own appender.

http://logging.apache.org/log4j/2.x/manual/appenders.html#RewriteAppender

只需扩展您正在使用的当前附加程序并添加所需的行为,或者简单地使用RewriteAppender

Just extend the current appender you are using and add the behaviour you need, or simply use the RewriteAppender

RewriteAppenderRewriteAppender允许在LogEvent被另一个Appender处理之前对其进行操作.这可用于掩盖敏感信息(例如密码)或将信息注入每个事件.RewriteAppender 必须配置有 RewritePolicy.应该在RewriteAppender引用的所有Appender之后对其进行配置,以使其能够正常关闭.

RewriteAppender The RewriteAppender allows the LogEvent to manipulated before it is processed by another Appender. This can be used to mask sensitive information such as passwords or to inject information into each event. The RewriteAppender must be configured with a RewritePolicy. The RewriteAppender should be configured after any Appenders it references to allow it to shut down properly.

这篇关于如何在记录log4j2消息之前对其进行修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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