13
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
                    properties = "logging.level.root=OFF")
public class MyTest {
         @Test
         public void test() {}
}

As a result of the simple test above, I'm getting much startup noise logged. This was not the case before upgrading to spring-boot-2.x. How can I prevent this noise?

Especially, my Intellij IDE logs those statements in red, which is even more confusing as the test itself passes...

Jul 31, 2018 1:55:57 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper buildDefaultMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [MyTest], using SpringBootContextLoader
Jul 31, 2018 1:55:57 PM org.springframework.test.context.support.AbstractContextLoader generateDefaultLocations
INFO: Could not detect default resource locations for test class [MyTest]: no resource found for suffixes {-context.xml, Context.groovy}.
Jul 31, 2018 1:55:57 PM org.springframework.test.context.support.AnnotationConfigContextLoaderUtils detectDefaultConfigurationClasses
INFO: Could not detect default configuration classes for test class [MyTest]: MyTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
Jul 31, 2018 1:55:57 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getOrFindConfigurationClasses
INFO: Found @SpringBootConfiguration MyApp for test class MyTest
Jul 31, 2018 1:55:58 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getDefaultTestExecutionListenerClassNames
INFO: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.security.test.context.support.ReactorContextTestExecutionListener]
Jul 31, 2018 1:55:58 PM org.springframework.boot.test.context.SpringBootTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1a4013, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1b6e1eff, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@306f16f3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@702b8b12, org.springframework.test.context.support.DirtiesContextTestExecutionListener@22e357dc, org.springframework.test.context.transaction.TransactionalTestExecutionListener@49912c99, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@10163d6, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@2dde1bff, org.springframework.security.test.context.support.ReactorContextTestExecutionListener@15bbf42f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@550ee7e5, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@5f9b2141, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@247d8ae, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@48974e45, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@6a84a97d]

Maybe it has to do using log4j2?

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    Use `logging.level.root=off` – Korashen Jul 31 '18 at 12:10
  • 1
    I tried, but no success... – membersound Jul 31 '18 at 12:14
  • That is strange. It works for me with Spring Boot v2.0.4.RELEASE – Korashen Jul 31 '18 at 12:24
  • Could you as a test add the `log4j2` dependency configuration as added above? Maybe this might be the cause? – membersound Jul 31 '18 at 13:00
  • I have the same issue. Did you try reporting it with Spring, or make any other progress? I can get rid of almost all the output by manually specifying `@ContextConfiguration(locations = ..., classes = ...)` and `@TestExecutionListeners(inheritListeners = true)`, but the last line is still there. In addition, I wonder why this is logged to `stderr` (red) instead of `stdout`? – Jodiug Jul 15 '19 at 14:53
  • @Jodiug check out my latest answer on this question :), i hope that resolves your problem – Milan Desai Jan 24 '20 at 14:13
  • In addition to logging levels there are more settings: `debug: false` and `trace: false` in application properties, [more info in docs](https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-console-output) – Ivar Jan 24 '20 at 14:17

5 Answers5

8

Edited

Create logback-test.xml in your test resource (\test\resources), Then put bellow snippet into it

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="OFF"/>
</configuration>

It prints default system INFOs, not startup noise logs. (As you wanted)

And dependency of test can be as follows

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  • Thank you for your answer, and this is a way to silence the messages. I am still missing something though - this also hides other, useful messages. INFO logs should normally go to stdout. Do you know why the log output shows up in red (stderr) and not white (stdout)? – Jodiug Jan 24 '20 at 15:43
  • ^ Cannot edit. When I specify `properties = ["logging.pattern.console="]`, the log messages still appear in stderr. Same for `logback-test.xml`. – Jodiug Jan 24 '20 at 16:00
  • Sorry for misunderstanding, you mean that you want INFO s in console , but in white mode ? – Mehrdad HosseinNejad Yami Jan 24 '20 at 16:03
  • @membersound Edited, please check the new answer and test it – Mehrdad HosseinNejad Yami Jan 24 '20 at 17:51
  • Adding lines in `/src/test/resources/logback-test.xml` does change the test output: I can see white INFO or DEBUG messages from Spring if I turn it on and these messages disappear when I turn it OFF. However, the red INFO messages during test startup still appear independent of the setting in logback-test.xml. It leads me to believe that there is some "initializing" phase that happens before logback-test.xml is loaded. – Jodiug Jan 27 '20 at 08:48
  • @Jodiug I think the logs that you're talking about, is for logback initialization, with my solution the other noisy logs (like in question) will be ignored (as you wanted) – Mehrdad HosseinNejad Yami Jan 27 '20 at 10:28
  • The red INFO messages are from Spring and are exactly the same as the ones posted in the original question. The original question, and my question, is how to suppress these red stderr INFO messages. I really appreciate your effort, but your solution does not suppress these messages – Jodiug Jan 27 '20 at 10:51
3

You will need to create one logback-test.xml in your \test\resources directory, contents of this file can be following

<?xml version="1.0" encoding="UTF-8"?>
<configuration />

Why empty ?

Because you'd not want anything to be logged at this point in time, hence - empty configuration.

