Fix test logging so that it also uses Log4j2 & sends logs to failsafe, etc

This commit is contained in:
Tim Donohue
2021-11-05 17:32:56 -05:00
parent c61d47e6d7
commit 4c87bded7d
7 changed files with 101 additions and 70 deletions

View File

@@ -38,6 +38,8 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
@@ -70,6 +72,11 @@ import org.springframework.web.context.WebApplicationContext;
@ContextConfiguration(initializers = { DSpaceKernelInitializer.class, DSpaceConfigurationInitializer.class })
// Tell Spring to make ApplicationContext an instance of WebApplicationContext (for web-based tests)
@WebAppConfiguration
// Load our src/test/resources/application-test.properties to override some settings in default application.properties
@TestPropertySource(locations = "classpath:application-test.properties")
// Enable our custom Logging listener to log when each test method starts/stops
@TestExecutionListeners(listeners = {LoggingTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class AbstractControllerIntegrationTest extends AbstractIntegrationTestWithDatabase {
protected static final String AUTHORIZATION_HEADER = "Authorization";

View File

@@ -21,6 +21,8 @@ import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
/**
@@ -47,6 +49,11 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// Load DSpace initializers in Spring ApplicationContext (to initialize DSpace Kernel & Configuration)
@ContextConfiguration(initializers = { DSpaceKernelInitializer.class, DSpaceConfigurationInitializer.class })
// Load our src/test/resources/application-test.properties to override some settings in default application.properties
@TestPropertySource(locations = "classpath:application-test.properties")
// Enable our custom Logging listener to log when each test method starts/stops
@TestExecutionListeners(listeners = {LoggingTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class AbstractWebClientIntegrationTest extends AbstractIntegrationTestWithDatabase {
// (Random) port chosen for test web server
@LocalServerPort

View File

@@ -0,0 +1,44 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.test;
import org.apache.logging.log4j.Logger;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* Custom DSpace TestExecutionListener which logs messages whenever a specific Test Case (i.e. test method) has
* started or ended execution. This makes Test environment logs easier to read/understand as you know which method has
* caused errors, etc.
*/
public class LoggingTestExecutionListener extends AbstractTestExecutionListener {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(LoggingTestExecutionListener.class);
/**
* Before each test method is run
* @param testContext
*/
@Override
public void beforeTestMethod(TestContext testContext) {
// Log the test method being executed. Put lines around it to make it stand out.
log.info("---");
log.info("Starting execution of test method: {}()", testContext.getTestMethod().getName());
log.info("---");
}
/**
* After each test method is run
* @param testContext
*/
@Override
public void afterTestMethod(TestContext testContext) {
// Log the test method just completed.
log.info("Finished execution of test method: {}()", testContext.getTestMethod().getName());
}
}

View File

@@ -0,0 +1,17 @@
#
# The contents of this file are subject to the license and copyright
# detailed in the LICENSE and NOTICE files at the root of the source
# tree and available online at
#
# http://www.dspace.org/license/
#
#
# application-test.properties contains our Spring Boot configuration for TEST environment
#
# By default, our Spring Boot TEST environment uses the src/main/resources/application.properties file.
# However, this file can be used to override specific application.properties settings for the test environment.
## Log4j2 configuration for test environment
## This file is found on classpath at src/test/resources/log4j2-test.xml
logging.config = classpath:log4j2-test.xml

View File

@@ -1,58 +0,0 @@
#
# The contents of this file are subject to the license and copyright
# detailed in the LICENSE and NOTICE files at the root of the source
# tree and available online at
#
# http://www.dspace.org/license/
#
###########################################################################
#
# log4j.properties
#
#
###########################################################################
# This is a copy of the log4j configuration file for DSpace, to avoid
# getting errors when running tests.
# Set root category priority to INFO and its only appender to A1.
log4j.rootCategory=INFO, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.logging.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p %c @ %m%n
###########################################################################
# Other settings
###########################################################################
# Block passwords from being exposed in Axis logs.
# (DEBUG exposes passwords in Basic Auth)
log4j.logger.org.apache.axis.handlers.http.HTTPAuthHandler=INFO
# Block services logging except on exceptions
log4j.logger.org.dspace.kernel=ERROR
log4j.logger.org.dspace.services=ERROR
log4j.logger.org.dspace.servicemanager=ERROR
log4j.logger.org.dspace.providers=ERROR
log4j.logger.org.dspace.utils=ERROR
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
#
# Root logger option
log4j.rootLogger=INFO, stdout
# Hibernate logging options (INFO only shows startup messages)
log4j.logger.org.hibernate=INFO
# For detailed Hibernate logging in Unit Tests, you can enable the following
# setting which logs all JDBC bind parameter runtime arguments.
# This will drastically increase the size of Unit Test logs though.
#log4j.logger.org.hibernate.SQL=DEBUG, A1
#log4j.logger.org.hibernate.type=TRACE, A1

View File

@@ -0,0 +1,26 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
log4j2-test.xml
Simple log4j configuration file used for our Integration Test environment.
Its goal is to simply output logs console, which will send those logs to failsafe-reports (or similar)
-->
<Configuration strict='true'
xmlns='http://logging.apache.org/log4j/2.0/config'>
<Appenders>
<Appender name='A1' type='Console'>
<Layout type='PatternLayout' pattern='%d %-5p %c @ %m%n'/>
</Appender>
</Appenders>
<Loggers>
<!-- DSpace packages should log at INFO level. For debugging tests, this may be changed to DEBUG -->
<Logger name="org.dspace" level="INFO" />
<!-- Default is that all classes only log warnings or errors -->
<Root level='WARN'>
<AppenderRef ref='A1'/>
</Root>
</Loggers>
</Configuration>

View File

@@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- Set custom log levels for Spring REST integration tests here -->
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="net.sf.ehcache" level="INFO"/>
<logger name="org.apache.solr" level="WARN"/>
<!-- Set this to DEBUG to see the JSON output of each request made in an IT test -->
<logger name="org.springframework.test.web.servlet.result" level="INFO"/>
</configuration>