Scala Logging is a convenient and performant logging library wrapping SLF4J.
It's convenient, because you can simply call log methods, without checking whether the respective log level is enabled:
logger.debug(s"Some $expensive message!")
It's performant, because thanks to Scala macros the check-enabled-idiom is applied and the following code is generated:
if (logger.isDebugEnabled) logger.debug(s"Some $expensive message!")
- Java 6 or higher
- Scala 2.11 or 2.12
- Logging backend compatible with SLF4J
A compatible logging backend is Logback, add it to your sbt build definition:
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.7"
If you are looking for a version compatible with Scala 2.10, check out Scala Logging 2.x.
Scala Logging is published to Sonatype OSS and Maven Central:
- Group id / organization: com.typesafe.scala-logging
- Artifact id / name: scala-logging
- Latest version is 3.5.0
Usage with SBT, adding a dependency to the latest version of Scala Logging to your sbt build definition file:
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.5.0"
The Logger
class from the com.typesafe.scalalogging
package wraps an underlying SLF4J logger.
In order to create a Logger
, you pass a name to the apply
factory method defined in the Logger
companion object:
val logger = Logger("name")
Or, you pass in a SLF4J logger instance:
val logger = Logger(LoggerFactory.getLogger("name"))
Or, you pass in a class:
val logger = Logger(classOf[MyClass])
Or, using the runtime class wrapped by the implicit class tag parameter:
val logger = Logger[MyClass]
The LazyLogging
and StrictLogging
traits from the com.typesafe.scalalogging
package define the logger
member as
a lazy or strict value respectively. In both cases the underlying SLF4J logger is named according to the class into which
these traits are mixed:
class MyClass extends LazyLogging {
logger.debug("This is very convenient ;-)")
}
- More Logger factory methods, bugfixes and upgrades, published for Scala 2.12.0-M5, 2.12.0-RC1, 2.12.0-RC2 and 2.12.0.
- Fixes #38 - Logger.info() cannot be used with primitive types.
- Fixes #42 - Request: Add Logger(class). README changes.
- SLF4J loggers and our Logger now survive serialization. By survive serialization, we mean that the deserialized logger instances are fully functional.
Using the sourcecode library, it's possible to add line number information (especially useful for debugging):
def foo(arg: String)(implicit line: sourcecode.Line, file: sourcecode.File) = {
... do something with arg ...
... do something with file.value ...
}
foo("hello") // the implicit sourcecode.File is filled in automatically
Check out scala-trace-debug to make multithreaded bug tracing and prevention easier than ever. Provides user-friendly prints, traces, assertions, container printing, and source code printing.
Check out logstash-logback-encoder if you're using Logstash. Provides logback encoders, layouts, and appenders to log in JSON format.
The original author Heiko Seeberger stepped down Q1 2015, starting a new adventure at codecentric. Future maintenance is taken over by Mathias Bogaert.
Contributions via GitHub pull requests are gladly accepted from their original author. Before we can accept pull requests, you will need to agree to the Typesafe Contributor License Agreement online, using your GitHub account.
This code is open source software licensed under the Apache 2.0 License.