{% set options = {
}|merge(options|default({})) %}
class Logger {
constructor({
enabled = false,
level = "info",
name = null,
} = {}) {
this.enabled = enabled;
this.level = level;
this.name = name;
this.levels = {
error: 0,
warn: 1,
info: 2,
debug: 3
};
}
log(level, ...args) {
if (!this.enabled) return;
if (this.levels[level] <= this.levels[this.level]) {
let text = `[${level}]`;
if (this.name) {
text += ` [${this.name}]`;
}
if (level === "error") {
console.error(text, ...args);
} else {
console.log(text, ...args);
}
}
}
error(...args) {
this.log("error", ...args);
this.logErrorObjects(args);
}
warn(...args) {
this.log("warn", ...args);
}
info(...args) {
this.log("info", ...args);
}
debug(...args) {
this.log("debug", ...args);
}
logErrorObjects(args) {
for (const arg of args) {
if (arg instanceof Error) {
console.error(arg);
}
}
}
}