2022-02-27 02:07:39 +00:00
|
|
|
import cluster from 'node:cluster';
|
|
|
|
import chalk from 'chalk';
|
2022-12-13 20:12:29 +00:00
|
|
|
import convertColor from 'color-convert';
|
2022-02-04 01:41:27 +00:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
2022-08-03 11:18:33 +00:00
|
|
|
import config from '@/config/index.js';
|
2023-05-23 17:43:04 +00:00
|
|
|
import { envOption, LOG_LEVELS } from '@/env.js';
|
2022-12-13 20:11:29 +00:00
|
|
|
import type { KEYWORD } from 'color-convert/conversions.js';
|
2022-12-07 19:56:11 +00:00
|
|
|
|
2019-03-02 09:51:59 +00:00
|
|
|
type Domain = {
|
|
|
|
name: string;
|
2022-12-13 20:11:29 +00:00
|
|
|
color?: KEYWORD;
|
2019-03-02 09:51:59 +00:00
|
|
|
};
|
|
|
|
|
2023-05-23 17:43:04 +00:00
|
|
|
export type Level = LOG_LEVELS[keyof LOG_LEVELS];
|
2019-03-02 09:51:59 +00:00
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
2023-05-18 22:59:53 +00:00
|
|
|
* Class that facilitates recording log messages to the console.
|
2022-12-07 19:56:11 +00:00
|
|
|
*/
|
2019-03-02 09:51:59 +00:00
|
|
|
export default class Logger {
|
|
|
|
private domain: Domain;
|
2019-04-12 16:43:22 +00:00
|
|
|
private parentLogger: Logger | null = null;
|
2019-03-02 09:51:59 +00:00
|
|
|
private store: boolean;
|
2023-05-18 19:41:32 +00:00
|
|
|
/**
|
|
|
|
* Messages below this level will be discarded.
|
|
|
|
*/
|
|
|
|
private minLevel: Level;
|
2022-12-07 19:56:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a logger instance.
|
|
|
|
* @param domain Logging domain
|
|
|
|
* @param color Log message color
|
|
|
|
* @param store Whether to store messages
|
|
|
|
*/
|
2023-05-31 09:39:50 +00:00
|
|
|
constructor(domain: string, color?: KEYWORD, store = true, minLevel?: Level) {
|
2019-03-02 09:51:59 +00:00
|
|
|
this.domain = {
|
|
|
|
name: domain,
|
2022-08-03 12:49:55 +00:00
|
|
|
color,
|
2019-03-02 09:51:59 +00:00
|
|
|
};
|
|
|
|
this.store = store;
|
2023-05-31 09:39:50 +00:00
|
|
|
this.minLevel = minLevel ?? envOption.logLevel;
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
|
|
|
* Create a child logger instance.
|
|
|
|
* @param domain Logging domain
|
|
|
|
* @param color Log message color
|
|
|
|
* @param store Whether to store messages
|
|
|
|
* @returns A Logger instance whose parent logger is this instance.
|
|
|
|
*/
|
2023-05-31 09:39:50 +00:00
|
|
|
public createSubLogger(domain: string, color?: KEYWORD, store = true, minLevel?: Level): Logger {
|
2023-05-18 19:41:32 +00:00
|
|
|
const logger = new Logger(domain, color, store, minLevel);
|
2019-03-02 09:51:59 +00:00
|
|
|
logger.parentLogger = this;
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2023-05-18 19:41:32 +00:00
|
|
|
/**
|
|
|
|
* Log a message.
|
|
|
|
* @param level Indicates the level of this particular message. If it is
|
|
|
|
* less than the minimum level configured, the message will be discarded.
|
|
|
|
* @param message The message to be logged.
|
|
|
|
* @param important Whether to highlight this message as especially important.
|
|
|
|
* @param subDomains Names of sub-loggers to be added.
|
|
|
|
*/
|
2023-05-18 20:42:30 +00:00
|
|
|
private log(level: Level, message: string, important = false, subDomains: Domain[] = [], _store = true): void {
|
2023-05-18 19:41:32 +00:00
|
|
|
const store = _store && this.store;
|
2019-03-02 09:51:59 +00:00
|
|
|
|
2023-05-18 19:41:32 +00:00
|
|
|
// Check against the configured log level.
|
|
|
|
if (level < this.minLevel) return;
|
|
|
|
|
|
|
|
// If this logger has a parent logger, delegate the actual logging to it,
|
|
|
|
// so the parent domain(s) will be logged properly.
|
2019-03-02 09:51:59 +00:00
|
|
|
if (this.parentLogger) {
|
2023-05-18 20:42:30 +00:00
|
|
|
this.parentLogger.log(level, message, important, [this.domain].concat(subDomains), store);
|
2019-03-02 09:51:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-04 01:41:27 +00:00
|
|
|
const time = dateFormat(new Date(), 'HH:mm:ss');
|
2022-12-07 19:56:11 +00:00
|
|
|
const worker = cluster.isPrimary ? '*' : cluster.worker?.id;
|
2022-12-13 20:12:29 +00:00
|
|
|
const domains = [this.domain].concat(subDomains).map(d => d.color ? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name) : chalk.white(d.name));
|
2023-05-18 19:41:32 +00:00
|
|
|
|
|
|
|
let levelDisplay;
|
|
|
|
let messageDisplay;
|
|
|
|
switch (level) {
|
2023-05-23 17:43:04 +00:00
|
|
|
case LOG_LEVELS.error:
|
2023-05-18 19:41:32 +00:00
|
|
|
if (important) {
|
|
|
|
levelDisplay = chalk.bgRed.white('ERR ');
|
|
|
|
} else {
|
|
|
|
levelDisplay = chalk.red('ERR ');
|
|
|
|
}
|
|
|
|
messageDisplay = chalk.red(message);
|
|
|
|
break;
|
2023-05-23 17:43:04 +00:00
|
|
|
case LOG_LEVELS.warning:
|
2023-05-18 19:41:32 +00:00
|
|
|
levelDisplay = chalk.yellow('WARN');
|
|
|
|
messageDisplay = chalk.yellow(message);
|
|
|
|
break;
|
2023-05-23 17:43:04 +00:00
|
|
|
case LOG_LEVELS.success:
|
2023-05-18 19:41:32 +00:00
|
|
|
if (important) {
|
|
|
|
levelDisplay = chalk.bgGreen.white('DONE');
|
|
|
|
} else {
|
|
|
|
levelDisplay = chalk.green('DONE');
|
|
|
|
}
|
|
|
|
messageDisplay = chalk.green(message);
|
|
|
|
break;
|
2023-05-23 17:43:04 +00:00
|
|
|
case LOG_LEVELS.info:
|
2023-05-18 19:41:32 +00:00
|
|
|
levelDisplay = chalk.blue('INFO');
|
|
|
|
messageDisplay = message;
|
|
|
|
break;
|
2023-05-23 17:43:04 +00:00
|
|
|
case LOG_LEVELS.debug: default:
|
2023-05-18 19:41:32 +00:00
|
|
|
levelDisplay = chalk.gray('VERB');
|
|
|
|
messageDisplay = chalk.gray(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let log = `${levelDisplay} ${worker}\t[${domains.join(' ')}]\t${messageDisplay}`;
|
2021-10-08 12:24:05 +00:00
|
|
|
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
|
2019-03-02 09:51:59 +00:00
|
|
|
|
|
|
|
console.log(important ? chalk.bold(log) : log);
|
|
|
|
}
|
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
|
|
|
* Log an error message.
|
|
|
|
* Use in situations where execution cannot be continued.
|
|
|
|
* @param err Error or string containing an error message
|
|
|
|
* @param important Whether this error is important
|
|
|
|
*/
|
2023-05-18 20:42:30 +00:00
|
|
|
public error(err: string | Error, important = false): void {
|
2022-12-07 19:56:11 +00:00
|
|
|
if (err instanceof Error) {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.error, err.toString(), important);
|
2022-12-07 19:56:11 +00:00
|
|
|
} else if (typeof err === 'object') {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.error, `${(err as any).message || (err as any).name || err}`, important);
|
2019-03-02 09:51:59 +00:00
|
|
|
} else {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.error, `${err}`, important);
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
|
|
|
* Log a warning message.
|
|
|
|
* Use in situations where execution can continue but needs to be improved.
|
|
|
|
* @param message Warning message
|
|
|
|
* @param important Whether this warning is important
|
|
|
|
*/
|
2023-05-18 20:42:30 +00:00
|
|
|
public warn(message: string, important = false): void {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.warning, message, important);
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
|
|
|
* Log a success message.
|
|
|
|
* Use in situations where something has been successfully done.
|
|
|
|
* @param message Success message
|
|
|
|
* @param important Whether this success message is important
|
|
|
|
*/
|
2023-05-18 20:42:30 +00:00
|
|
|
public succ(message: string, important = false): void {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.success, message, important);
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
|
|
|
* Log a debug message.
|
|
|
|
* Use for debugging (information needed by developers but not required by users).
|
|
|
|
* @param message Debug message
|
|
|
|
* @param important Whether this debug message is important
|
|
|
|
*/
|
2023-05-18 20:42:30 +00:00
|
|
|
public debug(message: string, important = false): void {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.debug, message, important);
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 19:56:11 +00:00
|
|
|
/**
|
|
|
|
* Log an informational message.
|
|
|
|
* Use when something needs to be logged but doesn't fit into other levels.
|
|
|
|
* @param message Info message
|
|
|
|
* @param important Whether this info message is important
|
|
|
|
*/
|
2023-05-18 20:42:30 +00:00
|
|
|
public info(message: string, important = false): void {
|
2023-05-23 17:43:04 +00:00
|
|
|
this.log(LOG_LEVELS.info, message, important);
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
}
|