JSON structured logging reporter for OCaml Logs - outputs to ELK, Datadog, Loki, etc.
OCaml 79.1%
Dune 5.7%
Other 15.2%
23 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-json-logs https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-json-logs
git@git.recoil.org:gazagnaire.org/ocaml-json-logs git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-json-logs

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

json-logs#

JSON structured logging reporter for the OCaml Logs library.

Overview#

Outputs log messages in JSON format for integration with log aggregation tools (ELK, Datadog, Splunk, Loki, etc.).

Each log entry includes:

  • timestamp: ISO 8601 / RFC 3339 format
  • level: app, debug, info, warning, error
  • source: log source name
  • message: formatted log message
  • Structured fields from Logs.Tag values

Installation#

opam install json-logs

Usage#

let () =
  Logs.set_reporter (Json_logs.reporter ());
  Logs.set_level (Some Logs.Info);
  Logs.info (fun m -> m "Server started on port %d" 8080)

Output:

{"timestamp":"2025-01-20T12:00:00Z","level":"info","source":"application","message":"Server started on port 8080"}

With Structured Tags#

let user_id = Logs.Tag.def "user_id" Format.pp_print_string
let request_id = Logs.Tag.def "request_id" Format.pp_print_string

let () =
  let tags = Logs.Tag.(empty |> add user_id "123" |> add request_id "abc-456") in
  Logs.info (fun m -> m "User logged in" ~tags)

Output:

{"timestamp":"2025-01-20T12:00:00Z","level":"info","source":"application","message":"User logged in","user_id":"123","request_id":"abc-456"}

API#

  • Json_logs.reporter () - Create a JSON logging reporter
  • Json_logs.json_escape - Escape strings for JSON output
  • Json_logs.level_to_string - Convert log level to string
  • logs - The underlying logging library
  • logs-fmt - Text-based reporter (included with logs)

Licence#

MIT License. See LICENSE.md for details.