Skip to content

Structured logging

Kosyrev Serge edited this page Feb 4, 2020 · 7 revisions

Overview

Structured logging essentially allows forwarding Haskell data values to JSON log files, provided that the data type has a ToObject instance.

To enable this one needs to:

  • pass a suitable data value to a suitable trace
  • enable structured logging in configuration

Code requirements

The basic prerequisite is, as mentioned above, having a data type with a ToObject instance.

Once that is satisfied the actual logging is as simple as:

     traceWith (toLogObject' StructuredLogging MaximalVerbosity tr) (Pet "bella" 8)

..where the base trace is a plain Trace IO Text.

An example is available at https://github.com/input-output-hk/iohk-monitoring-framework/blob/master/examples/complex/Main.lhs#L394.

Configuration

Let's assume that we want a JSON log file with structured payload -- that's easier for querying than text files.

The configuration, then, consists of two parts -- 1) scribe setup and 2) scribe selection:

setupScribes:
  - scKind: FileSK
    scName: "log.json"
    scFormat: ScJson
defaultScribes:
  - - FileSK
    - "log.json"

Note, that the second part can be similarly done using mapScribes, which would allow a fine-grain choice of what gets logged in a structured manner.

Querying structured logs

In this section we'll be using the jq command (https://stedolan.github.io/jq/manual/), so you'll need that installed.

Let's assume that you've got a JSON log file, as per the above description.

Basic queries can then be performed as follows:

Clone this wiki locally