Search code examples
scalalogback

Unable to print LoggerContext in Play/Scala


I am following the following tutorial - https://logback.qos.ch/manual/configuration.html

I want to print LoggerContext in my code but I can't figure out how to do so. The code in the example is in Java and I want to write it in Scala.

  // assume SLF4J is bound to logback in the current environment
  LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
  // print logback's internal status
  StatusPrinter.print(lc);
  ...

I am facing two issues. getILoggerFactory seem to return ILoggerFactory and none of the print methods in StaticPrinter takes ILoggerFactory. Also, ILoggerFactory is ann interface with only one method, getLogger so I can't get any context information from it.


Solution

  • The analog code in Scala looks like:

      val lc = LoggerFactory.getILoggerFactory().asInstanceOf[LoggerContext]
      StatusPrinter.print(lc)
    

    Casting in Scala is done using .asInstanceOf[Class] - and is considered an Anti-Pattern.