我使用 Logback 和 Slf4j 进行日志记录,配置以
logback.groovy
下的 src/main/resources
的形式存在,它总是像魅力一样工作。
我正要将日志配置升级到可用的最新版本,并偶然发现了令人不快的问题。
依赖项
ch.qos.logback:logback-classic:1.2.9
显示警告:
Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
ch.qos.logback.core.LogbackException: Unexpected filename extension of file [file:/[myproj]/build/resources/main/logback.groovy]. Should be either .groovy or .xml
at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:67)
at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:140)
at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
...
我检查了最后一个工作版本
ch.qos.logback.classic.util.ContextInitializer.configureByResource()
和1.2.8
的代码1.2.9
:
// 1.2.8
public void configureByResource(URL url) throws JoranException {
if (url == null) {
throw new IllegalArgumentException("URL argument cannot be null");
}
final String urlString = url.toString();
if (urlString.endsWith("groovy")) {
if (EnvUtil.isGroovyAvailable()) {
// avoid directly referring to GafferConfigurator so as to avoid
// loading groovy.lang.GroovyObject . See also http://jira.qos.ch/browse/LBCLASSIC-214
GafferUtil.runGafferConfiguratorOn(loggerContext, this, url);
} else {
StatusManager sm = loggerContext.getStatusManager();
sm.add(new ErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.", loggerContext));
}
} else if (urlString.endsWith("xml")) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(url);
} else {
throw new LogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml");
}
}
// 1.2.9
public void configureByResource(URL url) throws JoranException {
if (url == null) {
throw new IllegalArgumentException("URL argument cannot be null");
}
final String urlString = url.toString();
if (urlString.endsWith("xml")) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(url);
} else {
throw new LogbackException("Unexpected filename extension of file [" + url.toString() + "]. Should be either .groovy or .xml");
}
}
因此,警告可能会产生误导,但代码更改是显而易见的:尽管 ref-doc 中没有提供“弃用”警告,但删除了 groovy 支持。
为什么 groovy 支持已被删除?
为什么没有引入“logback-groovy”包来提供支持?
附注在项目的 GitHub 上没有“问题”部分,这很奇怪......
检查第 4 点:
https://logback.qos.ch/news.html#1.2.9
这是对 CVE-2021-42550 的回应。另请参阅LOGBACK-1591。
删除了 Groovy 配置支持。由于日志记录如此普遍,并且 Groovy 的配置可能过于强大,因此出于安全原因不太可能恢复此功能。
有一个 3rd 方插件可以重新添加 Groovy 支持,可在此处获取:https://github.com/virtualdogbert/logback-groovy-config