Logging in Scala

George picture George · Jun 10, 2009 · Viewed 104.4k times · Source

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.

Answer

Eugene Yokota picture Eugene Yokota · Jun 10, 2009

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.