2022-02-09 03:32:03 +08:00
|
|
|
const config = require('../../config');
|
|
|
|
|
2022-12-19 02:43:14 +08:00
|
|
|
const {level} = config.log;
|
2022-02-09 03:32:03 +08:00
|
|
|
const trace = level.toLowerCase() === 'trace';
|
|
|
|
const debug = trace || level.toLowerCase() === 'debug';
|
|
|
|
|
|
|
|
const date = () => new Date().toISOString();
|
|
|
|
|
|
|
|
const parse = (messages) => {
|
2022-12-19 02:43:14 +08:00
|
|
|
return messages.map((message) => {
|
2022-02-09 03:32:03 +08:00
|
|
|
if (typeof message === 'object') return JSON.stringify(message);
|
|
|
|
|
|
|
|
return message;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = class Logger {
|
|
|
|
constructor(context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace(...messages) {
|
|
|
|
if (trace) {
|
|
|
|
console.log(date(), 'TRACE\t', `[${this.context}]`, ...parse(messages));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(...messages) {
|
|
|
|
if (debug) {
|
|
|
|
console.log(date(), 'DEBUG\t', `[${this.context}]`, ...parse(messages));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info(...messages) {
|
|
|
|
console.log(date(), 'INFO\t', `[${this.context}]`, ...parse(messages));
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(...messages) {
|
|
|
|
if (debug) {
|
|
|
|
console.log(date(), 'WARN\t', `[${this.context}]`, ...parse(messages));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error(...messages) {
|
|
|
|
console.log(date(), 'ERROR\t', `[${this.context}]`, ...parse(messages));
|
|
|
|
}
|
|
|
|
|
|
|
|
fatal(...messages) {
|
|
|
|
console.log(date(), 'FATAL\t', `[${this.context}]`, ...parse(messages));
|
|
|
|
}
|
|
|
|
};
|