Skip to main content
Version: 2.x

Getting Started with ZIO Logging

This page shows how to add ZIO Logging to a project, which extra module to pick for your setup, and how to run a first program that writes its log lines through a ZIO Logging logger.

Installationโ€‹

Add the core module to your build.sbt:

// ZIO Logging core module (JVM)
libraryDependencies += "dev.zio" %% "zio-logging" % "2.5.5"

The core module is also published for Scala.js and Scala Native. In a Scala.js or Scala Native project, or in a cross-built project, use %%% instead of %%:

// ZIO Logging core module (JVM, Scala.js or Scala Native)
libraryDependencies += "dev.zio" %%% "zio-logging" % "2.5.5"

The core module holds the console loggers, the file loggers, the reconfigurable logger, log formats, log filters and log metrics. Many applications need nothing else. See Platforms for what works on JVM, Scala.js and Scala Native.

Choosing Extra Modulesโ€‹

The other modules connect ZIO Logging to Java logging libraries. All of them are JVM only. Pick a module by the direction the log lines travel:

  • A backend sends the log lines of your ZIO code to a Java logging library, which then formats and writes them.
  • A bridge does the opposite: it takes the log lines that Java libraries write and sends them to your ZIO Logging loggers.
ModuleKindUse it whenPage
zio-logging-slf4j2backendyour project already writes logs through SLF4J 2 (for example with Logback 1.3+ or Log4j 2), and ZIO logs should go there tooSLF4J v2
zio-logging-slf4jbackendthe same, with an SLF4J 1.x setupSLF4J v1
zio-logging-jplbackendyour project writes logs through the Java Platform/System Logger (System.Logger)Java Platform/System Logger
zio-logging-slf4j2-bridgebridgea library you depend on logs through SLF4J 2, and its lines should reach your ZIO loggersSLF4J v2 bridge
zio-logging-slf4j-bridgebridgethe same, for a library that logs through SLF4J 1.xSLF4J v1 bridge
zio-logging-jul-bridgebridgea library you depend on logs through java.util.logging (JUL), and its lines should reach your ZIO loggersjava.util.logging bridge

Add the line for each module you need:

// Backends: send ZIO log lines to a Java logging library
libraryDependencies += "dev.zio" %% "zio-logging-slf4j2" % "2.5.5" // SLF4J v2
libraryDependencies += "dev.zio" %% "zio-logging-slf4j" % "2.5.5" // SLF4J v1
libraryDependencies += "dev.zio" %% "zio-logging-jpl" % "2.5.5" // Java Platform/System Logger

// Bridges: send Java library log lines to ZIO Logging
libraryDependencies += "dev.zio" %% "zio-logging-slf4j2-bridge" % "2.5.5" // SLF4J v2
libraryDependencies += "dev.zio" %% "zio-logging-slf4j-bridge" % "2.5.5" // SLF4J v1
libraryDependencies += "dev.zio" %% "zio-logging-jul-bridge" % "2.5.5" // java.util.logging
caution

Do not combine a backend and a bridge for the same logging system in one application. For example, the SLF4J backend sends ZIO log lines to SLF4J, and the SLF4J bridge sends SLF4J log lines back to ZIO, so each line goes round in a loop. Also pick either the v1 or the v2 variant of a module, never both. How They Work Together lists the combinations.

Your First Programโ€‹

ZIO already logs through ZIO.logInfo, ZIO.logError and the other ZIO.log* methods. ZIO Logging does not change those calls. It provides loggers that decide how the log lines look and where they go.

Install the logger in the application's bootstrap layer. The logger then serves the whole application runtime, so the failure at the end of the program below is logged with it too:

import zio.logging.consoleLogger
import zio.{ ExitCode, Runtime, Scope, ZIO, ZIOAppArgs, ZIOAppDefault, ZLayer }

object SimpleApp extends ZIOAppDefault {

override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] =
Runtime.removeDefaultLoggers >>> consoleLogger()

override def run: ZIO[Scope, Any, ExitCode] =
for {
_ <- ZIO.logInfo("Start")
_ <- ZIO.fail("FAILURE")
_ <- ZIO.logInfo("Done")
} yield ExitCode.success

}

The bootstrap layer does two things:

  1. Runtime.removeDefaultLoggers removes ZIO's built-in logger. Without this step, every line is printed twice: once by ZIO's default logger and once by the console logger.
  2. consoleLogger() adds the ZIO Logging console logger. With no arguments, it reads its settings from the logger path of the application's configuration. When nothing is configured there, it uses the default format and logs at level Info and above. Configuration lists every setting.

Running the program prints lines like these:

timestamp=2023-03-15T08:36:24.421098+01:00 level=INFO thread=zio-fiber-4 message="Start"
timestamp=2023-03-15T08:36:24.440181+01:00 level=ERROR thread=zio-fiber-0 message="" cause=Exception in thread "zio-fiber-4" java.lang.String: FAILURE
at zio.logging.example.SimpleApp.run(SimpleApp.scala:29)

"Done" never appears, because ZIO.fail ends the program before it. The ERROR line comes from the ZIO runtime, which logs the failure through the same console logger.

Next Stepsโ€‹

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!