Disable scalatest logging statements when running tests from maven
Asked Answered
D

3

8

What is the method to disable logging on the scalatest log4j messages:

The log4j.properties is as follows:

log4j.rootLogger=INFO,CA,FA

#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{HH:mm:ss.SSS} %p %c: %m%n
log4j.appender.CA.Threshold = INFO


#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.append=false
log4j.appender.FA.file=target/unit-tests.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%d{HH:mm:ss.SSS} %p %c{1}: %m%n
log4j.appender.FA.Threshold = INFO

..
log4j.logger.org.scalatest=WARN

However we are seeing INFO level scalatest log4j messages:

2014-11-30 14:25:57,263 INFO  [ScalaTest-run-running-DiscoverySuite] Configuration.deprecation (Configuration.java:warnOnceIfDeprecated(840)) - hadoop.native.lib is deprecated. Instead, use io.native.lib.available
2014-11-30 14:25:57,493 INFO  [ScalaTest-run-running-DiscoverySuite] hbase.HBaseCommonTestingUtility (HBaseTestingUtility.java:startMiniCluster(840)) - Starting up minicluster with 1 master(s) and 2 regionserver(s) and 2 datanode(s)
2014-11-30 14:25:57,499 INFO  [ScalaTest-run-running-DiscoverySuite] hbase.HBaseCommonTestingUtility (HBaseTestingUtility.java:setupClusterTestDir(390)) - Created new mini-cluster data directory: /shared/hwspark/target/
Directive answered 1/12, 2014 at 0:42 Comment(4)
Any resolution to this problem? I am suffering from it as well.Aryn
@Aryn You need to ensure log4j.properties that you want to be used is in the first directory on the classpath.Directive
thanks for the response! Were you by chance using sbt for this? If so, how does one modify the classpath? I've tried doing it with "unmanagedClasspath in Test += -Dlog4j.configuration=src/main/resources/log4j.properties" but it didn't work.Aryn
I couldn't figure out the classpath thing. I did find this solution though. It works well enough: org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME) .asInstanceOf[ch.qos.logback.classic.Logger] .setLevel(ch.qos.logback.classic.Level.WARN)Aryn
O
3

Those log messages are not actually being printed by ScalaTest, but by something you are using from your ScalaTest tests. The reason "ScalaTest" shows up in them is that ScalaTest does change the name of threads when suites and tests are executed, so that if someone has a suite that hangs forever and does a thread dump to investigate, it is more obvious what test and suite is causing the run to hang. Log4J seems to print out the thread name in square brackets, so that can give you a hint as to where these log messages are coming from.

Oncoming answered 30/12, 2014 at 17:35 Comment(1)
Yes agreed. I will copy here my response on the scalatest ML: I should have updated that yes the log4j.properties is being pulled from one of the third party jar files when running spark. It is as you describe unrelated to scalatest. I need to find out how to properly specify the log4j.properties file when running maven - instead of having log4j just pick up whichever one it chooses from somewhere in the classpath.Directive
A
18

Alternatively, you can throw this bit of code anywhere in one of your tests,

org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME)
  .asInstanceOf[ch.qos.logback.classic.Logger]
  .setLevel(ch.qos.logback.classic.Level.WARN)

which will set all logging to the WARN level.

Aryn answered 16/4, 2015 at 3:25 Comment(2)
You are my man! This works guys. I used this to turn off my spark scala logsCombination
what is ch here Malcolm & @Combination , could you guys please look into this: #59848844Coruscate
O
3

Those log messages are not actually being printed by ScalaTest, but by something you are using from your ScalaTest tests. The reason "ScalaTest" shows up in them is that ScalaTest does change the name of threads when suites and tests are executed, so that if someone has a suite that hangs forever and does a thread dump to investigate, it is more obvious what test and suite is causing the run to hang. Log4J seems to print out the thread name in square brackets, so that can give you a hint as to where these log messages are coming from.

Oncoming answered 30/12, 2014 at 17:35 Comment(1)
Yes agreed. I will copy here my response on the scalatest ML: I should have updated that yes the log4j.properties is being pulled from one of the third party jar files when running spark. It is as you describe unrelated to scalatest. I need to find out how to properly specify the log4j.properties file when running maven - instead of having log4j just pick up whichever one it chooses from somewhere in the classpath.Directive
B
1

In my case it was slick.relational I looked at the classpath with the class reported in ScalaTest-run-running-.... information and found the class to be find in a package imported and added that specific package to logback.xml as

<logger name="slick.relational" level="INFO"/>

In your case search for HBaseTestingUtility or the other class reported there to find which jar it contains it and work out your logback logger name from the package prefix.

Badajoz answered 8/10, 2020 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.