@avonjs/avonjs
Version:
A fluent Node.js API generator.
69 lines (68 loc) • 2.06 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
class Debug {
namespace;
suffix;
debuggers = new Map();
constructor(namespace = 'avonjs', suffix) {
this.namespace = namespace;
this.suffix = suffix;
//
}
/**
* Extend the log namespace.
*/
extend(namespace) {
return new Debug(this.namespace, namespace);
}
/**
* Log the "error" level messages.
*/
error(formatter, ...args) {
this.resolve('error')(formatter, ...args);
return this;
}
/**
* Log the "info" level messages.
*/
info(formatter, ...args) {
this.resolve('info')(formatter, ...args);
return this;
}
/**
* Log the "warn" level messages.
*/
warn(formatter, ...args) {
this.resolve('warn')(formatter, ...args);
return this;
}
/**
* Log the "error" level messages.
*/
dump(formatter, ...args) {
this.resolve('debug')(formatter, ...args);
return this;
}
/**
* Resolve the logger instance with LRU management.
*/
resolve(level) {
const namespaces = [level, this.suffix ?? ''];
const cacheKey = namespaces.join(':');
if (!this.debuggers.has(cacheKey)) {
// Limit cache size to avoid memory leaks
if (this.debuggers.size >= 1000) {
// Remove the oldest entry to keep cache within limit
const oldestKey = this.debuggers.keys().next().value;
this.debuggers.delete(oldestKey);
}
this.debuggers.set(cacheKey, namespaces.reduce((debug, namespace) => (namespace ? debug.extend(namespace) : debug), (0, debug_1.default)(this.namespace)));
}
return this.debuggers.get(cacheKey);
}
}
exports.default = new Debug();