Files
SignageHTML/public/js/utils/logger.js
kyle 1fdb3e48c7 Items 10-15: ES modules, inline style cleanup, template modal, code modernization
- Item 10: Convert to ES modules with import/export, single module entry point
- Item 11: Replace inline styles with CSS classes (background overlay, card
  animations, highlight effect, config modal form elements)
- Item 12: Move ConfigManager modal HTML from JS template literal to
  <template> element in index.html
- Item 13: Replace deprecated url.parse() with new URL() in server.js
  and update route handlers to use searchParams
- Item 14: Replace JSON.parse/stringify deep clone with structuredClone()
- Item 15: Remove dead JSON-fixing regex code from departures.js route

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 14:30:03 +01:00

104 lines
3.0 KiB
JavaScript

/**
* Logger utility for consistent logging throughout the application
* Supports different log levels and can be configured for production vs development
*/
class Logger {
constructor() {
this.level = this.getLogLevel();
}
/**
* Get the current log level from environment or default to 'info'
* @returns {string} Log level ('debug', 'info', 'warn', 'error')
*/
getLogLevel() {
// In production, you might want to check window.location.hostname
// or a global config. For now, default to 'info'
if (typeof window !== 'undefined' && window.LOG_LEVEL) {
return window.LOG_LEVEL;
}
// Default to 'info' level
return 'info';
}
/**
* Check if a log level should be output
* @param {string} level - Log level to check
* @returns {boolean} Whether to output this level
*/
shouldLog(level) {
const levels = ['debug', 'info', 'warn', 'error'];
const currentLevelIndex = levels.indexOf(this.level);
const messageLevelIndex = levels.indexOf(level);
return messageLevelIndex >= currentLevelIndex;
}
/**
* Format a log message with timestamp
* @param {string} level - Log level
* @param {string} message - Message to log
* @param {any[]} args - Additional arguments
* @returns {string} Formatted message
*/
formatMessage(level, message, args = []) {
const timestamp = new Date().toISOString();
const prefix = `[${timestamp}] [${level.toUpperCase()}]`;
return args.length > 0 ? [prefix, message, ...args] : [prefix, message];
}
/**
* Log a debug message
* @param {string} message - Message to log
* @param {...any} args - Additional arguments
*/
debug(message, ...args) {
if (this.shouldLog('debug')) {
console.debug(...this.formatMessage('debug', message, args));
}
}
/**
* Log an info message
* @param {string} message - Message to log
* @param {...any} args - Additional arguments
*/
info(message, ...args) {
if (this.shouldLog('info')) {
console.info(...this.formatMessage('info', message, args));
}
}
/**
* Log a warning message
* @param {string} message - Message to log
* @param {...any} args - Additional arguments
*/
warn(message, ...args) {
if (this.shouldLog('warn')) {
console.warn(...this.formatMessage('warn', message, args));
}
}
/**
* Log an error message
* @param {string} message - Message to log
* @param {...any} args - Additional arguments
*/
error(message, ...args) {
if (this.shouldLog('error')) {
console.error(...this.formatMessage('error', message, args));
}
}
}
// Create a singleton instance
const logger = new Logger();
// ES module export
export { Logger, logger };
// Keep window reference for backward compatibility
window.Logger = Logger;
window.logger = logger;