Skip to content

Logging DSL

A family of small macros can form a mini-DSL — a domain-specific vocabulary without the need for an external framework. Each level (INFO, WARN, ERROR) becomes a dedicated macro that automatically prefixes the message. The argument is any string expression, including interpolations.

The log_at! macro adds a timestamp field, showing how to compose macros with variable arity to enrich context:

log_info!, log_warn!, log_error!, and log_at! forming a concise logging DSL.

08-logging-dsl.zolo
Playground
// Feature: Mini logging DSL with macros
// Syntax: several small macros that prefix messages with a level
// When to use: standardize log format without needing a framework;
// makes the level visually obvious in code.

macro log_info(msg) {
  print("[INFO ] {$msg}")
}


macro log_warn(msg) {
  print("[WARN ] {$msg}")
}


macro log_error(msg) {
  print("[ERROR] {$msg}")
}


// Direct usage.
log_info!("server started")
log_warn!("slow connection")
log_error!("database timeout")

// expected:
// [INFO ] server started
// [WARN ] slow connection
// [ERROR] database timeout

// Combined with interpolation — the argument is any expr.
let user = "alice"
let attempts = 3
log_warn!("user {user} tried {attempts} times")

// expected: [WARN ] user alice tried 3 times

// Composed macro: log with simulated timestamp.
macro log_at(when, level, msg) {
  print("[{$when}][{$level}] {$msg}")
}


log_at!("12:00:01", "INFO", "boot complete")
// expected: [12:00:01][INFO] boot complete

Challenge

Add a log_debug!(msg) macro that prints "[DEBUG] msg", but only when a variable let debug_mode = true is defined in scope. Use pick! from the multiple-parameters chapter to conditionally control the output.

enespt-br