Skip to content

json

json(
data: object,
options?: {
level?: ZylogOutputLevel;
message?: string;
}
): 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');

Attach a message for better traceability.

zylog.json({ query: 'SELECT * FROM users' }, { message: 'Database querying' });

Capture structured error details.

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