0

我不确定你们中有多少人对此很熟悉,但是在两个不同的环境中运行同一个应用程序会导致我的 Spring Boot 应用程序以不同的顺序加载其日志依赖项。我使用 logback-classic-1.1.7 和 slfj4j-log4j12-1.7.13 运行。

现在,我现在知道 logback 已经嵌入了 slfj(至少,现在我这样做了),它通常会显示一个多重绑定警告,然后它会继续。该应用程序将启动。

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Found binding in [jar:file:/root/dev2/target/lib/logback-classic-
1.1.7.jar! /org/slf4j/impl/StaticLoggerBinder.class]
SLF4J:  Found binding in [jar:file:/root/dev2/target/lib/
slf4j/slf4j-jdk14/1.5.3/slf4j-jdk14 1.5.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Actual binding is of type
[ch.qos.logback.classic.util.ContextSelectorStaticBinder]
//Spring then starts the app successfully here. 

我读过,有时当 slf4j 发现多个绑定时,它有时会随机选择一个。我已经删除了其他不必要的依赖项,这解决了我的问题。我更感兴趣的是为什么同一个应用程序会通过在具有相同 linux 版本的不同服务器上运行来加载错误的记录器。在另一个环境中运行,当我尝试启动它时的结果是:

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J:  Found binding in [jar:file:/root/dev3/target/lib/
slf4j/slf4j-jdk14/1.5.3/slf4j-jdk14 1.5.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/root/dev3/target/lib/logback-classic-
1.1.7.jar! /org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Actual binding is of type
[org.slf4j.impl.Log4jLoggerFactory]
Exception is in thread "main" java.lang.IllegalArgumentException: LoggerFactory
is not a Logback LoggerContext but Logback is on the classpath. 
Either remove Logback or the competing 
implemenataion (class org.slf4j.iml.Log4jLoggerFactory...
//app then fails to start 

在这种环境中,它以相反的顺序加载两个竞争的记录器,然后应用程序无法启动。有谁知道更多关于 Spring 加载依赖项的行为以及为什么它会在不同的环境中以不同的顺序加载它们?这导致此错误在一段时间内未被注意到。两种环境都有相同的 linux 版本。

提前致谢

4

0 回答 0