125

One of my maven module ignores my logging levels when running tests.

In src/test/resources I have 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

I also tried application-test.properties.

My Application logs a lot, especially when loading context. I tried logback.xml, logback-test.xml and logback-spring.xml but nothing helps.

My 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>

One simple Test class:

@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 logs is in DEBUG Mode.

And yes, the application.properties will be loaded. I already tried to break the application by wrong config.

Thank you for any hints.

Mr.Q
  • 4,316
  • 3
  • 43
  • 40
Michael Hegner
  • 5,555
  • 9
  • 38
  • 64

7 Answers7

128

Okay what I did now, in all modules I configured as follows:

src/main/resources:
I use logging configuration in application.properies like logging.level.* as described in the question.

src/test/resources:
I use logback-test.xml like:

<?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>

But I still don't understand, why in few modules I could use application.properties, but in another module it ignores ... But for now it works for me as it is.

But maybe few hints with background knowledge are still welcome.

I dont mark my answer as solution, cos it still feels like a workaround.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Michael Hegner
  • 5,555
  • 9
  • 38
  • 64
  • 7
    My assumption is that `application.properties` is being parsed later than test initialization. That's why `org.springframework.test` has no effect on initial test logging. – Elnur Abdurrakhimov May 24 '16 at 05:39
  • 4
    This is awesome. I added `` and `` to really minimized the noise. If you're using swagger, you can also add ``. Well done. Have a bounty! – Bohemian Dec 15 '17 at 16:11
  • 2
    This helped, although I started seeing a bunch of logs from logback itself after I tried adding the `logback-test.xml` file. To turn those off [I followed the main answer from this StackOverflow post](https://stackoverflow.com/questions/3257154/how-to-prevent-logback-from-outputting-its-own-status-at-the-start-of-every-log) - and BAM, I've managed to get rid of all the precursory logging when running tests. – Danny Bullis Apr 04 '18 at 22:51
  • 1
    My `logback-test.xml` file was as simple as this: – Danny Bullis Apr 04 '18 at 22:52
  • 1
    Same Problem. 2 Moduls with exactly the same Configuration (base class). One has logging during context creation, the other not. Changes in application.properties (logging.level) take effect. Using above `logback-test.xml` works as expected. (+1) thanks – Torsten Apr 01 '19 at 10:27
  • @DannyBullis, you may also simply omit the `include resource` line to prevent logback metadata from being logged. – jaco0646 Feb 06 '21 at 04:27
  • Stumbled on this post, has a similar experience. From what I have found, test slices etc. which may not bootstrap the entire spring context also do not necessarily load application.yml/properties. Couldn't say for sure if the times you're finding that it's not working are that. – Eric P May 19 '22 at 02:02
46
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="INFO"/>
</configuration>

As a fast fix, I put logback.xml file with the above content in src/test/resources and it works.

Gama11
  • 31,714
  • 9
  • 78
  • 100
dnocode
  • 1,928
  • 1
  • 18
  • 21
23

To enable application.properties need to add an annotation @SpringBootTest to test class, read more here.

Gama11
  • 31,714
  • 9
  • 78
  • 100
ksandr
  • 361
  • 3
  • 9
  • 6
    @SpringBootTest is for integration tests and therefore the application.properties will be loaded. But for unit tests this is not the correct answer. – Tobsch Aug 26 '19 at 18:06
19

I'm also looking for a solution to that, meanwhile I'm using the following solution:

this isn't the best but it works

@BeforeClass
public static void setErrorLogging() {
   LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR);
}

LoggingSystem: a common abstraction over logging systems.

->

get: Detect and return the logging system in use. Supports Logback and Java Logging

setLogLevel: Sets the logging level for a given logger.

Make sure to change back log level for all other test classes.

Hope it helps you, goodluck

Gama11
  • 31,714
  • 9
  • 78
  • 100
Idan
  • 201
  • 3
  • 5
9

If your tests are annotated with @DataJpaTest you can switch Hibernate SQL logging off with:

@DataJpaTest(showSql=false)
public class MyTest {
  ..
}
Gama11
  • 31,714
  • 9
  • 78
  • 100
uı6ʎɹnɯ ꞁəıuɐp
  • 3,431
  • 3
  • 40
  • 49
2

Try this:

@ContextConfiguration(classes = ApplicationImportBackend.class, 
    initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {
    //...
}
Gama11
  • 31,714
  • 9
  • 78
  • 100
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
1

According to the tutorial, you can add a logback.xml file to the src/test/resources directory

If you need to apply customizations to logback beyond those that can be achieved with application.properties, you will need to add a standard logback configuration file. You can add a logback.xml file to the root of your classpath for logback to find. You can also use logback-spring.xml if you want to use the Spring Boot Logback extensions.

The example of the logback.xml file:

<?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>
Mohammad abumazen
  • 1,286
  • 1
  • 11
  • 24
  • 1
    Thank you for the comment. The Problem described here are project using maven modules. For simple projects putting logback.xml into src/test/resources is fine – Michael Hegner May 11 '23 at 15:32