No Dependency Fluent Logging in Scala

Brian Schlining
2 min readMay 2, 2022
Photo by Ales Krivec (https://unsplash.com/photos/KnV-mJDGWzU)

Introduction to System.Logger

Since the release Java Development Kit (JDK) 9, Java has shipped with a minimal logging API that can be used to route log messages to your logging framework of choice, such as slf4j, log4j2, jul, etc. The advantage of using System.Logger is that your application is completely decoupled from 3rd party logging frameworks. This allows you to easily switch logging frameworks with out having to change your code in case something like log4shell happens again.

Using Java’s built-in System.Logger, you can instantiate a logger using some variant of :

# Look ma, no imports needed!!
val log = System.getLogger(getClass.getName)

One downside of using the System.Logger is that the API feels cumbersome in Scala. Here’s some examples:

import System.Logger.Level.*# A simple log message 
log.log(ERROR, "A log message")
# Lazy log message
log.log(TRACE, () => "build expensive log message")
# Lazy log message with exception
log.log(WARNING,
() => "build expensive log message",
exception)

Fluent Logging APIs

Recent logging libraries have adopted “fluent” interfaces. Fluent APIs have several advantages over…

--

--

Brian Schlining

Polyglot coder. Deep-sea Researcher. Zazen aficionado. I think squids are pretty cool.