A Preview of Logging in ZIO 2.0
ZIO has differentiated itself from Scala's own Future by embracing pure functional programming while taking a "batteries included" approach to application development.
The framework ships with:
- Async concurrent data structures for high-performance, non-blocking applications
- Software transactional memory for deadlock-free, race-free concurrency
- Lightweight, type-safe dependency injection
- ZIO Test for deterministic testing
As ZIO 2.0 development progresses, the team addresses universal pain points developers face. Built-in logging represents one such solution coming to ZIO 2.0.
Facades versus Backends
ZIO 2.0 introduces a "logging facade" rather than a complete logging backend. This lightweight interface standardizes logging across ZIO applications without replacing established backends like LogStage, slf4j, or log4j.
For local development, ZIO 2.0 prints formatted messages to the console before you integrate your chosen backend.
A Tour of ZIO 2.0 Logging
Basic usage:
ZIO.log("Hello World!")
Control log levels across a block of statements:
ZIO.logLevel(LogLevel.DEBUG) {
ZIO.log("Hello World from a DEBUG block!")
}
Per-statement control:
ZIO.logDebug("Another debug message!")
Recording spans with automatic timing:
ZIO.logSpan("test span") {
ZIO.log("It's alive!")
}
Sample output from the above:
timestamp=2021-08-28T10:28:30.299785200Z level=INFO thread=#262 message="It's alive!" "test span"=6ms file=ZIO.scala line=677 class=zio.ZIO method=exit
ZIO 2.0 automatically measures span duration and provides file, line, and class metadata through its execution tracing support.
Fully Fiber-Aware
A distinguishing feature of ZIO 2.0 logging is that it tracks at the fiber level rather than physical threads. The system gives backends access to fiber-specific data such as correlation IDs, telemetry information, or structured event data — making it straightforward to correlate log output across concurrent fibers.
ZIO 2.0 logging aims to create standardization across the entire ZIO ecosystem, enabling unified configuration and consolidated log output across all libraries and applications that build on ZIO. This feature debuted in ZIO 2.0 M3.