48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getLogger = getLogger;
|
|
const LEVELS = {
|
|
DEBUG: 0,
|
|
INFO: 1,
|
|
WARN: 2,
|
|
ERROR: 3,
|
|
};
|
|
const DEFAULT_LOG_LEVEL = "INFO";
|
|
const configuredLevel = (process.env.LOG_LEVEL ?? "").toUpperCase();
|
|
const currentLogLevel = configuredLevel in LEVELS ? configuredLevel : DEFAULT_LOG_LEVEL;
|
|
const currentLogLevelValue = LEVELS[currentLogLevel];
|
|
function shouldLog(level) {
|
|
return LEVELS[level] >= currentLogLevelValue;
|
|
}
|
|
function formatMessage(level, component, message) {
|
|
return `${new Date().toISOString()} ${level} [${component}] ${message}`;
|
|
}
|
|
class Logger {
|
|
constructor(component) {
|
|
this.component = component;
|
|
}
|
|
debug(message, ...args) {
|
|
if (!shouldLog("DEBUG"))
|
|
return;
|
|
console.log(formatMessage("DEBUG", this.component, message), ...args);
|
|
}
|
|
info(message, ...args) {
|
|
if (!shouldLog("INFO"))
|
|
return;
|
|
console.log(formatMessage("INFO", this.component, message), ...args);
|
|
}
|
|
warn(message, ...args) {
|
|
if (!shouldLog("WARN"))
|
|
return;
|
|
console.warn(formatMessage("WARN", this.component, message), ...args);
|
|
}
|
|
error(message, ...args) {
|
|
if (!shouldLog("ERROR"))
|
|
return;
|
|
console.error(formatMessage("ERROR", this.component, message), ...args);
|
|
}
|
|
}
|
|
function getLogger(component) {
|
|
return new Logger(component);
|
|
}
|