All Projects → lightbend → Scala Logging

lightbend / Scala Logging

Licence: apache-2.0
Convenient and performant logging library for Scala wrapping SLF4J.

Programming Languages

scala
5932 projects
macros
77 projects

Projects that are alternatives of or similar to Scala Logging

liquibase-slf4j
Liquibase SLF4J Logger.
Stars: ✭ 42 (-94.78%)
Mutual labels:  slf4j, logback, jvm
Kotlin Logging
Lightweight logging framework for Kotlin. A convenient and performant logging library wrapping slf4j with Kotlin extensions
Stars: ✭ 1,378 (+71.39%)
Mutual labels:  logging, jvm, slf4j
Scribe
The fastest logging library in the world. Built from scratch in Scala and programmatically configurable.
Stars: ✭ 304 (-62.19%)
Mutual labels:  logging, logback, slf4j
Tomcat Slf4j Logback
Tomcat, SLF4J and Logback integration Releases
Stars: ✭ 172 (-78.61%)
Mutual labels:  logging, logback
Lilith
Lilith is a Logging- and AccessEvent viewer for Logback, log4j, log4j2 and java.util.logging
Stars: ✭ 111 (-86.19%)
Mutual labels:  logging, logback
Logback Access Spring Boot Starter
Spring Boot Starter for Logback-access
Stars: ✭ 118 (-85.32%)
Mutual labels:  logging, logback
Testlogcollectors
A framework for capturing log statements during tests. Compatible with most popular logging frameworks. Works with JUnit and TestNG
Stars: ✭ 31 (-96.14%)
Mutual labels:  logging, slf4j
Scala Ddd Example
🎯 λ Hexagonal Architecture + DDD + CQRS applied in Scala using Akka HTTP
Stars: ✭ 86 (-89.3%)
Mutual labels:  sbt, logback
E
A zero-dependency micro library to deal with errors
Stars: ✭ 21 (-97.39%)
Mutual labels:  sbt, jvm
slack-webhook-appender
Logback appender which posts logs to slack via incoming webhook.
Stars: ✭ 16 (-98.01%)
Mutual labels:  slf4j, logback
logunit
A Java library for unit-testing logging.
Stars: ✭ 40 (-95.02%)
Mutual labels:  slf4j, logback
echopraxia
Java Logging API with clean and simple structured logging and conditional & contextual features. JSON implementations in Logback and Log4J.
Stars: ✭ 37 (-95.4%)
Mutual labels:  slf4j, logback
Zio Logging
Simple logging for ZIO apps, with correlation, context & pluggable backends out of the box.
Stars: ✭ 85 (-89.43%)
Mutual labels:  logging, slf4j
Logback Chinese Manual
logback 中文手册/文档。
Stars: ✭ 138 (-82.84%)
Mutual labels:  logging, logback
Timbre
Pure Clojure/Script logging library
Stars: ✭ 1,253 (+55.85%)
Mutual labels:  logging, slf4j
Ore
Repository software for Sponge plugins and Forge mods
Stars: ✭ 63 (-92.16%)
Mutual labels:  sbt, jvm
Terse Logback
Structured Logging, Tracing, and Observability with Logback
Stars: ✭ 146 (-81.84%)
Mutual labels:  logback, slf4j
Stubbornjava
Unconventional Java code for building web servers / services without a framework. Think dropwizard but as a seed project instead of a framework. If this project had a theme it would be break the rules but be mindful of your decisions.
Stars: ✭ 184 (-77.11%)
Mutual labels:  logback, slf4j
herald
Log annotation for logging frameworks
Stars: ✭ 71 (-91.17%)
Mutual labels:  slf4j, logback
sbt-jni
SBT Plugin to ease working with JNI
Stars: ✭ 110 (-86.32%)
Mutual labels:  sbt, jvm

scala-logging Build Status

Scala Logging is a convenient and fast 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 fast, 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!")

Prerequisites

  • Java 8 or higher
  • Scala 2.11, 2.12 or 2.13
  • 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.2.3"

If you are looking for a version compatible with Scala 2.10, check out Scala Logging 2.x.

Getting Scala Logging

Scala Logging is published to Sonatype OSS and Maven Central:

  • Group id / organization: com.typesafe.scala-logging
  • Artifact id / name: scala-logging

sbt users may add this to their build.sbt:

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"

Using Scala Logging

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 ;-)")

  logger.whenDebugEnabled {
    println("This would only execute when the debug level is enabled.")
    (1 to 10).foreach(x => println("Scala logging is great!"))
  }
}

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:

case class CorrelationId(value: String)
implicit case object CanLogCorrelationId extends CanLog[CorrelationId] {
  override def logMessage(originalMsg: String, a: CorrelationId): String = s"${a.value} $originalMsg"
}

implicit val correlationId = CorrelationId("ID")

val logger = Logger.takingImplicit[CorrelationId]("test")
logger.info("Test") // takes implicit correlationId and logs "ID Test"

It's possible to use MDC through CanLog without any troubles with execution context.

case class CorrelationId(value: String)
implicit case object CanLogCorrelationId extends CanLog[CorrelationId] {
  override def logMessage(originalMsg: String, a: CorrelationId): String = {
    MDC.put("correlationId", a.value)
    originalMsg
  }

  override def afterLog(a: CorrelationId): Unit = {
    MDC.remove("correlationId")
  }
}

implicit val correlationId = CorrelationId("ID")

val logger = Logger.takingImplicit[CorrelationId]("test")

def serviceMethod(implicit correlationId: CorrelationId): Future[Result] = {
  dbCall.map { value =>
    logger.trace(s"Received value $value from db") // takes implicit correlationId
    toResult(value)
  }
}

String Interpolation

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:

logger.error(s"my log message: $arg1 $arg2 $arg3")
logger.error("my log message: {} {} {}", arg1, arg2, arg3)

This has no effect on behavior and performace should be comparable (depends on the underlying logging library).

Limitations

  • Works only when string interpolation is directly used inside the logging statement. That is when the log message is static (available at compile time).
  • Works only for the 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).

Line numbers in log message?

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

Maintenance status

This library is community-maintained. It is not supported under the Lightbend subscription.

Contribution policy

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 Lightbend Contributor License Agreement online, using your GitHub account.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].