JBT
JBT

Reputation: 8746

How to make Intellij show logs when running a test method

Suppose I have the following code:

private static final Logger log = LoggerFactory.getLogger( DummyTest.class );
@Test
public void test() {
    log.info("dummy log");
}

When I run the test method test() in Intellij, I didn't get any log in the console output. However, if I run the test via mvn test from commandline, I will get the log. BTW, I use testng.

So, how can I make Intellij to show the log?

Upvotes: 14

Views: 14682

Answers (2)

Pawel Gorczynski
Pawel Gorczynski

Reputation: 1305

I had the same problem and as of mid 2020 the following line solved my problem:

java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME).addHandler(new ConsoleHandler());

Upvotes: 0

Jolta
Jolta

Reputation: 2725

I had the same problem - no log output at all in console. Taking @wemu's advice, I added the following file to the src/test/resources/ directory of my project, which gave me the debug ouput from both the test code and the classes under test. Note: I'm using log4j2. The file needed will vary according to your logging framework.

log4j.xml:

<?xml version='1.0' encoding='UTF-8'?>
<configuration monitorInterval="30">
    <appenders>
        <Console name='Console' target='SYSTEM_OUT'>
            <PatternLayout pattern='%d{HH:mm:ss.SSS} %p [%t] %c{1.}.%M %m%n' />
        </Console>
    </appenders>
    <loggers>
        <root level='trace'>
            <appender-ref ref='Console' />
        </root>
    </loggers>
</configuration>

Upvotes: 4

Related Questions