LogStage is a zero-cost structural logging framework for Scala & Scala.js

Key features:

  1. LogStage extracts structure from ordinary string interpolations in your log messages with zero changes to code.
  2. LogStage uses macros to extract log structure, its faster at runtime than a typical reflective structural logging frameworks,
  3. Log contexts
  4. Console, File and SLF4J sinks included, File sink supports log rotation,
  5. Human-readable output and JSON output included,
  6. Method-level logging granularity. Can configure methods com.example.Service.start and com.example.Service.doSomething independently,
  7. Slf4J adapters: route legacy Slf4J logs into LogStage router


libraryDependencies ++= Seq(
  // LogStage core library
  "io.7mind.izumi" %% "logstage-core" % "0.10.17-SNAPSHOT",
  // Json output
  "io.7mind.izumi" %% "logstage-rendering-circe" % "0.10.17-SNAPSHOT",
  // Router from Slf4j to LogStage
  "io.7mind.izumi" %% "logstage-adapter-slf4j" % "0.10.17-SNAPSHOT",
  // LogStage integration with DIStage
  "io.7mind.izumi" %% "distage-extension-logstage" % "0.10.17-SNAPSHOT",
  // Router from LogStage to Slf4J
  "io.7mind.izumi" %% "logstage-sink-slf4j " % "0.10.17-SNAPSHOT",


The following snippet:

import logstage.IzLogger
import scala.util.Random

val logger = IzLogger()
// logger: IzLogger = izumi.logstage.api.IzLogger@46126c5d

val justAnArg = "example"
// justAnArg: String = example
val justAList = List[Any](10, "green", "bottles")
// justAList: List[Any] = List(10, green, bottles)

logger.trace(s"Argument: $justAnArg, another arg: $justAList")

// custom name, not based on `val` name

logger.info(s"Named expression: ${Random.nextInt() -> "random number"}")

// print result without a name

logger.warn(s"Invisible argument: ${Random.nextInt() -> "random number" -> null}")

// add following fields to all messages printed by a new logger value

val ctxLogger = logger("userId" -> "user@google.com", "company" -> "acme")
// ctxLogger: IzLogger = izumi.logstage.api.IzLogger@7eb45342
val delta = Random.nextInt(1000)
// delta: Int = 645

ctxLogger.info(s"Processing time: $delta")

Will look like this in string form:


And like this in JSON:



  1. JSON formatter is type aware!
  2. Each JSON message contains @class field with holds a unique event class identifier. All events produced by the same source code line will share the same event class.

Syntax Reference

  1. Simple variable: scala logger.info(s"My message: $argument")

  2. Chain: scala logger.info(s"My message: ${call.method} ${access.value}")

  3. Named expression: scala logger.info(s"My message: ${Some.expression -> "argname"}")

  4. Invisible named expression: scala logger.info(s"My message: ${Some.expression -> "argname" -> null}")

5) De-camelcased name: scala logger.info(${camelCaseName-> ' '})

Basic setup

import logstage.{ConsoleSink, IzLogger, Trace}
import logstage.circe.LogstageCirceRenderingPolicy

val textSink = ConsoleSink.text(colored = true)
// textSink: ConsoleSink = izumi.logstage.sink.ConsoleSink$ColoredConsoleSink$@1e7b486c
val jsonSink = ConsoleSink(LogstageCirceRenderingPolicy(prettyPrint = true))
// jsonSink: ConsoleSink = izumi.logstage.sink.ConsoleSink@31931c68

val sinks = List(jsonSink, textSink)
// sinks: List[ConsoleSink] = List(izumi.logstage.sink.ConsoleSink@31931c68, izumi.logstage.sink.ConsoleSink$ColoredConsoleSink$@1e7b486c)

val logger: IzLogger = IzLogger(Trace, sinks)
// logger: IzLogger = izumi.logstage.api.IzLogger@6436fe83
val contextLogger: IzLogger = logger("key" -> "value")
// contextLogger: IzLogger = izumi.logstage.api.IzLogger@38449f04



Log algebras

LogIO, LogBIO & LogBIO3 algebras provide a purely-functional API for one-, two-, and three-parameter effect types respectively:

import logstage.{IzLogger, LogIO}
import cats.effect.IO

val logger = IzLogger()
// logger: IzLogger = izumi.logstage.api.IzLogger@7c388848

val log = LogIO.fromLogger[IO](logger)
// log: LogIO[IO] = logstage.LogIO$$anon$1@5561abfb

log.info(s"Hey! I'm logging with ${log}stage!")
// res9: IO[Unit] = IO$108037426
I 2019-03-29T23:21:48.693Z[Europe/Dublin] r.S.App7.res8 ...main-12:5384  (00_logstage.md:92) Hey! I'm logging with log=logstage.LogIO$$anon$1@72736f25stage!

