Getting LogBack to log to console with TestNG and IntelliJ

If you’re having problems with a TestNG test using Logback not logging anything to the console inside IntelliJ, assuming you’re in a maven project, do the following:

create the file src/test/resources/logback-test.xml

Put this in it:

  1. <configuration>
  2.     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  3.         <!– encoders are assigned the type
  4.      ch.qos.logback.classic.encoder.PatternLayoutEncoder by default –>
  5.         <encoder>
  6.             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} – %m%n</pattern>
  7.         </encoder>
  8.     </appender>
  9.  
  10.     <logger name="com.whatever" level="DEBUG" />
  11.  
  12.     <root level="ERROR">
  13.         <appender-ref ref="STDOUT"/>
  14.     </root>
  15. </configuration>

And whatever you do, pay no mind at all to the highlighted rule in the configuration manual:

“Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  1. Logback tries to find a file called logback.groovy in the classpath.
  2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
  3. If no such file is found, it checks for the file logback.xml in the classpath..
  4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

The dirty trick you’re probably experiencing is that you typically see logback telling you about how its loading its configuration, but there are ways to make logback not give you that critical information on stdout. If you expected the highlighted last fallback option above to be working, and it’s not, some other dependency must have a lockback.xml file of some sort that’s redirecting logging elsewhere and prevent logback from telling you about that config choice.

Thanks to David Pratt, and Jason Duke, for helping me figure this out after way-too-much-brainpower-investment today.