Files
generalFunctions/src/helper/logger.js
Rene De Ren 82094d8d09 Fix ESLint errors, bugs, and add gravity export
- Fix missing return in childRegistrationUtils.registerChild()
- Fix assertionUtils: assertNoNaN uses this.assertNoNaN
- Fix logger: nameModule uses this.nameModule
- Fix assetUtils: convert ESM to CommonJS
- Fix childRegistrationUtils_DEPRECATED: desc -> softwareType
- Add gravity export to index.js for rotatingMachine
- Fix ESLint errors across 18 files (no-undef, no-case-declarations,
  no-mixed-spaces-and-tabs, parsing errors)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 13:39:40 +01:00

57 lines
1.7 KiB
JavaScript

class Logger {
constructor(logging = true, logLevel = 'debug', nameModule = 'N/A') {
this.logging = logging; // Boolean flag to enable/disable logging
this.logLevel = logLevel; // Default log level: 'debug', 'info', 'warn', 'error'
this.levels = ['debug', 'info', 'warn', 'error']; // Log levels in order of severity
this.nameModule = nameModule; // Name of the module that uses the logger
}
// Helper function to check if a log message should be displayed based on current log level
shouldLog(level) {
return this.levels.indexOf(level) >= this.levels.indexOf(this.logLevel);
}
// Log a debug message
debug(message) {
if (this.logging && this.shouldLog('debug')) {
console.debug(`[DEBUG] -> ${this.nameModule}: ${message}`);
}
}
// Log an info message
info(message) {
if (this.logging && this.shouldLog('info')) {
console.info(`[INFO] -> ${this.nameModule}: ${message}`);
}
}
// Log a warning message
warn(message) {
if (this.logging && this.shouldLog('warn')) {
console.warn(`[WARN] -> ${this.nameModule}: ${message}`);
}
}
// Log an error message
error(message) {
if (this.logging && this.shouldLog('error')) {
console.error(`[ERROR] -> ${this.nameModule}: ${message}`);
}
}
// Set the log level dynamically
setLogLevel(level) {
if (this.levels.includes(level)) {
this.logLevel = level;
} else {
console.error(`[ERROR ${this.nameModule}]: Invalid log level: ${level}`);
}
}
// Toggle the logging on or off
toggleLogging() {
this.logging = !this.logging;
}
}
module.exports = Logger;