Published: 3/7/2026 Enhanced console logger APEX JavaScript Enhanced console logger Example – how you would use it in your codeconst logger = setupLogging(); // <-- one‑time call console.log('This is a log message'); console.info('Info goes here'); console.warn('Watch out!'); console.error('Something broke'); logger.toggleLogging(false); // mute all further console output console.log('You will NOT see this'); logger.toggleLogging(true); // turn it back on console.error('Now you see this again'); logger.restore(); // put the original console back (optional) console.log('Back to the vanilla console');/** * Installs the enhanced console logger. * * @returns {{ toggleLogging: (enabled:boolean)=>void, * restore: ()=>void, * isEnabled: ()=>boolean }} * * Call once (e.g. at application start): * const logger = setupLogging(); * * // Use the console as usual: * console.log('Hello'); * console.warn('Oops'); * * // Turn logging on/off: * logger.toggleLogging(false); // mute * logger.toggleLogging(true); // un‑mute * * // When you need the original console back: * logger.restore(); */ function setupLogging() { // ----------------------------------------------------------------- // Keep a reference to the original console methods. // ----------------------------------------------------------------- const originalLog = console.log; const originalInfo = console.info; const originalWarn = console.warn; const originalError = console.error; // ----------------------------------------------------------------- // Internal state – is logging currently enabled? // ----------------------------------------------------------------- let isLoggingEnabled = true; // ----------------------------------------------------------------- // Central logging routine – used by all four overridden methods. // ----------------------------------------------------------------- function logMessage(level, ...args) { if (!isLoggingEnabled) return; // muted → nothing const timestamp = new Date().toLocaleTimeString(); // e.g. "14:23:07" const prefix = `[${timestamp}] [${level}]`; // Join args with a space (mirrors how console.log prints them) const text = args.map(a => (typeof a === 'object' ? JSON.stringify(a) : a)).join(' '); // Use the *original* console.log to avoid recursion. originalLog(`${prefix} ${text}`); } // ----------------------------------------------------------------- // Override the console methods. // ----------------------------------------------------------------- console.log = (...args) => logMessage('LOG', ...args); console.info = (...args) => logMessage('INFO', ...args); console.warn = (...args) => logMessage('WARN', ...args); console.error = (...args) => logMessage('ERROR', ...args); // ----------------------------------------------------------------- // Public helpers returned to the caller. // ----------------------------------------------------------------- return { /** Enable (true) or disable (false) logging. */ toggleLogging(enabled) { isLoggingEnabled = Boolean(enabled); }, /** Query the current enabled state. */ isEnabled() { return isLoggingEnabled; }, /** Restore the original console methods (undo the patch). */ restore() { console.log = originalLog; console.info = originalInfo; console.warn = originalWarn; console.error = originalError; }, }; }