这个bug是我在看源码的过程中发现的,已经发了jira给Log4J2的团队.
bug信息
- 背景:logger配置变化的时候,会通过LoggerContext的updateLogger方法更新,并发送一条propertyChangeEvent的事件
- 问题:请先看以下代码:
public void updateLoggers() {
updateLoggers(this.configuration);
}
public void updateLoggers(final Configuration config) {
final Configuration old = this.configuration;
for (final Logger logger : loggerRegistry.getLoggers()) {
logger.updateConfiguration(config);
}
firePropertyChangeEvent(new PropertyChangeEvent(this, PROPERTY_CONFIG, old, config));
}
大家可以看到,如果调用updateLoggers方法来更新配置,这样会导致PropertyChangeEvent中的old和new始终都会是一样的,对应的监听器如果有使用到就会出现问题。
影响范围
- LoggerContext中的setConfiguration 方法重复发送该事件,这可能是因为开始updateLoggers方法中没有firePropertyChangeEvent,加上之后代码没仔细review导致的,请看问题代码:
this.configuration = config;
updateLoggers();
if (prev != null) {
prev.removeListener(this);
prev.stop();
}
firePropertyChangeEvent(new PropertyChangeEvent(this, PROPERTY_CONFIG, prev, config));
- 监听了PropertyChangeEvent的监听会有错误处理的情况,就目前log4j-core的代码来看,只有在LoggerContextAdmin有监听这个事件,它的处理逻辑如下:
public void propertyChange(final PropertyChangeEvent evt) {
if (!LoggerContext.PROPERTY_CONFIG.equals(evt.getPropertyName())) {
return;
}
final Notification notif = new Notification(NOTIF_TYPE_RECONFIGURED, getObjectName(), nextSeqNo(), now(), null);
sendNotification(notif);
}
幸好event中的old和new没有被使用到。
结语
这么长时间都没有发现, 说明这个功能很有可能就是没啥人用的一个鸡肋的功能而已···