Member-only story
Oh no, not another post about logging in Scala!
1 min readFeb 3, 2022
Oh yes it is. A simple solution to some common logging boiler plate in Scala.
I’ll keep this post short, it was inspired by the article “Clear and concise logging in Scala”. This post provides an example of minimizing the common boiler from logging information about a return value from a function.
The problem
Our starting point is a simple function.
def someFunction(): Int = 3
When we want to log something about the return value, the common idiom is to add the boilerplate below:
def someFunction(): Int
val i = 3
log.log(Level.DEBUG, s"The value is $i")
i
A simple solution
With an extension, we can simplify the boiler plate to:
def someFunction(): Int = 3.logDebug(i => s"The value is $i")
The extension
Another alternative
Scala has tap
method that can be added to any object.
import scala.util.chaining.*def someFunction(): Int =
3.tap(i => log.log(Level.Debug, i => s"The value is $i"))