How to use Java-style logging in Scala with SLF4J

Problem: You want to add Java-style logging to your Scala application, and you’re comfortable with the Java SLF4J library.


Assuming you’re using SBT, include the necessary SLF4J dependencies in your build.sbt file:

name := "SLF4JTest"

version := "1.0"

scalaVersion := "2.10.0"

libraryDependencies ++= Seq("org.slf4j" % "slf4j-api" % "1.7.5",
                            "org.slf4j" % "slf4j-simple" % "1.7.5")

Alternatively, download the SLF4J libraries and put them in your lib folder.

You can then use SLF4J as you would with Java, as shown in this example, creating a logger instance, and then using it in your code:

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class Pizza {
  val logger = LoggerFactory.getLogger(classOf[Pizza])"Hello from the Pizza class")

object Main extends App {
  val p = new Pizza

Running that file with sbt run prints several lines of output, including this line from

[run-main] INFO Pizza - Hello from the Pizza class


SLF4J is a popular Java logging solution, and as you can see from this example, it can be used the same way in a Scala application.

Although this is a nice approach, it can be improved by making it a little more “Scala like.” To that end, see the next recipe, “Scala-Style Logging with Grizzled-SLF4J,” which demonstrates how to use the Grizzled-SLF4J library to improve on this approach.

See Also

For more information, see: