Timing and Grouping
Problem
You need to measure how long a code block takes and group the related log entries so they are easy to correlate in output — without sprinkling Date.now() calls across the codebase.
Solution
Use time(label, fn) to measure execution and groupCollapsed(label, fn) to nest related log lines under a collapsible group in the console.
ts
import { createLogger } from '@vielzeug/rune';
const log = createLogger('checkout');
const order = { id: 'order-42' };
async function processOrder(value: typeof order): Promise<string> {
return value.id;
}
const result = await log.groupCollapsed('Checkout', async () => {
log.info('validating cart');
return log.time('process order', () => processOrder(order));
});
console.log(result);Pitfalls
time()always emits atdebuglevel with the label as the log message and{ duration_ms }in context. IflogLevelis abovedebug, the timer still runs but no entry is emitted. SetlogLevel: 'debug'when capturing timing data.time()measures wall-clock time viaperformance.now(). For async functions, it covers total elapsed time including I/O wait — not CPU time. Long I/O waits will inflate the result.group()andgroupCollapsed()callconsole.group/console.groupEndregardless of which transport is configured. They are visual wrappers for the console; other transports (remote, JSON, batch) receive entries as normal flat events with no grouping semantics.- When
logLevelis'off', the group wrapper is bypassed entirely but the callback still executes.