181

What is a good way to do logging in a Scala application? Something that is consistent with the language philosophy, does not clutter the code, and is low-maintenance and unobtrusive. Here's a basic requirement list:

  • simple
  • does not clutter the code. Scala is great for its brevity. I don't want half of my code to be logging statements
  • log format can be changed to fit the rest of my enterprise logs and monitoring software
  • supports levels of logging (ie debug, trace, error)
  • can log to disk as well as other destinations (i.e. socket, console, etc.)
  • minimum configuration, if any
  • works in containers (ie, web server)
  • (optional, but nice to have) comes either as part of the language or as a maven artifact, so I don't have to hack my builds to use it

I know I can use the existing Java logging solutions, but they fail on at least two of the above, namely clutter and configuration.

Thanks for your replies.

14 Answers 14

122

slf4j wrappers

Most of Scala's logging libraries have been some wrappers around a Java logging framework (slf4j, log4j etc), but as of March 2015, the surviving log libraries are all slf4j. These log libraries provide some sort of log object to which you can call info(...), debug(...), etc. I'm not a big fan of slf4j, but it now seems to be the predominant logging framework. Here's the description of SLF4J:

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

The ability to change underlying log library at deployment time brings in unique characteristic to the entire slf4j family of loggers, which you need to be aware of:

  1. classpath as configuration approach. The way slf4j knows which underlying logging library you are using is by loading a class by some name. I've had issues in which slf4j not recognizing my logger when classloader was customized.
  2. Because the simple facade tries to be the common denominator, it's limited only to actual log calls. In other words, the configuration cannot be done via the code.

In a large project, it could actually be convenient to be able to control the logging behavior of transitive dependencies if everyone used slf4j.

Scala Logging

Scala Logging is written by Heiko Seeberger as a successor to his slf4s. It uses macro to expand calls into if expression to avoid potentially expensive log call.

Scala Logging is a convenient and performant logging library wrapping logging libraries like SLF4J and potentially others.

Historical loggers

  • Logula, a Log4J wrapper written by Coda Hale. Used to like this one, but now it's abandoned.
  • configgy, a java.util.logging wrapper that used to be popular in the earlier days of Scala. Now abandoned.
4
  • 1
    Gotta say, I love Logula...finally a logger that doesn't make me want to stab screwdriver up my ear. No xml and no BS, just a scala config on startup
    – Arg
    Feb 22, 2012 at 19:35
  • Heiko Seeberger's SLF4S no longer appears to be maintained. I made my own targeting Scala 2.10 and the latest SLF4J. http://slf4s.org/ Feb 26, 2014 at 21:42
  • 4
    Logula is abandoned according to its README on Github. Mar 30, 2014 at 20:16
  • Oncue Journal is similar in concept to Scala Logging, but the log messages are sent to an actor which calls SLF4J from a dedicated thread pool. Trades CPU time for (much better) latency. github.com/oncue/journal
    – Gary Coady
    Jul 24, 2015 at 12:03
65

With Scala 2.10+ Consider ScalaLogging by Typesafe. Uses macros to deliver a very clean API

https://github.com/typesafehub/scala-logging

Quoting from their wiki:

Fortunately Scala macros can be used to make our lives easier: ScalaLogging offers the class Logger with lightweight logging methods that will be expanded to the above idiom. So all we have to write is:

logger.debug(s"Some ${expensiveExpression} message!")

After the macro has been applied, the code will have been transformed into the above described idiom.

In addition ScalaLogging offers the trait Logging which conveniently provides a Logger instance initialized with the name of the class mixed into:

import com.typesafe.scalalogging.slf4j.LazyLogging

class MyClass extends LazyLogging {
  logger.debug("This is very convenient ;-)")
}
6
  • 12
    Use class MyClass with Logging. Apr 10, 2013 at 13:17
  • 1
    Please note that using logging in traits causes the trait to have the same name for the logger as the class it is mixed into. I think you can get the logger manually instead of the macro convenience to provide the logger name manually, if needed. But please, correct me if I'm wrong.
    – mkko
    Aug 15, 2013 at 12:26
  • 1
    Does this mean MyClass has public methods for debug, info warn etc? If so I'd rather do the extra manual work of instantiating a logger private to the class. Logging methods should not leak into a classes interface.
    – ChucK
    Sep 25, 2013 at 7:50
  • 2
    you get a logger field, which you can use at your leisure. See github.com/typesafehub/scalalogging/blob/master/… for details
    – fracca
    Sep 25, 2013 at 13:36
  • 2
    2.x requires Scala 2.11; hopefully there's not much practical difference; for Scala 2.10.x, there's "com.typesafe" %% "scalalogging-slf4j" % "1.1.0". Mar 30, 2014 at 20:24
