Name: scala-logging
Owner: Lightbend (Typesafe archive)
Owner: Lightbend
Description: Convenient and performant logging library for Scala wrapping SLF4J.
Created: 2013-07-16 20:24:15.0
Updated: 2018-01-17 13:10:28.0
Pushed: 2018-01-13 00:24:40.0
Size: 125
Language: Scala
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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:
er.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:
logger.isDebugEnabled) logger.debug(s"Some $expensive message!")
A compatible logging backend is Logback, add it to your sbt build definition:
aryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
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:
Usage with SBT, adding a dependency to the latest version of Scala Logging to your sbt build definition file:
aryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.7.2"
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:
logger = Logger("name")
Or, you pass in a SLF4J logger instance:
logger = Logger(LoggerFactory.getLogger("name"))
Or, you pass in a class:
logger = Logger(classOf[MyClass])
Or, using the runtime class wrapped by the implicit class tag parameter:
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:
s MyClass extends LazyLogging {
gger.debug("This is very convenient ;-)")
LoggerTakingImplicit
provides the same methods as Logger
class, but with additional implicit parameter A
.
During creation of the LoggerTakingImplicit
evidence CanLog[A]
is required.
It may be useful when contextual parameter (e.g. Correlation ID) is being passed around and you would like to include it in the log messages:
class CorrelationId(value: String)
icit case object CanLogCorrelationId extends CanLog[CorrelationId] {
erride def logMessage(originalMsg: String, a: CorrelationId): String = s"${a.value} $originalMsg"
icit val correlationId = CorrelationId("ID")
logger = Logger.takingImplicit[CorrelationId]("test")
er.info("Test") // takes implicit correlationId and logs "ID Test"
It's possible to use MDC
through CanLog
without any troubles with execution context.
class CorrelationId(value: String)
icit case object CanLogCorrelationId extends CanLog[CorrelationId] {
erride def logMessage(originalMsg: String, a: CorrelationId): String = {
MDC.put("correlationId", a.value)
originalMsg
erride def afterLog(a: A): Unit = {
MDC.remove("correlationId")
icit val correlationId = CorrelationId("ID")
logger = Logger.takingImplicit[CorrelationId]("test")
serviceMethod(implicit correlationId: CorrelationId): Future[Result] = {
Call.map { value =>
logger.trace(s"Received value $value from db") // takes implicit correlationId
toResult(value)
Any
with slf4 interpolator.It is idiomatic to use Scala's string interpolation logger.error(s"log $value")
instead of SLF4J string interpolation logger.error("log {}", value)
.
However there are some tools (such as Sentry) that use the log message format as grouping key. Therefore they do not work well with
Scala's string interpolation.
Scala Logging replaces simple string interpolations with their SLF4J counterparts like this:
er.error(s"my log message: $arg1 $arg2 $arg3")
cala
er.error("my log message: {} {} {}", arg1, arg2, arg3)
This has no effect on behavior and performace should be comparable (depends on the underlying logging library).
logger.<level>(message)
and logger.<level>(marker, message)
logging methods. It does not work if you want to log an exception and
use string interpolation too (this is a limitation of the SLF4J API).Using the sourcecode library, it's possible to add line number information (especially useful for debugging):
foo(arg: String)(implicit line: sourcecode.Line, file: sourcecode.File) = {
. do something with arg ...
. do something with file.value ...
"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.