UNPKG

@noony-serverless/core

Version:

A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript

135 lines 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = exports.logger = void 0; // Performance optimization: Object pool for log data to reduce GC pressure class LogDataPool { pool = []; maxPoolSize = 50; acquire() { return (this.pool.pop() || { timestamp: '', level: 'info', message: '', }); } release(logData) { if (this.pool.length < this.maxPoolSize) { // Reset object properties logData.timestamp = ''; logData.level = 'info'; logData.message = ''; // Remove any additional properties const keys = Object.keys(logData); for (let i = 3; i < keys.length; i++) { delete logData[keys[i]]; } this.pool.push(logData); } } } // Performance optimization: Dynamic method references for testing compatibility const getLogMethod = (level) => { switch (level) { case 'error': return console.error; case 'warn': return console.warn; case 'debug': return console.debug; default: return console.log; } }; class Logger { logDataPool = new LogDataPool(); isDebugEnabled; timestampCache = ''; lastTimestamp = 0; constructor() { // Performance optimization: Cache debug mode check this.isDebugEnabled = process.env.NODE_ENV === 'development' || process.env.DEBUG === 'true' || process.env.LOG_LEVEL === 'debug'; } /** * Performance optimized timestamp generation with caching * Cache timestamps for up to 1 second to reduce Date object creation */ getTimestamp() { const now = Date.now(); // Cache timestamp for 1 second to reduce object creation if (now - this.lastTimestamp > 1000) { this.timestampCache = new Date(now).toISOString(); this.lastTimestamp = now; } return this.timestampCache; } /** * Optimized log method with object pooling and lazy evaluation */ log(level, message, options) { // Performance optimization: Early return for debug logs in production if (level === 'debug' && !this.isDebugEnabled) { return; } const logData = this.logDataPool.acquire(); // Performance optimization: Lazy timestamp generation logData.timestamp = this.getTimestamp(); logData.level = level; logData.message = message; // Add options if provided if (options) { Object.assign(logData, options); } // Use dynamic method reference for testing compatibility const logMethod = getLogMethod(level); // For testing: create a copy of the data to avoid pool interference const logDataCopy = { ...logData }; logMethod(logDataCopy); // Always return object to pool this.logDataPool.release(logData); } /** * Performance optimized logging methods with level checks */ info(message, options) { this.log('info', message, options); } error(message, options) { this.log('error', message, options); } warn(message, options) { this.log('warn', message, options); } debug(message, options) { // Performance optimization: Early return for debug in production if (!this.isDebugEnabled) return; this.log('debug', message, options); } /** * Performance monitoring method for internal framework use */ logPerformance(operation, duration, metadata) { if (this.isDebugEnabled) { this.debug(`Performance: ${operation}`, { duration: `${duration}ms`, ...metadata, }); } } /** * Get logger statistics for monitoring */ getStats() { return { poolSize: this.logDataPool['pool'].length, maxPoolSize: this.logDataPool['maxPoolSize'], debugEnabled: this.isDebugEnabled, }; } } exports.Logger = Logger; exports.logger = new Logger(); //# sourceMappingURL=logger.js.map