3

Does anyone know how to configure loglevel when intellij runs a junit test from gradle? It seems to jump to debug for all and I don't see any place to configure it. The debug output is way too verbose for me.

Thanks

Peter

Peter Kahn
  • 12,364
  • 20
  • 77
  • 135
  • This question contains the solution. http://stackoverflow.com/questions/9356543/logging-while-testing-through-gradle – Benjamin Apr 12 '15 at 20:11

3 Answers3

6

Logging for tests must be configured properly or we go to default unconfigured which is LOG EVERYTHING. At least, that's my theory.

  • Create test/resources/logback.xml
  • Populate it with basic form
  • adjust for your use case

Below we just force all logging to info. Ideally, I want org.apache.* @ info and my classes at debug but I'll that requires me learning logback.xml format (which comes next)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
    </encoder>
</appender>
<root>
    <level value="INFO"/>
    <appender-ref ref="CONSOLE"/>
</root>
</configuration>
Community
  • 1
  • 1
Peter Kahn
  • 12,364
  • 20
  • 77
  • 135
1

Set up the path to log4j in VM option

-Dlog4j.configuration=file:/C:/myfolder/log4j.properties

With this the logger will be available to all the test classes. I have not tested with Gradle but have set up in maven and in my test configuration.

josliber
  • 43,891
  • 12
  • 98
  • 133
vsingh
  • 6,365
  • 3
  • 53
  • 57
0

You need to configure the logging framework(s) used by the code under test, e.g. by placing a configuration file into src/test/resources. How exactly this is done depends on which logging framework is used.

PS: Unless this is about Android Studio, Gradle is not involved when running a unit test from IntelliJ.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Thanks Peter. Is there documentation on which frameworks gradle uses by default? – Peter Kahn Oct 09 '14 at 15:25
  • Isn't this about logging performed by the code under test (or its dependencies), rather than logging performed by Gradle? As I said, Gradle is typically not involved when running unit tests from IntelliJ. Or are you testing a Gradle plugin? – Peter Niederwieser Oct 09 '14 at 15:27
  • Ok, so the problem is with intellij and how it runs unit tests not gradle then. My commandline gradle execution is fine. My IDE exeuction is way to verbose leading to a 2s test taking 2m. So, I need to find a way to configure logging in intellij's execution of the junit unit test – Peter Kahn Oct 09 '14 at 15:31
  • First step is to figure out why logging is more verbose in IntelliJ. Normally this isn't the case. Perhaps the IntelliJ project isn't set up correctly (e.g. `src/test/resources` isn't marked as a resource folder, or, in earlier IntelliJ versions that don't have a concept of resource folder, `src/test/resorces` isn't marked as a source folder or the logging configuration file doesn't match IntelliJ's pattern for resource files). Or the Gradle build configures logging in some other way (e.g. sets a system property for tests) which isn't replicated in the IntelliJ setup. – Peter Niederwieser Oct 09 '14 at 15:37
  • Actually, first step is to figure out how logging gets configured in the Gradle build. It's common to have a config file in `src/test/resources`. – Peter Niederwieser Oct 09 '14 at 15:38
  • Thanks. that was it... I started with this and configured logging properly http://stackoverflow.com/questions/25104992/configuring-logging-for-unit-tests-in-spring-boot-project-using-gradle-build – Peter Kahn Oct 09 '14 at 15:46