UNPKG

@bernierllc/logging

Version:

A comprehensive logging package with Winston integration for audit and system logging

125 lines (124 loc) 4.94 kB
"use strict"; /* Copyright (c) 2025 Bernier LLC This file is licensed to the client under a limited-use license. The client may use and modify this code *only within the scope of the project it was delivered for*. Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAuditLogger = createAuditLogger; exports.logAudit = logAudit; exports.logAuditEntry = logAuditEntry; const winston_1 = __importDefault(require("winston")); const REQUIRED_ENV_VARS = ['LOG_LEVEL', 'NODE_ENV']; const warnedMissingEnvVars = new Set(); function areRequiredEnvVarsPresent() { return REQUIRED_ENV_VARS.every(envVar => !!process.env[envVar]); } /** * Creates a Winston logger configured for audit logging * @param config Configuration options for the audit logger * @returns Configured Winston logger instance */ function createAuditLogger(config = {}) { // Fail hard if used in a browser context if (typeof window !== 'undefined' && typeof window.document !== 'undefined') { throw new Error('[@bernierllc/logging] Audit logger cannot be used in the browser. This logger is server-only and must only be instantiated in API or server-side code.'); } if (!areRequiredEnvVarsPresent()) { // No-op logger if required env vars are missing return { log: () => { }, info: () => { }, warn: () => { }, error: () => { }, debug: () => { }, clear: () => { }, add: () => { }, }; } const { level = 'info', format = 'json', transports = [], defaultMeta = { service: 'audit-log' }, databaseUrl, collection = 'audit_logs', enableConsole = false, enableFile = false, filePath = './logs/audit.log' } = config; const loggerTransports = []; // Add database transport if URL is provided if (databaseUrl) { try { // Note: This requires winston-mongodb or similar transport // For now, we'll use a file transport as fallback loggerTransports.push(new winston_1.default.transports.File({ filename: filePath, level: 'info' })); } catch (error) { console.warn('Failed to create database transport, falling back to file transport'); loggerTransports.push(new winston_1.default.transports.File({ filename: filePath, level: 'info' })); } } // Add console transport if enabled if (enableConsole) { loggerTransports.push(new winston_1.default.transports.Console({ level: 'debug', format: winston_1.default.format.combine(winston_1.default.format.colorize(), winston_1.default.format.simple()) })); } // Add file transport if enabled if (enableFile) { loggerTransports.push(new winston_1.default.transports.File({ filename: filePath, level: 'info' })); } // Add custom transports loggerTransports.push(...transports); // If no transports configured, add default file transport if (loggerTransports.length === 0) { loggerTransports.push(new winston_1.default.transports.File({ filename: filePath, level: 'info' })); } const logger = winston_1.default.createLogger({ level, format: winston_1.default.format.combine(winston_1.default.format.timestamp(), winston_1.default.format.json()), defaultMeta, transports: loggerTransports }); // Prevent audit logs in development unless explicitly enabled if (process.env.NODE_ENV !== 'production' && !enableConsole) { logger.clear(); logger.add(new winston_1.default.transports.Console({ level: 'debug', format: winston_1.default.format.combine(winston_1.default.format.colorize(), winston_1.default.format.simple()) })); } return logger; } /** * Logs an audit entry with proper formatting and validation * @param logger Winston logger instance * @param entry Audit log entry data */ function logAudit(logger, entry) { const auditEntry = { ...entry, timestamp: entry.timestamp || new Date().toISOString(), environment: process.env.NODE_ENV || 'development' }; logger.info('AUDIT_LOG', auditEntry); } /** * Convenience function to create and log audit entry in one call * @param entry Audit log entry data * @param config Optional logger configuration */ function logAuditEntry(entry, config) { const logger = createAuditLogger(config); logAudit(logger, entry); } //# sourceMappingURL=auditLogger.js.map