Skip to content

with

with(opts: ZylogFormatOptions): this

The with method returns a proxy-like interface that applies specific formatting options to exactly one subsequent logging call. This is extremely useful for adding temporary context (like Request IDs or User IDs) without mutating the global logger state.


Commonly used in HTTP servers to trace logs back to a specific request.

function handleRequest(req, res) {
const reqId = req.headers['x-request-id'];
zylog.with({ prefix: `req:${reqId}` }).info('Processing payment');
// The next call returns to the default prefix
zylog.info('Normal log line');
}

Change the separator or line ending for a single complex log.

zylog.with({ sep: ' -> ', prefix: 'DEBUG' }).trace('Step 1', 'Step 2', 'Step 3');

Combine with json() for structured, contextual output.

zylog.with({ prefix: 'AUDIT' }).json({ action: 'delete', userId: 501 }, 'warn');