jans jans - 1 year ago 107
Scala Question

Complete "Scala Logging" Example

I'm trying to use Typesafe's Scala Logging but couldn't get it to print any debug message. I expect Scala Logging to print debug message to the default screen but it doesn't work. A complete example would be very helpful or specific advise what to change would be great too. I use Scala 2.11. Here is what I did:

  1. I added the dependency to build.sbt:

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"

    Even though I'm not sure if this is required, I added the following line but it didn't do any difference:

    libraryDependencies += "com.typesafe.scala-logging" % "scala-logging-slf4j_2.11" % "2.1.2"

  2. This is how my class looks like basically:

    import com.typesafe.scalalogging._

    class MyClass extends LazyLogging {
    // ...
    logger.debug("Here goes my debug message.")
    // ...

  3. I discovered the file ./src/main/resources/logback.xml but am not sure which module installed it and if its relevant. I changed the log level to "debug" without effect.


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
    ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

    <logger name="scala.slick" level="DEBUG"/>

    <root level="debug">
    <appender-ref ref="STDOUT" />

Answer Source

IIRC it'll print messages starting from info level by default. To change this, you need to put logback.xml file into src/main/resources (or use -Dlogback.configurationFile=/path/to/config.xml JVM parameter). See Configuration chapter in Logback documentation.