[logback-user] can't understand cast error

Paul Krause pkrause at investsoftech.com
Sun Aug 2 19:53:27 CEST 2015


It looks to me as if you are downcasting org.slf4j.Logger to ch.qos.logback.classic.Logger. Stop doing that and the problem should go away.  As to why it works in some configurations and not others, the answer is probably that one configuration loads the classes in one order and the other uses a different order.

Thanks,
Paul

On Aug 2, 2015, at 13:43, pedro rijo <pedrorijo91 at gmail.com<mailto:pedrorijo91 at gmail.com>> wrote:

Hey,

I'm not sure if this is a logback or slf4j issue, so sorry if it's a slf4j issue.

My question is posted on stackoverflow ( http://stackoverflow.com/questions/31772577/org-slf4j-helpers-substitutelogger-cannot-be-cast-to-ch-qos-logback-classic-logg ) but I will paste it here:

I've seen some questions very similar to this one (http://stackoverflow.com/questions/15483309/org-slf4j-helpers-noploggerfactory-cannot-be-cast-to-ch-qos-logback-classic-logg)), but none of them received a good answer, or at least one that explained or solved this problem

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 at 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: See also http://www.slf4j.org/codes.html#substituteLogger
    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?

 As I said before, I've created a repository with a working example (working, meaning with the error happening) here: https://bitbucket.org/pedrorijo91/logger-exp



--
Kind Regards,

Pedro Rijo
_______________________________________________
Logback-user mailing list
Logback-user at qos.ch<mailto:Logback-user at qos.ch>
http://mailman.qos.ch/mailman/listinfo/logback-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20150802/e8b1141c/attachment-0001.html>


More information about the Logback-user mailing list