UNPKG

legal-markdown-js

Version:

Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version

162 lines 5.37 kB
/** * @fileoverview Lightweight Logging Utility for Legal Markdown Processing * * This module provides a simple, efficient logging utility for the Legal Markdown * processing system. It offers structured logging with different levels and * optional data context support. * * Features: * - Multiple log levels (debug, info, warn, error) * - Debug mode controlled by environment variable * - Structured logging with contextual data * - stderr output via process.stderr.write (debug/info) and console.warn/error (warn/error) * - Optional data parameter for detailed logging * - Lightweight with no external dependencies * * @example * ```typescript * import { logger } from './logger.js'; * * // Basic logging * logger.info('Processing legal document'); * logger.warn('Missing field detected'); * logger.error('Failed to parse YAML'); * * // Logging with context data * logger.debug('Field processed', { fieldName: 'client.name', value: 'Acme Corp' }); * logger.info('Document exported', { format: 'pdf', size: '2.3MB' }); * ``` */ /** * Simple logger utility for Legal Markdown processing * * Provides structured logging with different levels and optional context data. * Debug logging is only enabled when DEBUG environment variable is set. * * {Object} logger * @example * ```typescript * import { logger } from './logger.js'; * * // Set DEBUG=true in environment to see debug logs * logger.debug('Processing started', { file: 'contract.md' }); * logger.info('Document processed successfully'); * logger.warn('Optional field missing', { field: 'client.address' }); * logger.error('Processing failed', { error: 'Invalid syntax' }); * ``` */ import { getRuntimeConfig } from '../config/runtime.js'; // Global debug state for browser compatibility let debugEnabled = false; let logLevel = getConfig().logging.level; function safeSerialize(data) { try { return JSON.stringify(data); } catch { try { return String(data); } catch { return '[Unserializable data]'; } } } function writeToStderr(prefix, message, data) { const dataPart = data !== undefined ? ` ${safeSerialize(data)}` : ''; process.stderr.write(`${prefix} ${message}${dataPart}\n`); } export const logger = { /** * Enable or disable debug logging * @param {boolean} enabled - Whether to enable debug logging */ setDebugEnabled: (enabled) => { debugEnabled = enabled; }, /** * Set the logging level * @param {string} level - The log level ('debug', 'info', 'warn', 'error', 'none') */ setLogLevel: (level) => { logLevel = level; }, /** * Log debug messages (only shown when DEBUG environment variable is set or debug is enabled) * * debug * @param {string} message - The debug message to log * @param {any} [data] - Optional contextual data to include with the log * @returns {void} * @example * ```typescript * // Enable debug mode: DEBUG=true node app.js or logger.setDebugEnabled(true) * logger.debug('Processing field', { name: 'client.name', type: 'string' }); * logger.debug('Import resolved', { path: './shared/header.md' }); * ``` */ debug: (message, data) => { const isDebugEnabled = debugEnabled || getConfig().logging.debug; if (isDebugEnabled) { writeToStderr('[DEBUG]', message, data); } }, /** * Log informational messages * * info * @param {string} message - The informational message to log * @param {any} [data] - Optional contextual data to include with the log * @returns {void} * @example * ```typescript * logger.info('Document processing completed'); * logger.info('Export successful', { format: 'html', path: './output.html' }); * ``` */ info: (message, data) => { if (logLevel === 'none' || logLevel === 'warn' || logLevel === 'error') return; writeToStderr('[INFO]', message, data); }, /** * Log warning messages * * warn * @param {string} message - The warning message to log * @param {any} [data] - Optional contextual data to include with the log * @returns {void} * @example * ```typescript * logger.warn('Missing optional field', { field: 'client.phone' }); * logger.warn('Import file not found', { path: './missing.md' }); * ``` */ warn: (message, data) => { if (logLevel === 'none' || logLevel === 'error') return; console.warn(`[WARN] ${message}`, data || ''); }, /** * Log error messages * * error * @param {string} message - The error message to log * @param {any} [data] - Optional contextual data to include with the log * @returns {void} * @example * ```typescript * logger.error('Failed to parse YAML frontmatter', { error: 'Invalid syntax' }); * logger.error('Document processing failed', { file: 'contract.md', reason: 'Missing imports' }); * ``` */ error: (message, data) => { if (logLevel === 'none') return; console.error(`[ERROR] ${message}`, data || ''); }, }; function getConfig() { return getRuntimeConfig(); } //# sourceMappingURL=logger.js.map