在我的maven项目中,我有一个模块取决于另一个模块,它是测试代码/资源:
...
<scope>test</scope>
<type>test-jar</type>
...
现在,两个模块都有自己的 logback-test.xml ,每个模块都有一个特定的配置,用于在该特定模块中运行测试。但是,正如预期的那样,当在子模块中运行测试时,logback会抱怨路径中有多个 logback-test.xml ,并且使用它的默认日志记录配置执行此操作:
08:44:17,528 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
08:44:17,530 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [file:path/to/my/project/module2/logback-test.xml]
08:44:17,532 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback-test.xml] occurs multiple times on the classpath.
08:44:17,533 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback-test.xml] occurs at [file:/C:/path/to/my/project/module2/logback-test.xml]
08:44:17,533 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback-test.xml] occurs at [file:/C:/path/to/my/project/module1/logback-test.xml]
08:44:17,636 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
08:44:17,647 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
08:44:17,653 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
08:44:17,692 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
08:44:17,764 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to WARN
08:44:17,764 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
08:44:17,765 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.myproject] to TRACE
08:44:17,765 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [ch.qos.logback] to OFF
08:44:17,768 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5b0a485c - Registering current configuration as safe fallback point
我想知道如何解决这个问题..我想为每个模块保留多个配置;我想我不能从classpath中排除一个文件;我真的只想回来关闭我所知道的事情。从最后两行可以看出,我在配置中的“OFF”上有完整的logback包,但在此之后它仍然会继续记录。日志模式也与我在任一配置中配置的不同。
通过SLF4J在代码中配置和使用Logback。
在父模块中运行测试时,没有记录这些测试(INFO和WARN消息都没有),所以除了奇怪的Logback行为之外,我不能把它归咎于其他任何事情。
答案 0 :(得分:5)
您可以从测试jar中排除logback-test.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<excludes>
<excludes>logback-test.xml</excludes>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
或者(我还没试过)您可以使用命名约定logback-test-project.a.xml
,logback-test-project.b.xml
等,并使用-Dlogback.configurationFile=logback-test-project.a.xml
在每个项目中配置surefire。