[英]Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j Issue
I am writing Junits to test my logs.我正在编写 Junits 来测试我的日志。 I am using LogCaptor.我正在使用 LogCaptor。 I have attached my actual method and my testcases along with pom.xml.我附上了我的实际方法和我的测试用例以及 pom.xml。
This is my method where i have logs defined.这是我定义日志的方法。
@Override
public ApplicationResponse createApplication (final String serviceId, final String appId) throws KMSException {
log.debug("Entering createApplication");
try {
final Application application =
applicationOperations.createApplication(appId, serviceId);
return applicationResponse(application);
}
catch (KMSCommonException exception) {
throw new KMSInternalServerErrorException("Not able to create application", exception);
}
}
This is my test method
to test Loggers:这是我测试记录器的test method
:
@Test
public void testCreateApplicationLogger () throws KMSException {
String expectedInfoMessage = "Entering createApplication";
LogCaptor logCaptor = LogCaptor.forClass(Test.class);
adminAPIApplicationOpService.createApplication(TEST_SERVICE, TEST_APP + 1);
assertTrue( logCaptor.getInfoLogs().contains(expectedInfoMessage));
}
It is a conflict.这是一个冲突。 Run the command运行命令
mvn dependency:tree -Dverbose -Dincludes=log4j-to-slf4j
Check from where you import that dependency and exclude it.检查从哪里导入该依赖项并将其排除。
Reason is log4j
is an API.原因是log4j
是 API。 You must include in your project an implementation for that API to work.您必须在您的项目中包含一个实现,该 API 才能工作。 Both log4j-slf4j-impl
and log4j-to-slf4j
are implementations for log4j
API. log4j-slf4j-impl
和log4j-to-slf4j
都是log4j
API 的实现。 When you include multiple implementations conflicts arise.当您包含多个实现时,就会出现冲突。
I am the library maintainer and this behaviour is normal when there are multiple slf4j implementations present on the classpath.我是库维护者,当类路径上存在多个 slf4j 实现时,这种行为是正常的。 This known issue is also mentioned at the github page, see here: Using Log Captor alongside with other logging libraries github 页面也提到了这个已知问题,请参阅此处: 使用 Log Captor 与其他日志库一起使用
The library ships with couple out of the box options, a utility to redirect Java Util Logging to SLF4J and it is also able to redirect Log4j (Apache logging) to SLF4J.该库附带了几个开箱即用的选项,一个将 Java Util Logging 重定向到 SLF4J 的实用程序,它还能够将 Log4j(Apache 日志记录)重定向到 SLF4J。 Redirecting the Log4j logging is enabled by providing an slf4j implementation log4j-to-slf4j
see here for the details: https://github.com/Hakky54/log-captor/blob/3732e25c041af78153bfa7c0c83fb2874fd547b0/pom.xml#L84 The main reason why I have added this is to make it easier for end-users who are using Apache Log4j Core
for their logging. Redirecting the Log4j logging is enabled by providing an slf4j implementation log4j-to-slf4j
see here for the details: https://github.com/Hakky54/log-captor/blob/3732e25c041af78153bfa7c0c83fb2874fd547b0/pom.xml#L84 The main reason why I添加这是为了让使用Apache Log4j Core
进行日志记录的最终用户更容易。 It was a design decision... I didn't want the end-user to add manually log4j-to-slf4j
when they wanted to use LogCaptor
.这是一个设计决定......我不希望最终用户在他们想要使用 LogCaptor 时手动添加log4j-to-slf4j
LogCaptor
。 The downside of this option is that you will get an error if you have log4j-slf4j-impl
because SLF4J won't allow you to have multiple implementations.这个选项的缺点是如果你有log4j-slf4j-impl
你会得到一个错误,因为 SLF4J 不允许你有多个实现。
The solution is pretty simple.解决方案非常简单。 Just exclude log4j-slf4j-impl
during the test phase from your classpath.只需在测试阶段从类路径中排除log4j-slf4j-impl
。 Below is an example with maven and surefire/failsafe:以下是 maven 和安全/故障安全的示例:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>org.apache.logging.log4j:log4j-slf4j-impl</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>org.apache.logging.log4j:log4j-slf4j-impl</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
</plugins>
</build>
I have also created an example project for this specific use case as a cheat sheet for other developers, see here: https://github.com/Hakky54/java-tutorials/tree/main/log-captor-examples/log-captor-with-slf4j-log4j我还为此特定用例创建了一个示例项目作为其他开发人员的备忘单,请参见此处: https://github.com/Hakky54/java-tutorials/tree/main/log-captor-examples/log-captor -with-slf4j-log4j
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.