@minimaltech/ra-infra
Version:
Minimal Technology ReactJS Infrastructure
64 lines • 1.92 kB
JavaScript
import { getError } from '../utilities';
import dayjs from 'dayjs';
const applicationLogger = console;
export class Logger {
static instance;
isDebugEnabled;
constructor(opts) {
this.isDebugEnabled = opts?.enableDebug ?? false;
}
static getInstance(opts) {
if (!this.instance) {
this.instance = new Logger(opts);
}
this.instance.isDebugEnabled = opts?.enableDebug ?? false;
return this.instance;
}
getTimestamp() {
return dayjs().toISOString();
}
toggleDebug(opts) {
this.isDebugEnabled = opts?.state || !this.isDebugEnabled;
}
generateLog(opts) {
const { level, message, args } = opts;
const timestamp = this.getTimestamp();
switch (typeof message) {
case 'string': {
return {
message: `${timestamp} - [${level}] ${message}`,
args,
};
}
default: {
return {
message: `${timestamp} - [${level}]`,
args: [message, ...args],
};
}
}
}
log(level, message, ...args) {
if (!applicationLogger) {
throw getError({ message: '[info] Invalid logger instance!' });
}
if (level === 'debug' && !this.isDebugEnabled) {
return;
}
const generated = this.generateLog({ level, message, args });
applicationLogger.info(generated.message, ...generated.args);
}
debug(message, ...args) {
this.log('debug', message, ...args);
}
info(message, ...args) {
this.log('info', message, ...args);
}
warn(message, ...args) {
this.log('warn', message, ...args);
}
error(message, ...args) {
this.log('error', message, ...args);
}
}
//# sourceMappingURL=logger.helper.js.map