Tern

log

The log module writes leveled, structured log lines to stderr: one line per call, in logfmt (the default), JSON, or a plain text format for people. Fields are a {str: str} map, so a line carries the values a log search needs, not just a sentence.

import log

fn main():
    log.info("server started")
    log.info_with("user logged in", {"user": "ada", "id": "7"})
    log.debug("not shown: the default level is info")
    print("log lines go to stderr; stdout stays for output")
Output
log lines go to stderr; stdout stays for output

On stderr that prints:

time=2026-09-28T06:45:12.345Z level=info msg="server started"
time=2026-09-28T06:45:12.345Z level=info msg="user logged in" user=ada id=7

Functions

fn debug(msg: str)          fn debug_with(msg: str, fields: {str: str})
fn info(msg: str)           fn info_with(msg: str, fields: {str: str})
fn warn(msg: str)           fn warn_with(msg: str, fields: {str: str})
fn error(msg: str)          fn error_with(msg: str, fields: {str: str})
fn set_level(name: str) bool      "debug", "info" (the default), "warn", "error", "off"
fn set_format(name: str) bool     "logfmt" (the default), "json", "text"

set_level and set_format return false for a name they don't know (and change nothing). The environment variables TERN_LOG_LEVEL and TERN_LOG_FORMAT set the same things without changing the program, which is how you'd turn on debug logs in production for a while. A call below the level returns right away without formatting its fields.

Formats

logfmt  time=2026-09-28T06:45:12.345Z level=warn msg="slow query" ms=250 sql="SELECT 1"
json    {"time":"2026-09-28T06:45:12.345Z","level":"warn","msg":"slow query","ms":"250","sql":"SELECT 1"}
text    2026-09-28 06:45:12.345 WARN  slow query ms=250 sql="SELECT 1"

Times are UTC with milliseconds. In logfmt, values with spaces, quotes or = are quoted and escaped. Field values are strings (str(n) for numbers), so JSON output has them as JSON strings. Each line is a single write, so lines from different threads and tasks never interleave.

import log

fn main():
    log.set_format("json")
    print(log.set_level("debug"), log.set_level("verbose"))
    log.debug_with("cache miss", {"key": "user:7"})
    log.set_level("off")
    log.error("silenced")
Output
true false

For per-request lines in a web server, roost.logger() middleware does the access log; use log inside handlers for everything else.

Recent records

fn recent(n: int) [Record]          the newest n kept records, oldest first
fn keep(name: str) bool             keep records from this level up (default "warn")

Whatever the output level, the last 100 records at warn and above stay in memory, so an error reporter can attach what led up to a failure (the Sentry package sends them as breadcrumbs). Record has time (seconds since 1970), level, msg and fields.

Source: docs/std/log.md. Every example on this page is compiled and run by tern x test docs.