我的一个 Maven 模块在运行测试时忽略了我的日志记录级别。
在
src/test/resources
我有application.properties
:
app.name=bbsng-import-backend
app.description=Import Backend Module for Application
spring.profiles.active=test
# LOGGING
logging.level.root=error
logging.level.org.springframework.core =fatal
logging.level.org.springframework.beans=fatal
logging.level.org.springframework.context=fatal
logging.level.org.springframework.transaction=error
logging.level.org.springframework.test=error
logging.level.org.springframework.web=error
logging.level.org.hibernate=ERROR
我也试过
application-test.properties
.
我的应用程序日志很多,尤其是在加载上下文时。我尝试了
logback.xml
、logback-test.xml
和 logback-spring.xml
但没有任何帮助。
我的pom:
<parent>
<groupId>at.company.bbsng</groupId>
<artifactId>bbsng-import</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>bbsng-import-backend</artifactId>
<name>bbsng-import-backend</name>
<properties>
<start-class>at.company.bbsng.dataimport.ApplicationImportBackend</start-class>
</properties>
<dependencies>
<!-- APPLICATION ... -->
<dependency>
<groupId>at.company.bbsng</groupId>
<artifactId>bbsng-app-domain</artifactId>
<scope>test</scope>
</dependency>
<!-- SPRING ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>test</scope>
</dependency>
<!-- JAVAX ... -->
...
<!-- COMMONS ... -->
...
<!-- LOMBOK ... -->
...
<!-- DB -->
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${org.springframework.boot-version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
一个简单的测试类:
@ContextConfiguration(classes = { ApplicationImportBackend.class })
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {
@Autowired
private JobLauncher jobLauncher;
@Test
public void testSimpleProperties() throws Exception {
assertNotNull(jobLauncher);
}
}
应用程序日志处于调试模式。
是的,
application.properties
将被加载。我已经尝试通过错误的配置破坏应用程序。
感谢您的任何提示。
好吧,我现在做了什么,在我配置的所有模块中,如下所示:
src/main/资源:
我在
application.properies
中使用日志记录配置,如问题中所述的logging.level.*
。
src/测试/资源:
我用
logback-test.xml
喜欢:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="*.myapp" level="error" />
<logger name="org.springframework.core " level="error" />
<logger name="org.springframework.beans" level="error" />
<logger name="org.springframework.context" level="error" />
<logger name="org.springframework.transaction" level="error" />
<logger name="org.springframework.web" level="error" />
<logger name="org.springframework.test" level="error" />
<logger name="org.hibernate" level="error" />
</configuration>
但我仍然不明白,为什么在少数模块中我可以使用 application.properties,但在另一个模块中它会忽略......但现在它对我有用。
但可能仍然欢迎一些具有背景知识的提示。
我没有将我的答案标记为解决方案,因为它仍然感觉像是一种解决方法。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework" level="INFO"/>
</configuration>
作为快速修复,我将包含上述内容的
logback.xml
文件放在 src/test/resources
中并且它有效。
我也在寻找解决方案,同时我正在使用以下解决方案:
这不是最好的,但它有效
@BeforeClass
public static void setErrorLogging() {
LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR);
}
LoggingSystem
:对日志系统的通用抽象。
->
get
:检测并返回正在使用的日志系统。支持 Logback 和 Java 日志记录
setLogLevel
:
设置给定记录器的日志记录级别。
确保更改所有其他测试类的日志级别。
希望对你有帮助,祝你好运
如果您的测试带有
@DataJpaTest
注释,您可以使用以下命令关闭 Hibernate SQL 注销:
@DataJpaTest(showSql=false)
public class MyTest {
..
}
试试这个:
@ContextConfiguration(classes = ApplicationImportBackend.class,
initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {
//...
}
按照教程,可以在src/test/resources目录下添加一个logback.xml文件
如果您需要对 logback 应用超出 application.properties 所能实现的定制,则需要添加一个标准的 logback 配置文件。您可以将 logback.xml 文件添加到类路径的根目录以供 logback 查找。如果你想使用 Spring Boot Logback 扩展,你也可以使用 logback-spring.xml。
logback.xml文件的例子:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include
resource="org/springframework/boot/logging/logback/defaults.xml" />
<include
resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="warn">
<appender-ref ref="CONSOLE" />
</root>
<logger name="ua.com.company" level="warn" />
<logger name="org.springframework.core " level="warn" />
<logger name="org.springframework.beans" level="warn" />
<logger name="org.springframework.context" level="warn" />
<logger name="org.flyway" level="debag" />
<logger name="org.springframework.transaction" level="warn" />
<logger name="org.springframework.web" level="debug" />
<logger name="org.springframework.test" level="warn" />
<logger name="org.hibernate" level="warn" />
</configuration>