json
Signature
Section titled âSignatureâjson( data: object, options?: { level?: ZylogOutputLevel; message?: string; }): voidDescription
Section titled âDescriptionâ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.
Examples
Section titled âExamplesâLogging a Payload
Section titled âLogging a PayloadâStandard usage for inspecting incoming data.
const user = { id: 1, name: 'Alice', roles: ['admin', 'editor'] };
zylog.json(user);Critical Data Inspection
Section titled âCritical Data Inspectionâ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');}Using with Prefixes
Section titled âUsing with PrefixesâAdd context to your JSON dumps.
zylog.with({ prefix: 'CONFIG' }).json(process.env, 'debug');Adding Context Message
Section titled âAdding Context MessageâAttach a message for better traceability.
zylog.json({ query: 'SELECT * FROM users' }, { message: 'Database querying' });Error Inspection
Section titled âError InspectionâCapture structured error details.
try { processData();} catch (err) { zylog.json( { message: err.message, stack: err.stack }, { level: 'error', message: 'Unhandled exception' }, );}