bigbluebutton-Github/bbb-export-annotations/lib/utils/logger.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

import fs from 'fs';
const config = JSON.parse(fs.readFileSync('./config/settings.json', 'utf8'));
2022-02-09 03:32:03 +08:00
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;
});
};
export default class Logger {
2022-02-09 03:32:03 +08:00
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));
}
};