log4j将属性重置为原始文件,如log4j.properties文件中所示 [英] log4j reset properties back to original as in log4j.properties file

查看:202
本文介绍了log4j将属性重置为原始文件,如log4j.properties文件中所示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将log4j.properites定义如下

In my application, I defined log4j.properites as follows

log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.Subject=email Notification

以后的程序我正在动态地将主题更改为

later in the program i am dynamically changing the subject to

Properties prop = new Properties();
prop.setProperty("log4j.appender.email.Subject", "Test Completed");

使用此变量后,我想将其重置为原始文件。所以我这样做了

After I use this variable, I wan to reset this back to original on file. so I did this

LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);

但是,每当我使用这个主题属性时,代码后面的值都是'测试完成。任何重置配置的建议都非常感谢。谢谢

But, later in the code whenever I use this subject property it is giving its value as 'Test Completed'. Any suggestion to reset configuration is greatly appreciated. Thanks

推荐答案

如上述评论所述

LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);

不要重新配置现有的Logger实例,这样他们仍然可以使用旧的EmailAppender。为了使更改生效,您应该重新创建记录器。
如果不可能(例如你的记录器是静态的最终字段),你可以创建一个简单的Logger包装器,它将自己注册一些监听器,它将通知配置更改,以便包装器可以创建新的记录器实例

don't reconfigure already existing Logger instances, so that they still use your old EmailAppender. In order to make changes to take effect you should recreate loggers. If it's not possible (your loggers are static final fields for example), you can create a simple Logger wrapper, which will register itself with some listener, that will notify on configuration change, so that wrapper can create fresh logger instance

这篇关于log4j将属性重置为原始文件,如log4j.properties文件中所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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