Functional Logging in Scala with Odin

A focused Odin and Cats Effect example covering composed logging effects, constant context, and safe file-logger resource management.

Functional logging in Scala makes log statements part of an effectful program rather than hidden work during expression evaluation. Odin returns values such as IO[Unit], which you compose with application effects. This focused example uses Scala 2.13, Odin 0.13.0, and Cats Effect 3.3.4.

Keep logging inside the effect

A call such as logger.info(...) describes a logging effect. It must be composed into the program to run. File loggers return Resource, so allocation, flushing, and release belong inside use.

A complete console and file example

Add these pinned dependencies to an existing Scala 2.13 sbt project. They describe the example version, not a claim that these are the newest releases.

libraryDependencies ++= Seq(
  "com.github.valskalla" %% "odin-core" % "0.13.0",
  "org.typelevel" %% "cats-effect" % "3.3.4"
)
import cats.effect.{IO, IOApp}
import cats.syntax.all._
import io.odin._
import io.odin.syntax._

object OdinExample extends IOApp.Simple {
  private val logger = consoleLogger[IO](minLevel = Level.Info)
    .withConstContext(Map("service" -> "orders"))

  def run: IO[Unit] =
    logger.info("Request accepted") *>
      fileLogger[IO]("orders.log").use { file =>
        file.info("Request recorded")
      }
}

The console logger has constant service context. The file logger is acquired and released with use. In this version, the basic file logger can truncate an existing file by default; configure open options or a rolling backend deliberately before using it for retained logs.

Boundaries that matter in a service

Log the fields needed to explain an operation and its failure. Keep tokens, passwords, and personal data out of message text and context. Do not assume that adding a context wrapper redacts arbitrary secrets.

Asynchronous logging introduces buffering and release behavior that should be measured under overload. Use the versioned API and examples in the Odin repository when adding rolling files, asynchronous backends, or context transformations.

For deciding what to keep in an application, see negative space programming and the trade-offs in maintainable code.

Share
Edit Template
Newsletter

Get new engineering deep dives

Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.

Join the newsletter