A Simple Logging Facade for Scala (SLF4S) built on top of SLF4J.
- Call by macro expansion (call by name in Scala 2.9);
- A Logging mixin for conveniently obtaining a Logger;
- A LoggerFactory that is
ClassTag
aware; - Built for Scala 2.9, 2.10 and 2.11;
- Tested on Java 6, 7 and 8; and
- Tracks slf4j releases.
The use of macros means that calls to log.debug
are expanded into if (log.isDebugEnabled) log.debug
at compile time.
libraryDependencies ++= Seq(
"org.slf4s" %% "slf4s-api" % "1.7.12",
"ch.qos.logback" % "logback-classic" % "1.1.2"
)
You don't have to use logback, any slf4j compatible framework will do.
The Logging
trait provides a protected val log
to your class, so it'll be available in the local scope, but won't pollute your interface. You can also reference it via a self type if you're using the cake pattern.
package org.slf4s
import org.scalatest.WordSpec
class LoggingExampleSpec extends WordSpec with Logging {
"The Logging trait should be easy to use" in {
val importantValue = 10
log.debug(s"importantValue: $importantValue")
val importantThrowable = new Throwable
log.debug(s"importantValue: $importantValue", importantThrowable)
}
}
The LoggerFactory
gives you great flexibility and behaves much like the slf4j LoggerFactory. You probably only need to use this if you wish to log as a class other than the one in scope.
package org.slf4s
import org.scalatest.WordSpec
class LoggerFactoryExampleSpec extends WordSpec {
"The LoggerFactory should be familiar" in {
val log = LoggerFactory.getLogger[LoggerFactoryExampleSpec]
val importantValue = 10
log.debug(s"importantValue: $importantValue")
val importantThrowable = new Throwable
log.debug(s"importantValue: $importantValue", importantThrowable)
}
}
Scala 2.10.0 offers a new mechanism to create strings from your data: String Interpolation. This allows you to to embed variable references and expressions directly in processed string literals.
package org.slf4s
import org.scalatest.WordSpec
class StringInterpolatorExampleSpec extends WordSpec with Logging {
"String interpolation should work" in {
val name = "James"
val height = 1.9d
log.debug(s"Hello, $name") // Hello, James
log.debug(s"1 + 1 = ${1 + 1}") // 1 + 1 = 2
log.debug(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall
log.debug(raw"a\nb") // New line is preserved
}
}
The Logger
instance exposes an underlying
field that enables you to call slf4j methods directly. This is useful if you wish to avoid a macro expansion for some reason.
For those who prefer not to use an IDE, I've made the ScalaDoc available online.