Skip to main content
Version: 2.x

Introduction to ZIO Logging

ZIO Logging is simple logging for ZIO apps, with correlation, context, and pluggable backends out of the box with integrations for common logging backends.

Production Ready CI Badge Sonatype Releases Sonatype Snapshots javadoc zio-logging

Introductionโ€‹

When we are writing our applications using ZIO effects, to log easy way we need a ZIO native solution for logging.

Key features of ZIO Logging:

  • ZIO Native โ€” Other than it is a type-safe and purely functional solution, it leverages ZIO's features.
  • Multi-Platform - The core module supports JVM, JS and Native. File loggers are JVM and Native only, and all other modules are JVM only. See Platforms.
  • Composable โ€” Loggers are composable together via contraMap.
  • Pluggable Backends โ€” Support multiple backends like ZIO Console, SLF4j, JPL.
  • Logger Context โ€” It has a first citizen Logger Context implemented on top of FiberRef. The Logger Context maintains information like logger name, filters, correlation id, and so forth across different fibers. It supports Mapped Diagnostic Context (MDC) which manages contextual information across fibers in a concurrent environment.
  • Richly integrated into ZIO 2's built-in logging facilities
  • ZIO Console, SLF4j, and other backends

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"

Getting Started explains the core module and when to add the modules below.

Modulesโ€‹

These modules are optional. Each one connects ZIO Logging to a Java logging library. Add only the ones you need.

Backendsโ€‹

A backend sends the log lines of your ZIO code to a Java logging library, which formats and writes them. All backend modules are JVM only.

// SLF4J 2 backend (JVM)
libraryDependencies += "dev.zio" %% "zio-logging-slf4j2" % "2.5.5"

// SLF4J 1.x backend (JVM)
libraryDependencies += "dev.zio" %% "zio-logging-slf4j" % "2.5.5"

// Java Platform/System Logger backend (JVM)
libraryDependencies += "dev.zio" %% "zio-logging-jpl" % "2.5.5"

SLF4J v2, SLF4J v1, and Java Platform/System Logger describe these modules.

Bridgesโ€‹

A bridge does the opposite: it sends the log lines that Java libraries write to your ZIO Logging loggers. All bridge modules are JVM only.

// SLF4J 2 bridge (JVM)
libraryDependencies += "dev.zio" %% "zio-logging-slf4j2-bridge" % "2.5.5"

// SLF4J 1.x bridge (JVM)
libraryDependencies += "dev.zio" %% "zio-logging-slf4j-bridge" % "2.5.5"

// java.util.logging bridge (JVM)
libraryDependencies += "dev.zio" %% "zio-logging-jul-bridge" % "2.5.5"

SLF4J v2 bridge, SLF4J v1 bridge, and java.util.logging bridge describe these modules. Do not combine a backend and a bridge for the same logging system in one application. See How They Work Together.

Platformsโ€‹

ZIO Logging is published for JVM, JS and Native. Which parts work on each platform depends on where the code lives in the source tree:

FeatureJVMJSNativeWhy
Console loggers (consoleLogger, consoleJsonLogger, consoleErrLogger, consoleErrJsonLogger)yesyesyesshared code, writes to System.out / System.err
File loggers (fileLogger, fileJsonLogger, fileAsyncLogger, fileAsyncJsonLogger)yesnoyesJVM and Native code only
Log metrics (logMetrics), reconfigurable loggeryesyesyesshared code
LogFormat, LogFilter, LogAnnotationyesyesyesshared code
SLF4J, SLF4J bridges, java.util.logging bridge, JPLyesnonoseparate JVM-only modules

Exampleโ€‹

Let's try an example of ZIO Logging which demonstrates a simple application of ZIO logging.

The recommended place for setting the logger is the application bootstrap. The logger is then set for the whole application runtime, so application failures are also logged with it. The following application installs the console logger in its bootstrap:

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

}

Expected console output:

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)

Getting Started explains this program step by step. You can find the source code of more examples here.

ZIO Chat

Hi there! ๐Ÿ‘‹ I'm ZIO Chat, your friendly neighborhood ZIO expert. I'm here to help you navigate the world of ZIO. Whether you're a seasoned pro or just starting out, fire away with your ZIO questions. I will hit you back with the best insights I've got. Let's dive into the world of ZIO together!