If you wish to find out more specific you can see some examples here

Milan Desai
  • 1,228
  • 8
  • 23
  • I will try this, though I am still missing something - this silences the above messages but may also hide other, useful messages. INFO logs should normally go to stdout. Do you know why the log output shows up in red (stderr) and not white (stdout)? – Jodiug Jan 24 '20 at 15:41
  • ^ Cannot edit, but this method does not work for me. I still get INFO output in stderr even though `logback-test.xml` is present - I suspect the logback config is only read after these messages appear. – Jodiug Jan 24 '20 at 15:58
1

First of all: I know, this is not a real answer, but way too long for a comment.

Using the log4j dependency didn't changed much in my output.

My POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

My application.properties is empty, so no log config there.

My test

package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = "logging.level.root=")
public class DemoApplicationTests
{

    @Test
    public void test()
    {
        System.out.println("Katzenbilder sind doof");
    }

}

Running the tests from within IntelliJ with logging.level.root=OFF leads to this output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

##### Optional[Hallo Welt]
##### Hallo Welt
Katzenbilder sind doof

Process finished with exit code 0

The two lines with #####are System.out.println() from a test bean.

Running the test with logging.level.root=INFO I get the expected clutter of Spring log messages.

I also, just for the sake of validation, put logging.level.root=INFO in my application.properties and had it to OFF in the test. No clutter, just the System.out.println() messages.

Korashen
  • 2,144
  • 2
  • 18
  • 28
0

This seems to be a configuration issue with log4j2. Pls refer the hierarchy of log4j2 configuration here.

In your case log4j2 is initially configured using log4j2.properties or log4j2.xml available under src/main/resources and hence INFO logs are printed on console. The logging level will be changed to OFF only when the test class is loaded, which is done only after startup logging noise is already printed.

To avoid any startup logging noise, pls add a log4j2.properties or log4j2.xml under src/test/resources with root logging level as ERROR (I would not prefer setting it to OFF as this would also suppress any errors).

I was not able to reproduce the error where startup logs were written to stderr. Pls check if you have set dest property to err in log4j2 configuration. This can force the startup logs to be written to stderr.

EDIT #1

Sharing my log4j2.xml configuration placed under src/main/resources for your reference.

Under src/test/resources, <Root level="info"> is changed to <Root level="error">.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="0">

  <Properties>
    <Property name="LOG_PATTERN">
      %d | %5p | [%t] | %c:%M(%L) | %m %n
    </Property>
  </Properties>

  <Appenders>
    <Console name="ConsoleAppender">
      <PatternLayout pattern="${LOG_PATTERN}" />
    </Console>
  </Appenders>

  <Loggers>
    <Root level="info">
      <AppenderRef ref="ConsoleAppender" />
    </Root>
  </Loggers>

</Configuration>
Dhaval Shewale
  • 224
  • 1
  • 6
  • I tried adding a log4j2.xml, logj4.xml, log4j2-test.xml, log4j-test.xml to the test resource folder, with log appenders and different root log levels. Unfortunately it has no effect. This is what a failed test looks like: https://i.imgur.com/xNlj9Pu.jpg – Jodiug Jan 29 '20 at 11:33
  • Can you pls share your `log4j2.xml` config. Let me check with the exact same configuration. I did not face any issues of having logs written to `stderr`. – Dhaval Shewale Jan 29 '20 at 13:46
  • I found this one online, and added some `AAAAAAA`'s to the log format to make sure I noticed if it would change: https://pastebin.com/raw/D7Nfspmj – Jodiug Jan 30 '20 at 10:39
  • I tried with your configuration in `src/test/resources` by changing `root` logging level to `info` and it works fine for me. Logs are written to `stdout` and not `stderr`. :) – Dhaval Shewale Jan 30 '20 at 10:52
  • Unfortunately I still see the same INFO logs in red, using your configuration in `src/test/resources/log4j2.xml`. I did a search on the word `dest` in our code base and that also came up empty... I guess it's time to post a minimal example and start A/B testing – Jodiug Jan 30 '20 at 11:19
  • @Jodiug I guess we will have to do that. Btw, which version of IntelliJ are you using? I am wondering can this be a bug in IntelliJ. Though unlikely but just a thought. I am using `IntelliJ IDEA 2019.3.2 (Community Edition)` and spring version `2.0.4.RELEASE` – Dhaval Shewale Jan 30 '20 at 11:26
0

Good night, lot's of answers here.. Regarding the log level configuration - all seem valid. But your problem may be more the maven build to be noisy - why don't you try to redirect the surefire's test output into separate files, one for each test ?

https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#redirectTestOutputToFile

https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#redirectTestOutputToFile

This could be nicely done globally as:

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
      </configuration>
  </plugin>
</pluginManagement>
Diogo Quintela
  • 191
  • 1
  • 4
  • Hi Diogo, thanks for your answer I had not tried this yet. With test output redirected to a file, the output of the actual test disappears but the red INFO messages upon the initialization of a SpringBootTest are still there! – Jodiug Jan 31 '20 at 09:23