Skip to content

json

json(data: object, level: ZylogOutputLevel = 'info'): void

The json method stringifies any JavaScript object into a pretty-printed JSON format (2-space indentation). This ensures that complex data structures are readable in the terminal and correctly formatted for file logs.


Standard usage for inspecting incoming data.

const user = { id: 1, name: 'Alice', roles: ['admin', 'editor'] };
zylog.json(user);

Override the log level to ensure critical object state is captured in error logs.

try {
processData();
} catch (err) {
zylog.json({ error: err.message, stack: err.stack }, 'error');
}

Add context to your JSON dumps.

zylog.with({ prefix: 'CONFIG' }).json(process.env, 'debug');