Message Templates

Type-safe structured logging for OCaml with PPX support.

Overview

Message Templates is a high-performance structured logging library for OCaml that provides:

Quick Start

(* Configure and set global logger *)
let logger =
  Configuration.create ()
  |> Configuration.write_to_console ~colors:true ()
  |> Configuration.create_logger
in
Log.set_logger logger;

(* Log with structured data *)
Log.information "User {username} logged in from {ip}"
  [("username", `String "alice"); ("ip", `String "192.168.1.1")]

Using the PPX Extension

The PPX extension provides compile-time template validation and automatic variable capture:

(* Template variables are validated at compile time *)
let username = "alice" in
let ip_address = "192.168.1.1" in
[%log.information "User {username} logged in from {ip_address}"]

(* Output CLEF JSON format *)
let event = [%template "Processing {item_id}"] in

Core Modules

The library is organized into several key modules:

Types and Events

Configuration and Logging

Sinks

Advanced Features

Installation

Add to your dune-project:

(depends
  (message-templates (>= 0.1.0)))

For PPX support:

(depends
  (message-templates-ppx (>= 0.1.0)))