I'm not sure if this is a logback or slf4j issue, so sorry if it's a slf4j issue.
I was able to create a very small project (basically 2 Scala classes - each with a logger - and 2 test classes) with a similar structure to my real project where I found the problem. The example is available here:
https://bitbucket.org/pedrorijo91/logger-exp
The problem happens when I run `sbt test`, resulting in the following error:
{14:43:41} (#47) ~/Desktop/logger-exp/log-exp@pedrorijo(master) $ sbt test
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=384m; support was removed in 8.0
[info] Loading global plugins from /Users/pedrorijo/.sbt/0.13/plugins
[info] Loading project definition from /Users/pedrorijo/Desktop/git/scala/logger-exp/log-exp/project
[info] Set current project to log-exp (in build file:/Users/pedrorijo/Desktop/git/scala/logger-exp/log-exp/)
[info] HelloTest:
[info] Exception encountered when attempting to run a suite with class name: HelloTest *** ABORTED ***
[info] java.lang.ExceptionInInitializerError:
[info] at HelloTest$$anonfun$1.apply$mcV$sp(HelloTest.scala:8)
[info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)
[info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)
[info] at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
[info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info] at org.scalatest.Transformer.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer.apply(Transformer.scala:20)
[info] at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:166)
[info] at org.scalatest.Suite$class.withFixture(Suite.scala:1122)
[info] ...
[info] Cause: java.lang.ClassCastException: org.slf4j.helpers.SubstituteLogger cannot be cast to ch.qos.logback.classic.Logger
[info] at com.example.Hello$.<init>(Hello.scala:8)
[info] at com.example.Hello$.<clinit>(Hello.scala)
[info] at HelloTest$$anonfun$1.apply$mcV$sp(HelloTest.scala:8)
[info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)
[info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)
[info] at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
[info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info] at org.scalatest.Transformer.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer.apply(Transformer.scala:20)
[info] ...
SLF4J: The following set of substitute loggers may have been accessed
SLF4J: during the initialization phase. Logging calls during this
SLF4J: phase were not honored. However, subsequent logging calls to these
SLF4J: loggers will work as normally expected.
SLF4J: com.example.Hello$
14:43:52.846 [pool-6-thread-4-ScalaTest-running-WorldTest] INFO com.example.World$ - LOGGING
[info] WorldTest:
[info] - test
[info] Run completed in 456 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 1
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] *** 1 SUITE ABORTED ***
[error] Error during tests:
[error] HelloTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 1 s, completed Aug 2, 2015 2:43:52 PM
but if I run each of the tests individually through:
`sbt "testOnly HelloTest"` and `sbt "testOnly WorlTest"` I get no error.
As far as I've seen this is not just a Scala issue, it seems that in Java is also possible to happen. Furthermore, I've read that during initialization the loggers are substituted by a Substitute Logger (
http://www.slf4j.org/codes.html#substituteLogger).
But I can't understand:
1. why it only happens in tests, and when running both tests?
2. why it happens?
3. how to debug/solve this problem?