Records / Console output
Console output
A logger prints each line to standard error, pretty on a terminal and as JSON anywhere else, so a developer reads it and a log collector parses it. Every language prints the same bytes for the same line, so one collector parses a program written in two. Every SDK's tests print the lines of one shared file byte for byte (contract LOG-03).
16:42:24.094 INFO api signed in user=42
16:42:24.094 WARN api slow request requestId=r-1 route=/notes ms=1200
16:42:24.094 ERROR api charge failed order=7
error: Error: card declined
at charge (billing.ts:41:9){"time":"2026-10-19T16:42:24.094Z","level":"INFO","stream":"api","msg":"signed in","user":42}Choosing the format
const log = store.logger('api', { console: 'json', time: 'full', hideStream: true })log = store.logger("api", console="json", time="full", hide_stream=True)handler := records.NewHandler(store, "api", console.JSON, console.TimeFull, console.HideStream)| Option | Values | Unless given |
|---|---|---|
console | pretty, json, off | pretty on a terminal, else json |
time | clock, full, off: a pretty line's time | clock |
hideStream | leaves the stream out of a pretty line | shown |
to | where lines go | standard error |
level | the least level kept and printed, or { store, console } | every level |
console: 'off' prints nothing and keeps every line: a server that ships its
console elsewhere, or a test.
Control characters in what you log
A pretty line never passes a control character from your data to the terminal. Say a user picks a name that holds a newline and a fake log line:
log.warn(`login failed for ${name}`, { user: 7 }) // name is "ann\n16:42:24.094 INFO api admin signed in"log.warning(f"login failed for {name}", user=7) # name is "ann\n16:42:24.094 INFO api admin signed in"slog.New(handler).Warn("login failed for "+name, "user", 7) // name is "ann\n16:42:24.094 INFO api admin signed in"tracing::warn!(user = 7, "login failed for {name}"); // name is "ann\n16:42:24.094 INFO api admin signed in"16:42:24.094 WARN api login failed for ann user=7
16:42:24.094 INFO api admin signed in- A message's lines after the first go under its line, indented four spaces, as the lines of a stack trace do. So a line that a message adds never looks like a log line of its own.
- Any other control character except a tab prints as its escape, such as
\u001b,\ror\u007f. This holds in a message, an event's name, a value and a source. So a logged value can't change colours, move the cursor, clear the screen or write to the terminal's clipboard. - The colours that the console paints with are its own. A JSON line escapes control characters as JSON does. The store keeps them as you logged them.
A quiet console
A store's logger can print less than it keeps. Give level a level for
each place: store is the least level kept, and console the least level
printed. In this example the console shows warnings and errors, and the store
keeps every line from info, so a query still finds what the terminal didn't
show:
const log = store.logger('api', { level: { store: 'info', console: 'warn' } })log = store.logger("api", level={"store": "info", "console": "warning"})handler := records.NewHandler(store, "api", records.Levels{Store: slog.LevelInfo, Console: slog.LevelWarn})A part that you leave out works as if level weren't set for it. Without
store, the store keeps every level:
const log = store.logger('api', { level: { console: 'warn' } }) // keeps every line, prints warnings and errorslog = store.logger("api", level={"console": "warning"}) # keeps every line, prints warnings and errorshandler := records.NewHandler(store, "api", records.Levels{Console: slog.LevelWarn}) // keeps every line, prints warnings and errors- One level, such as
level: 'info', sets both places. It is the same as{ store: 'info' }. - Go spells the two parts
records.Levels, whoseStoreandConsoleareslog.Levelervalues, so a part you leave out isnil. Rust spells themrecords::Levels, whose fields areOption<Level>. In Go,console.Level(slog.LevelInfo)isrecords.Levels{Store: slog.LevelInfo}. - The
consolepart changes only what is printed. A line at thestorelevel or above is kept with the same fields and context, whatever theconsolepart is. - The two parts are independent. With
{ store: 'info', console: 'debug' }, the console prints debug lines that the store doesn't keep. - An event from
log.eventhas no level, so it is printed and kept. - Without a
consolepart, the console prints what the store keeps. When the configlogchanges the level, the console follows it. Aconsolepart set in the code, orLOG_CONSOLE_LEVEL, stays as it is.
The environment wins
So that a deployment changes how lines look without a build, each variable goes over its own option in the code:
| Variable | Values | Wins over |
|---|---|---|
LOG_LEVEL | trace, debug, info, warn, error | level, or its store part |
LOG_CONSOLE_LEVEL | trace, debug, info, warn, error | the console part of level |
LOG_FORMAT | pretty, json, off | console |
LOG_TIME | clock, full, off | time |
LOG_LEVEL=debugchanges what a store keeps. The console prints from it too, unlesslevelhas aconsolepart. Aconsolepart that the code gave stays as it is.LOG_CONSOLE_LEVELchanges only what is printed, and never what a store keeps. It sets what every console prints: a store's logger, a logger of the console alone, and the stderr oftinystore serve.- A logger of the console alone only prints, so
LOG_CONSOLE_LEVELwins over itsleveland overLOG_LEVEL. For example,LOG_CONSOLE_LEVEL=warnwithLOG_LEVEL=debugprints warnings and errors. env: { prefix: 'APP' }readsAPP_LOG_LEVELand the rest instead;env: falsereads none.- The environment never turns on a console the code turned off.
- A value none of them can mean is ignored, and said once.
Colours
Every logger decides colours by one rule, in every language, and so does the
tinystore command line:
| Environment | Colours |
|---|---|
NO_COLOR set to anything but "" | off |
TERM=dumb | off |
FORCE_COLOR other than 0 | on, in a pipe too |
| none of them, on a terminal | on |
| none of them, in a pipe or a file | off |
FORCE_COLOR also makes a pipe's lines pretty, for an IDE's run console. On
Windows, colours on a terminal ask the console to read escape sequences
first, and stay off where an old console refuses.
A program that colours its own words asks the same rule, so that they and the log lines beside them are coloured alike (contract LOG-11):
import { colors } from '@tinyshed/tinystore'
const painted = colors(process.stdout)import sys
import tinystore
painted = tinystore.colors(sys.stdout)painted := console.Colors(os.Stdout)let painted = tinystore::console::colors(&std::io::stdout());A console without a store
A program that wants the logger and no store, a CLI, a script, takes it from
the package. The same calls, the same options but buffer and keep, the
same lines, kept nowhere. Its level is one level, because it only prints:
import { logger, secrets } from '@tinyshed/tinystore'
const log = logger('app', { redact: secrets })from tinystore import logger, secrets
log = logger("app", redact=secrets)log := slog.New(console.NewHandler("app", console.Redact(console.Secrets...)))let log = tinystore::console::logger("app").redact(SECRETS).layer();
tracing_subscriber::registry().with(log).init();In TypeScript and Python it loads nothing native. Go's package console
imports neither the store nor cgo. In Rust the crate's console feature
builds it alone, without a store or SQLite. Swapping logger('app') for
store.logger('app') later keeps the lines too, with no other change.