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:
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"
This is how my class looks like basically:
import com.typesafe.scalalogging._ class MyClass extends LazyLogging { // ... logger.debug("Here goes my debug message.") // ... }
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>
Open build. Add the dependency for scala-logging, so the file contents look like this. Next, we need to download jar files for logback and add them to the runtime classpath. First, create a new directory in your project folder named libs. Then, add it to the project's classpath.
trait LazyLogging extends AnyRef Defines logger as a lazy value initialized with an underlying org. slf4j. Logger named according to the class into which this trait is mixed.
Log4j Scala API is a Scala logging facade based on Log4j 2. Support for Scala versions 2.10, 2.11, and 2.12 is provided, and experimental support for pre-release versions of 2.13 is also provided. Log4j Scala API uses Log4j 2.
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:
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"
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>
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 ;-)") }
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With