<div dir="ltr">Hey,<div><br></div><div>I'm not sure if this is a logback or slf4j issue, so sorry if it's a slf4j issue.</div><div><br></div><div>My question is posted on stackoverflow ( <a href="http://stackoverflow.com/questions/31772577/org-slf4j-helpers-substitutelogger-cannot-be-cast-to-ch-qos-logback-classic-logg">http://stackoverflow.com/questions/31772577/org-slf4j-helpers-substitutelogger-cannot-be-cast-to-ch-qos-logback-classic-logg</a> ) but I will paste it here:</div><div><br></div><div><div>I've seen some questions very similar to this one (<a href="http://stackoverflow.com/questions/15483309/org-slf4j-helpers-noploggerfactory-cannot-be-cast-to-ch-qos-logback-classic-logg)">http://stackoverflow.com/questions/15483309/org-slf4j-helpers-noploggerfactory-cannot-be-cast-to-ch-qos-logback-classic-logg)</a>), but none of them received a good answer, or at least one that explained or solved this problem</div><div><br></div><div>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: <a href="https://bitbucket.org/pedrorijo91/logger-exp">https://bitbucket.org/pedrorijo91/logger-exp</a></div><div><br></div><div>The problem happens when I run `sbt test`, resulting in the following error:</div><div><br></div><div> {14:43:41} (#47) ~/Desktop/logger-exp/log-exp@pedrorijo(master) $ sbt test</div><div> Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=384m; support was removed in 8.0</div><div> [info] Loading global plugins from /Users/pedrorijo/.sbt/0.13/plugins</div><div> [info] Loading project definition from /Users/pedrorijo/Desktop/git/scala/logger-exp/log-exp/project</div><div> [info] Set current project to log-exp (in build file:/Users/pedrorijo/Desktop/git/scala/logger-exp/log-exp/)</div><div> [info] HelloTest:</div><div> [info] Exception encountered when attempting to run a suite with class name: HelloTest *** ABORTED ***</div><div> [info] java.lang.ExceptionInInitializerError:</div><div> [info] at HelloTest$$anonfun$1.apply$mcV$sp(HelloTest.scala:8)</div><div> [info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)</div><div> [info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)</div><div> [info] at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)</div><div> [info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)</div><div> [info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)</div><div> [info] at org.scalatest.Transformer.apply(Transformer.scala:22)</div><div> [info] at org.scalatest.Transformer.apply(Transformer.scala:20)</div><div> [info] at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:166)</div><div> [info] at org.scalatest.Suite$class.withFixture(Suite.scala:1122)</div><div> [info] ...</div><div> [info] Cause: java.lang.ClassCastException: org.slf4j.helpers.SubstituteLogger cannot be cast to ch.qos.logback.classic.Logger</div><div> [info] at com.example.Hello$.<init>(Hello.scala:8)</div><div> [info] at com.example.Hello$.<clinit>(Hello.scala)</div><div> [info] at HelloTest$$anonfun$1.apply$mcV$sp(HelloTest.scala:8)</div><div> [info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)</div><div> [info] at HelloTest$$anonfun$1.apply(HelloTest.scala:8)</div><div> [info] at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)</div><div> [info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)</div><div> [info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)</div><div> [info] at org.scalatest.Transformer.apply(Transformer.scala:22)</div><div> [info] at org.scalatest.Transformer.apply(Transformer.scala:20)</div><div> [info] ...</div><div> SLF4J: The following set of substitute loggers may have been accessed</div><div> SLF4J: during the initialization phase. Logging calls during this</div><div> SLF4J: phase were not honored. However, subsequent logging calls to these</div><div> SLF4J: loggers will work as normally expected.</div><div> SLF4J: See also <a href="http://www.slf4j.org/codes.html#substituteLogger">http://www.slf4j.org/codes.html#substituteLogger</a></div><div> SLF4J: com.example.Hello$</div><div> 14:43:52.846 [pool-6-thread-4-ScalaTest-running-WorldTest] INFO com.example.World$ - LOGGING</div><div> [info] WorldTest:</div><div> [info] - test</div><div> [info] Run completed in 456 milliseconds.</div><div> [info] Total number of tests run: 1</div><div> [info] Suites: completed 1, aborted 1</div><div> [info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0</div><div> [info] *** 1 SUITE ABORTED ***</div><div> [error] Error during tests:</div><div> [error] <span class="Apple-tab-span" style="white-space:pre"> </span>HelloTest</div><div> [error] (test:test) sbt.TestsFailedException: Tests unsuccessful</div><div> [error] Total time: 1 s, completed Aug 2, 2015 2:43:52 PM</div><div><br></div><div><br></div><div>but if I run each of the tests individually through:</div><div><br></div><div>`sbt "testOnly HelloTest"` and `sbt "testOnly WorlTest"` I get no error.</div><div><br></div><div>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 (<a href="http://www.slf4j.org/codes.html#substituteLogger">http://www.slf4j.org/codes.html#substituteLogger</a>).</div><div><br></div><div>But I can't understand:</div><div><br></div><div> 1. why it only happens in tests, and when running both tests? </div><div> 2. why it happens?</div><div> 3. how to debug/solve this problem?</div><div><br></div><div> As I said before, I've created a repository with a working example (working, meaning with the error happening) here: <a href="https://bitbucket.org/pedrorijo91/logger-exp">https://bitbucket.org/pedrorijo91/logger-exp</a></div></div><div><br></div><div><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Kind Regards,<br><br>Pedro Rijo</div></div>
</div>