Log4J-类似SiftingAppender的功能 [英] Log4J - SiftingAppender-like functionality

查看:120
本文介绍了Log4J-类似SiftingAppender的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个使用 Log4J 的项目中工作.要求之一是为每个线程创建一个单独的日志文件.这本身就是一个奇怪的问题,可以通过动态创建一个新的FileAppender并将其附加到Logger实例进行某种程度的排序.

I work in a project that uses Log4J. One of the requirement is to create a separate log file for each thread; this itself was a odd issue, somewhat sorted by creating a new FileAppender on the fly and attaching it to the Logger instance.

Logger logger = Logger.getLogger(<thread dependent string>);
FileAppender appender = new FileAppender();
appender.setFile(fileName);
appender.setLayout(new PatternLayout(lp.getPattern()));
appender.setName(<thread dependent string>);
appender.setThreshold(Level.DEBUG);
appender.activateOptions();
logger.addAppender(appender);

一切正常,直到我们意识到我们使用的另一个库- Spring框架 v3.0.0(使用常用日志记录)-不能与上述技术配合使用-仅由从log4.configuration文件初始化的Appender,但不是由运行时创建的Appender初始化的. 因此,回到正题.

Everything went fine until we realised that another library we use - Spring Framework v3.0.0 (which use Commons Logging) - does not play ball with the technique above – the Spring logging data is "seen" only by Appenders initialised from the log4.configuration file but not by the runtime created Appenders. So, back to square one.

经过调查,我发现新的和改进的 LogBack 具有附加器- SiftingAppender –正是我们所需要的,即线程级登录独立文件.

After some investigation, I found out that the new and improved LogBack has an appender - SiftingAppender – which does exactly what we need i.e. thread level logging on independent files.

目前,不能选择迁移到LogBack,因此,被Log4J所束缚,我如何才能实现类似SiftingAppender的功能并保持Spring的幸福呢?

At the moment, moving to LogBack is not an option, so, being stuck with Log4J, how can I achieve SiftingAppender-like functionality and keep Spring happy as well ?

注意:Spring仅用于 JdbcTemplate 功能,没有IOC;为了钩住" Spring的Commons Logging到Log4J,我在log4j.properties文件中添加了以下行:

Note: Spring is only used for JdbcTemplate functionality, no IOC; in order to "hook" Spring’s Commons Logging to Log4J I added this line in the log4j.properties file:

log4j.logger.org.springframework =调试

log4j.logger.org.springframework=DEBUG

按照此处的指示.

推荐答案

在Log4j2中,我们现在可以使用

In Log4j2, we can now use RoutingAppender:

RoutingAppender评估LogEvent,然后将其路由到下级Appender.目标Appender可以是先前配置的Appender,可以通过其名称进行引用,也可以根据需要动态创建Appender.

The RoutingAppender evaluates LogEvents and then routes them to a subordinate Appender. The target Appender may be an appender previously configured and may be referenced by its name or the Appender can be dynamically created as needed.

从他们的常见问题解答:

如何动态写入单独的日志文件? 看一下RoutingAppender.您可以在配置中定义多个路由,并将值放在ThreadContext映射中,以确定该线程中的后续事件记录到哪个日志文件中.

How do I dynamically write to separate log files? Look at the RoutingAppender. You can define multiple routes in the configuration, and put values in the ThreadContext map that determine which log file subsequent events in this thread get logged to.

这篇关于Log4J-类似SiftingAppender的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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