log4j 将属性重置回原始值,如 log4j.properties 文件中所示
在我的应用程序中,我在程序中定义了 log4j.properites,如下所示,
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.Subject=email Notification
我动态地将主题更改为“
Properties prop = new Properties();
prop.setProperty("log4j.appender.email.Subject", "Test Completed");
使用此变量后,我想将其重置回文件中的原始值”。所以我这样做了
LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);
但是,稍后在代码中每当我使用此主题属性时,它都会将其值指定为“测试已完成”。非常感谢任何重置配置的建议。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我在上面的评论中所述,两者都
不会重新配置现有的 Logger 实例,因此它们仍然使用旧的 EmailAppender。为了使更改生效,您应该重新创建记录器。
如果不可能(例如,您的记录器是静态最终字段),您可以创建一个简单的记录器包装器,它将向某个侦听器注册自身,该侦听器将通知配置更改,以便包装器可以创建新的记录器实例
As stated in my comments above both
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
这是部分答案(我知道);但是您可以在没有显式
LogManager.resetConfiguration();
的情况下进行重置https://logging.apache.org/log4j/ 1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
This is a partial answer (I know); But you can do a reset without explicit
LogManager.resetConfiguration();
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html