LogStage

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

Dependencies

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

Overview

The following snippet:

import logstage.IzLogger
import scala.util.Random

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

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@39841e8c
val delta = Random.nextInt(1000)
// delta: Int = 291

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

Will look like this in string form:

logstage-sample-output-string

And like this in JSON:

logstage-sample-output-string

Note:

  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$@6ce78876
val jsonSink = ConsoleSink(LogstageCirceRenderingPolicy(prettyPrint = true))
// jsonSink: ConsoleSink = izumi.logstage.sink.ConsoleSink@47a3ea5e

val sinks = List(jsonSink, textSink)
// sinks: List[ConsoleSink] = List(izumi.logstage.sink.ConsoleSink@47a3ea5e, izumi.logstage.sink.ConsoleSink$ColoredConsoleSink$@6ce78876)

val logger: IzLogger = IzLogger(Trace, sinks)
// logger: IzLogger = izumi.logstage.api.IzLogger@265bd914
val contextLogger: IzLogger = logger("key" -> "value")
// contextLogger: IzLogger = izumi.logstage.api.IzLogger@762f4191

logger.info("Hey")

contextLogger.info(s"Hey")

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@2e06b410

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

log.info(s"Hey! I'm logging with ${log}stage!")
// res9: IO[Unit] = IO$1943653432
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.

Example:

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@4f7c785e

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

Example:

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

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

log.info(s"Hey! I'm logging with ${log}stage!")
// res14: IO[Nothing, Unit] = zio.ZIO$Descriptor@7940ddbc
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)

Example:

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]
    .fn
    .provide(Has(LogBIO3.fromLogger(IzLogger())))
}

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.

Example:

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@5552e442

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@693bc680

// configure SLF4j to use the same router that `myLogger` uses
StaticLogRouter.instance.setup(myLogger.router)