UNPKG

traceperf

Version:

High-performance function execution tracking and monitoring for Node.js

85 lines 2.47 kB
/** * JSON formatter for structured output * Formats log entries as JSON objects */ export class JsonFormatter { /** * Format a log entry as JSON * * @param level - The log level * @param message - The log message * @param args - Additional arguments * @param meta - Metadata for the log entry * @returns The formatted log entry as a JSON string */ format(level, message, args, meta) { const logEntry = { timestamp: meta.timestamp.toISOString(), level, indentLevel: meta.indentLevel, message: this.formatMessage(message), args: this.formatArgs(args), ...this.extractExtraMeta(meta), }; return JSON.stringify(logEntry); } /** * Format a message for JSON output * * @param message - The message to format * @returns The formatted message */ formatMessage(message) { if (typeof message === 'string') { return message; } return this.sanitizeObject(message); } /** * Format arguments for JSON output * * @param args - The arguments to format * @returns The formatted arguments */ formatArgs(args) { return args.map(arg => { if (typeof arg === 'object' && arg !== null) { return this.sanitizeObject(arg); } return arg; }); } /** * Sanitize an object for JSON serialization * * @param obj - The object to sanitize * @returns A sanitized version of the object */ sanitizeObject(obj) { try { // Use JSON.parse(JSON.stringify()) to remove non-serializable values return JSON.parse(JSON.stringify(obj)); } catch (error) { // If serialization fails, return a simplified object return { value: String(obj) }; } } /** * Extract additional metadata from the log meta object * * @param meta - The log metadata * @returns Additional metadata as an object */ extractExtraMeta(meta) { const result = {}; // Copy all properties except the standard ones Object.keys(meta).forEach(key => { if (!['timestamp', 'level', 'indentLevel'].includes(key)) { result[key] = meta[key]; } }); return result; } } //# sourceMappingURL=json.js.map