14

Using slf4j and a wrapper is nice but the use of it's built in interpolation breaks down when you have more than two values to interpolate, since then you need to create an Array of values to interpolate.

A more Scala like solution is to use a thunk or cluster to delay the concatenation of the error message. A good example of this is Lift's logger

Log.scala Slf4jLog.scala

Which looks like this:

class Log4JLogger(val logger: Logger) extends LiftLogger {
  override def trace(msg: => AnyRef) = if (isTraceEnabled) logger.trace(msg)
}

Note that msg is a call-by-name and won't be evaluated unless isTraceEnabled is true so there's no cost in generating a nice message string. This works around the slf4j's interpolation mechanism which requires parsing the error message. With this model, you can interpolate any number of values into the error message.

If you have a separate trait that mixes this Log4JLogger into your class, then you can do

trace("The foobar from " + a + " doesn't match the foobar from " +
      b + " and you should reset the baz from " + c")

instead of

info("The foobar from {0} doesn't match the foobar from {1} and you should reset the baz from {c},
     Array(a, b, c))
1
  • 3
    is it possible to get accurate line numbers for logging statements in scala classes?????
    – jpswain
    Jan 29, 2011 at 7:58
13

Don't use Logula

I've actually followed the recommendation of Eugene and tried it and found out that it has a clumsy configuration and is subjected to bugs, which don't get fixed (such as this one). It doesn't look to be well maintained and it doesn't support Scala 2.10.

Use slf4s + slf4j-simple

Key benefits:

  • Supports latest Scala 2.10 (to date it's M7)
  • Configuration is versatile but couldn't be simpler. It's done with system properties, which you can set either by appending something like -Dorg.slf4j.simplelogger.defaultlog=trace to execution command or hardcode in your script: System.setProperty("org.slf4j.simplelogger.defaultlog", "trace"). No need to manage trashy config files!
  • Fits nicely with IDEs. For instance to set the logging level to "trace" in a specific run configuration in IDEA just go to Run/Debug Configurations and add -Dorg.slf4j.simplelogger.defaultlog=trace to VM options.
  • Easy setup: just drop in the dependencies from the bottom of this answer

Here's what you need to be running it with Maven:

<dependency>
  <groupId>com.weiglewilczek.slf4s</groupId>
  <artifactId>slf4s_2.9.1</artifactId>
  <version>1.0.7</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.6.6</version>
</dependency>
4
  • where do you find slf4s that supports 2.10? I'm looking at github.com/weiglewilczek/slf4s and see "Supported Scala versions are 2.8.0, 2.8.1, 2.9.0-1 and 2.9.1." .. am I looking in the wrong place?
    – nairbv
    Sep 10, 2012 at 1:43
  • @Brian Use the 2.9.1 as suggested in the answer. I've tested it to be working fine with 2.10.0-M7 Sep 10, 2012 at 3:54
  • I'm using 2.9.1, I was just curious about the highlighted "key benefit" and what that means.
    – nairbv
    Sep 10, 2012 at 4:22
  • After carefully searching through the answers for the pros and cons of the logging libraries, I choose the one that tells me what dependency to import. Thanks, Sir.
    – teo
    Dec 31, 2012 at 16:02
13

This is how I got Scala Logging working for me:

Put this in your build.sbt:

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.7.2",
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"

Then, after doing an sbt update, this prints out a friendly log message:

import com.typesafe.scalalogging._
object MyApp extends App with LazyLogging {
  logger.info("Hello there")
}

If you are using Play, you can of course simply import play.api.Logger for writing log messages: Logger.debug("Hi").

See the docs for more info.

1
  • Only worked for me after I created log4j.properties inside project resource folder. May 3, 2019 at 11:02
11

Logging in 2020

I was really surprised that Scribe logging framework that I use at work isn't even mentioned here. What is more, it doesn't even appear on the first page in Google after searching "scala logging". But this page appears when googling it! So let me leave that here.

Main advantages of Scribe:

7
  • This looks really nice! It's unclear if it's possible to change the logging level via configuration though?
    – Nick
    Sep 18, 2020 at 22:09
  • You can use scribe.Logger.root.clearHandlers().clearModifiers().withHandler(minimumLevel = Some(Level.Error)).replace() as mentioned in github.com/outr/scribe/wiki/getting-started for setting it globaly. If you want to use configuration file you have to use a library like pureconfig or just use program arguments in your custom configuration class. I just put global config setting line mentioned before in my custom trait that extends App trait and it works fine.
    – Ava
    Sep 19, 2020 at 7:51
  • You an also configure it per class but I have never needed it because I work with microservices.
    – Ava
    Sep 19, 2020 at 8:08
  • I don't mean in code, I mean using a file. E.g. If I want to debug something, I am not going to recompile my code to turn debugging on
    – Nick
    Sep 19, 2020 at 8:46
  • 1
    You will have to implement something on your own. Scribe just gives us tools to implement our own mechanisms for configuration, not the mechanisms themselves.
    – Ava
    Sep 19, 2020 at 9:26
8

I use SLF4J + Logback classic and apply it like this:

trait Logging {
  lazy val logger = LoggerFactory.getLogger(getClass)

  implicit def logging2Logger(anything: Logging): Logger = anything.logger
}

Then you can use it whichever fits your style better:

class X with Logging {
    logger.debug("foo")
    debug("bar")
}

but this approach of course uses a logger instance per class instance.

7

I pulled a bit of work form the Logging trait of scalax, and created a trait that also integrated a MessageFormat-based library.

Then stuff kind of looks like this:

class Foo extends Loggable {
    info( "Dude, I'm an {0} with {1,number,#}", "Log message", 1234 )
}

We like the approach so far.

Implementation:

trait Loggable {

    val logger:Logger = Logging.getLogger(this)

    def checkFormat(msg:String, refs:Seq[Any]):String =
        if (refs.size > 0) msgfmtSeq(msg, refs) else msg 

    def trace(msg:String, refs:Any*) = logger trace checkFormat(msg, refs)

    def trace(t:Throwable, msg:String, refs:Any*) = logger trace (checkFormat(msg, refs), t)

    def info(msg:String, refs:Any*) = logger info checkFormat(msg, refs)

    def info(t:Throwable, msg:String, refs:Any*) = logger info (checkFormat(msg, refs), t)

    def warn(msg:String, refs:Any*) = logger warn checkFormat(msg, refs)

    def warn(t:Throwable, msg:String, refs:Any*) = logger warn (checkFormat(msg, refs), t)

    def critical(msg:String, refs:Any*) = logger error checkFormat(msg, refs)

    def critical(t:Throwable, msg:String, refs:Any*) = logger error (checkFormat(msg, refs), t)

}

/**
 * Note: implementation taken from scalax.logging API
 */
object Logging {  

    def loggerNameForClass(className: String) = {  
        if (className endsWith "$") className.substring(0, className.length - 1)  
        else className  
    }  

    def getLogger(logging: AnyRef) = LoggerFactory.getLogger(loggerNameForClass(logging.getClass.getName))  
}
1
  • Cute, though I'm having some issues getting this to work -- specifically, the output (on default slf4j configuration) always looks something like the following, instead of the class actually extending Loggable: Jul 22, 2011 3:02:17 AM redacted.util.Loggable$class info INFO: Some message... Any chance you've ran into this? Jul 22, 2011 at 0:09
5

Writer, Monoid and a Monad implementation.

3
4

You should have a look at the scalax library : http://scalax.scalaforge.org/ In this library, there is a Logging trait, using sl4j as backend. By using this trait, you can log quite easily (just use the logger field in the class inheriting the trait).

3

Haven't tried it yet, but Configgy looks promising for both configuration and logging:

http://github.com/robey/configgy/tree/master

3

Quick and easy forms.

Scala 2.10 and older:

import com.typesafe.scalalogging.slf4j.Logger
import org.slf4j.LoggerFactory
val logger = Logger(LoggerFactory.getLogger("TheLoggerName"))
logger.debug("Useful message....")

And build.sbt:

libraryDependencies += "com.typesafe" %% "scalalogging-slf4j" % "1.1.0"

Scala 2.11+ and newer:

import import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
val logger = Logger(LoggerFactory.getLogger("TheLoggerName"))
logger.debug("Useful message....")

And build.sbt:

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
3
2

After using slf4s and logula for a while, I wrote loglady, a simple logging trait wrapping slf4j.

It offers an API similar to that of Python's logging library, which makes the common cases (basic string, simple formatting) trivial and avoids formatting boilerplate.

http://github.com/dln/loglady/

2

I find very convenient using some kind of java logger, sl4j for example, with simple scala wrapper, which brings me such syntax

val #! = new Logger(..) // somewhere deep in dsl.logging.

object User with dsl.logging {

  #! ! "info message"
  #! dbg "debug message"
  #! trace "var a=true"

}

In my opinion very usefull mixin of java proven logging frameworks and scala's fancy syntax.

1
  • @sschaef, oh, yes. Sorry. I've almost forget. It was a great fight with this issue for a few days, but looks like this is really impossible. I've used #! ! "info message" instead. I'll edit edit my answer.
    – Alex Povar
    Jun 23, 2013 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.