从log4j 1.2迁移到log4j 2-如何获取所有附加程序列表和滚动文件策略 [英] Migrating from log4j 1.2 to log4j 2 - how to get list of all appenders and rolling file strategy

查看:90
本文介绍了从log4j 1.2迁移到log4j 2-如何获取所有附加程序列表和滚动文件策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序从log4j 1.2迁移到log4j 2.0

I am in the process of migrating my application from log4j 1.2 to log4j 2.0

我已有代码:

Enumeration appenders = logger.getAllAppenders();
.
.
.
fileBackupIndex = rollingFileAppender.getMaxBackupIndex();

在log4j 2.0中,我找不到替换上述Java代码的方法.如何获取所有附加程序的列表以及如何以编程方式获取为RollingFile附加程序定义的最大值?

In log4j 2.0 I could not find way to replace above java code. How to get list of all appenders and how to get the max value defined for RollingFile appender programatically?

推荐答案

使用log4j2,API和CORE之间是分离的.这样,团队就可以在不破坏客户代码的情况下对实现进行更改.

With log4j2, there is a separation between API and CORE. This allows the team to make changes to the implementation without breaking client code.

因此,如果您的代码取决于实现细节,请注意,将来这可能会更改并且您的代码可能会中断.

So, if your code depends on implementation details, be aware that in the future this may change and your code may break.

也就是说,您可以像这样获得附加程序的地图:

That said, you can get a map of the appenders like this:

Logger logger = LogManager.getLogger();
Map<String, Appender> appenderMap = 
        ((org.apache.logging.log4j.core.Logger) logger).getAppenders();

您可以在地图上循环播放,直到找到RollingFileAppender.从这一点开始,它变得非常丑陋...您想要的信息全部在私有字段中,因此您需要使用反射来执行以下操作:

You can loop over the map until you find a RollingFileAppender. From this point it gets really ugly... The information you want is all in private fields, so you would need to use reflection to do the following:

  • 获取fileAppender的经理"字段,并将其转换为RollingFileManager
  • 获取经理的策略"字段并将其投射到DefaultRolloverStrategy
  • 获取defaultRolloverStrategy的"maxIndex"字段

这显然很脆弱...如果您确实需要此功能,则可以在log4j-dev邮件列表中请求此功能或创建JIRA票证.获得此功能的最快方法是提供功能请求的补丁.

This would obviously be pretty fragile... If you really need this you can request this feature on the log4j-dev mailing list or create a JIRA ticket. The quickest way to get this feature is if you provide a patch with the feature request.

这篇关于从log4j 1.2迁移到log4j 2-如何获取所有附加程序列表和滚动文件策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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