@ricdotnet/logger
Version:
A logger utility for my personal projects.
75 lines • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const Constants_1 = require("./Constants");
const Queue_1 = require("./Queue");
class Logger {
logDir;
logToConsole;
logToFile;
level;
static logger = null;
logQueue;
constructor(options) {
console.log('Initiating a logger...');
if (Logger.logger) {
console.warn('The logger was already initiated.');
return;
}
this.logToConsole = options?.logToConsole ?? true;
this.logToFile = options?.logToFile ?? false;
this.level = options?.level ?? Constants_1.LogLevels.INFO;
Logger.logger = this;
if (this.logToFile) {
this.logQueue = new Queue_1.Queue(options);
}
console.log('Logger initiated.');
}
static get() {
return Logger.logger;
}
static dispose() {
this.logger = null;
}
async debug(message) {
if (this.logToFile) {
this.logQueue.add(message, Constants_1.LogLevels.DEBUG);
}
if (this.logToConsole && this.level === 'debug') {
console.debug(`${Constants_1.Constants.TEXT_CYAN}[DEBUG] (${new Date()}):${Constants_1.Constants.RESET} ${message}`);
}
}
async info(message) {
if (this.logToFile) {
this.logQueue.add(message, Constants_1.LogLevels.DEBUG);
}
if (this.logToConsole) {
console.log(`${Constants_1.Constants.TEXT_BLUE}[INFO] (${new Date()}):${Constants_1.Constants.RESET} ${message}`);
}
}
async warn(message) {
if (this.logToFile) {
this.logQueue.add(message, Constants_1.LogLevels.WARN);
}
if (this.logToConsole) {
console.warn(`${Constants_1.Constants.TEXT_YELLOW}[WARN] (${new Date()}):${Constants_1.Constants.RESET} ${message}`);
}
}
async error(message) {
if (this.logToFile) {
this.logQueue.add(message, Constants_1.LogLevels.ERROR);
}
if (this.logToConsole) {
console.error(`${Constants_1.Constants.TEXT_RED}[ERROR] (${new Date()}):${Constants_1.Constants.RESET} ${message}`);
}
}
async fmt(lvl, msg, ...args) {
let _msg = msg;
while (args.length > 0) {
_msg = _msg.replace('{}', args.shift());
}
this[lvl](_msg);
}
}
exports.Logger = Logger;
//# sourceMappingURL=Logger.js.map