LogIO/LogBIO algebras can be extended with custom context using their .apply method, same as IzLogger:

import cats.effect.IO
import cats.implicits._
import logstage.LogIO
import io.circe.Printer
import io.circe.syntax._

def importEntity(entity: Entity)(implicit log: LogIO[IO]): IO[Unit] = {
  val ctxLog = log("ID" -> entity.id, "entityAsJSON" -> entity.asJson.printWith(Printer.spaces2))

  load(entity).handleErrorWith {
    case error =>
      ctxLog.error(s"Failed to import entity: $error.").void
      // JSON message includes `ID` and `entityAsJSON` fields

LogIO.log/LogBIO.log/LogBIO3.log/IzLogger.log let you refer to an implicit logger’s methods without naming a variable

import logstage.LogIO
import logstage.LogIO.log

def fn[F[_]: LogIO]: F[Unit] = {
  log.info(s"I'm logging with ${log}stage!")

ZIO environment support

LogstageZIO.log lets you carry LogZIO capability in environment.


import logstage.LogstageZIO.{LogZIO, log}
import zio.ZIO

val fn: ZIO[LogZIO, Nothing, Unit] = {
  log.info(s"I'm logging with ${log}stage!")
// fn: ZIO[LogZIO, Nothing, Unit] = zio.ZIO$Read@1ad09647

LogstageZIO.withFiberId provides a LogBIO instance that logs the current ZIO FiberId in addition to the thread id:


import logstage.{IzLogger, LogstageZIO}
import zio.IO

val log = LogstageZIO.withFiberId(IzLogger())
// log: logstage.package.LogBIO[IO] = logstage.LogstageZIO$$anon$1@2212379d

log.info(s"Hey! I'm logging with ${log}stage!")
// res14: IO[Nothing, Unit] = zio.ZIO$Descriptor@7a747e57
I 2019-03-29T23:21:48.760Z[Europe/Dublin] r.S.App9.res10 ...main-12:5384  (00_logstage.md:123) {fiberId=0} Hey! I'm logging with log=logstage.LogstageZIO$$anon$1@c39104astage!

LogBIO3.log extends environment support to trifunctor effect types with an instance of BIOMonadAsk[F] typeclass (from BIO)


import logstage.{LogBIO3, LogBIOEnv, IzLogger}
import logstage.LogBIOEnv.log
import zio.{Has, ZIO}

class Service[F[-_, +_, +_]: LogBIOEnv] {
  val fn: F[Has[LogBIO3[F]], Nothing, Unit] = {
    log.info(s"I'm logging with ${log}stage!")

zio.Runtime.default.unsafeRun {
  implicit val logger: LogBIOEnv[ZIO] = LogBIOEnv.make[ZIO]
  new Service[ZIO]

Custom JSON rendering with LogstageCodec

If you define an instance of LogstageCodec for your type, it will be used when rendering your logs to JSON instead of the default renderer.

You can derive instances of LogstageCodec from Circe codecs with logstage-rendering-circe or write them manually.


import io.circe.Codec
import io.circe.derivation
import logstage.LogstageCodec
import logstage.circe.LogstageCirceCodec

final case class KV(key: String, value: Int)

object KV {
  implicit val circeCodec: Codec[KV] = derivation.deriveCodec[KV]
  implicit val logstageCodec: LogstageCodec[KV] = LogstageCirceCodec.derived[KV]

If you want to make sure that all values in your interpolations are rendered using LogstageCodec instead of default renderer, use IzStrictLogger/LogIOStrict types from logstage.strict package:

import logstage.strict.IzStrictLogger
import logstage.ConsoleSink
import logstage.circe.LogstageCirceRenderingPolicy

val logger = IzStrictLogger(sink = ConsoleSink(LogstageCirceRenderingPolicy()))
// logger: IzStrictLogger = izumi.logstage.api.strict.IzStrictLogger@782f3d3e

logger.info(s"Got ${KV("key", 7) -> "KeyValue"}")

SLF4J Router

When not configured, logstage-adapter-slf4j will log messages with level >= Info to stdout.

Due to the global mutable nature of slf4j, to configure slf4j logging you’ll have to mutate a global singleton StaticLogRouter. Replace its LogRouter with the same one you use elsewhere in your application to use the same configuration for Slf4j.

import logstage.IzLogger
import izumi.logstage.api.routing.StaticLogRouter

val myLogger = IzLogger()
// myLogger: IzLogger = izumi.logstage.api.IzLogger@6653fed2

// configure SLF4j to use the same router that `myLogger` uses