39

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.

    <configuration>
    
      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
      </appender>
    
      <logger name="scala.slick" level="DEBUG"/>
    
      <root level="debug">
        <appender-ref ref="STDOUT" />
      </root>
    </configuration>
    
jans
  • 1,768
  • 3
  • 17
  • 22

3 Answers3

73

For those who're still struggling for how to make your scala-logging work in your sbt project. They just need to follow these steps:

  1. Add these two dependencies in your build.sbt:

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
    libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"
    
  2. Create a file logback.xml in your /src/main/resources/ and paste below mentioned content in that file.

    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <!-- path to your log file, where you want to store logs -->
            <file>/Users/yourusername/test.log</file>
            <append>false</append>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="debug">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="FILE" />
        </root>
    </configuration>
    
  3. Extend your Scala class or object with trait LazyLogging:

    import com.typesafe.scalalogging.slf4j.LazyLogging
    class MyClass extends LazyLogging {
      logger.debug("This is very convenient ;-)")
    }
    
  4. It's done.

P.S: Only a side note that logger is already a member of trait LazyLogging so you don't need to create it (as shown in above example)

Vinay
  • 1,280
  • 2
  • 13
  • 18
12

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.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Actually I already have the appropriate file 'logback.xml'. It still doesn't print any debug messages. I updated my question accordingly. – jans Mar 16 '15 at 13:06
  • 3
    Try adding `libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"` to build.sbt. You should also remove the old version (`"scala-logging-slf4j_2.11" % "2.1.2"`); they could conflict. – Alexey Romanov Mar 16 '15 at 15:21
  • 3
    Yes, adding `libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"` solves it. Thanks a lot. – jans Mar 16 '15 at 20:33
  • Is there some documentation for this? debug log level does not work for me as well. – Sohaib Jun 18 '15 at 09:50
  • My logger is working but now I want to ship my code to one of my friend, but I don't know how to update value inside file tag /path/to/file.log in logback.xml by scala code at runtime. Can anyone please let me know how to accomplish that? – Vinay Aug 18 '15 at 03:39
  • 1
    @Vinay You should ask it as a separate question. – Alexey Romanov Aug 18 '15 at 06:41
5

You're close, but you have to declare a logger instance using an SLF4J logger in the apply method for the Logger companion in com.typesafe.scalalogging. In your build.sbt include:

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

libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.10"

Then this will work:

import com.typesafe.scalalogging._
import org.slf4j.LoggerFactory

class MyClass extends LazyLogging {
  // ...
  val logger = Logger(LoggerFactory.getLogger(this.getClass))
  logger.debug("Here goes my debug message.")
  // ...
}

Here is a reference for LoggerFactory. Hope it helps!

Joseph Sawyer
  • 463
  • 3
  • 11
  • 7
    The `LazyLogging` trait [already includes a `logger` member that is an instance of the SLF4J Logger class](https://github.com/typesafehub/scala-logging/blob/5b45f24a35d0a4ee560cc3148bb63dde88047ede/src/main/scala/com/typesafe/scalalogging/Logging.scala#L27-L28), so I don't think this is it. – Sean Vieira Mar 16 '15 at 03:45
  • Thanks for your help. But it doesn't work for me. 'logger' is already defined in trait 'LazyLogging'. So I renamed 'logger' to 'mylogger' which compiles but still no debug messages are printed. – jans Mar 16 '15 at 13:03