2021-12-29 23:08:41 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
import * as fs from 'fs';
|
|
|
|
export class LoggerService {
|
|
|
|
constructor() {
|
|
|
|
this.log = (msgJSON) => {
|
2022-01-16 20:55:50 +00:00
|
|
|
let msgStr = '[' + new Date().toLocaleString() + '] ' + msgJSON.level + ': ' + msgJSON.fileName + ' => ' + msgJSON.msg;
|
2021-12-29 23:08:41 +00:00
|
|
|
switch (msgJSON.level) {
|
|
|
|
case 'ERROR':
|
|
|
|
if (msgJSON.error) {
|
2022-11-15 20:55:44 +00:00
|
|
|
msgStr = msgStr + ': ' + ((msgJSON.error.error && msgJSON.error.error.message && typeof msgJSON.error.error.message === 'string') ?
|
|
|
|
msgJSON.error.error.message : (typeof msgJSON.error === 'object' && msgJSON.error.message && typeof msgJSON.error.message === 'string') ? msgJSON.error.message : (typeof msgJSON.error === 'object' && msgJSON.error.stack && typeof msgJSON.error.stack === 'string') ?
|
|
|
|
msgJSON.error.stack : (typeof msgJSON.error === 'object') ? JSON.stringify(msgJSON.error) : (typeof msgJSON.error === 'string') ?
|
|
|
|
msgJSON.error : '') + '\r\n';
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-01-16 20:55:50 +00:00
|
|
|
msgStr = msgStr + '.\r\n';
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
|
|
|
console.error(msgStr);
|
|
|
|
if (msgJSON.selectedNode && msgJSON.selectedNode.log_file) {
|
|
|
|
fs.appendFile(msgJSON.selectedNode.log_file, msgStr, () => { });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'WARN':
|
2022-01-16 20:55:50 +00:00
|
|
|
msgStr = prepMsgData(msgJSON, msgStr);
|
|
|
|
if (!msgJSON.selectedNode || msgJSON.selectedNode.log_level === 'WARN' || msgJSON.selectedNode.log_level === 'INFO' || msgJSON.selectedNode.log_level === 'DEBUG') {
|
2021-12-29 23:08:41 +00:00
|
|
|
if (msgJSON.selectedNode && msgJSON.selectedNode.log_file) {
|
|
|
|
fs.appendFile(msgJSON.selectedNode.log_file, msgStr, () => { });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'INFO':
|
2022-01-16 20:55:50 +00:00
|
|
|
if (!msgJSON.selectedNode && msgJSON.fileName === 'RTL') {
|
|
|
|
console.log(msgStr + '.\r\n');
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
2022-01-16 20:55:50 +00:00
|
|
|
else if (msgJSON.selectedNode && msgJSON.selectedNode.log_level === 'INFO') {
|
|
|
|
msgStr = msgStr + '.\r\n';
|
|
|
|
console.log(msgStr);
|
|
|
|
if (msgJSON.selectedNode.log_file) {
|
|
|
|
fs.appendFile(msgJSON.selectedNode.log_file, msgStr, () => { });
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
2022-01-16 20:55:50 +00:00
|
|
|
}
|
|
|
|
else if (msgJSON.selectedNode && msgJSON.selectedNode.log_level === 'DEBUG') {
|
|
|
|
msgStr = prepMsgData(msgJSON, msgStr);
|
|
|
|
console.log(msgStr);
|
|
|
|
if (msgJSON.selectedNode.log_file) {
|
|
|
|
fs.appendFile(msgJSON.selectedNode.log_file, msgStr, () => { });
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
2022-01-16 20:55:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'DEBUG':
|
|
|
|
if (!msgJSON.selectedNode) {
|
|
|
|
console.log(msgStr + '.\r\n');
|
|
|
|
}
|
|
|
|
else if (msgJSON.selectedNode && msgJSON.selectedNode.log_level === 'DEBUG') {
|
|
|
|
msgStr = prepMsgData(msgJSON, msgStr);
|
2021-12-29 23:08:41 +00:00
|
|
|
console.log(msgStr);
|
2022-01-16 20:55:50 +00:00
|
|
|
if (msgJSON.selectedNode.log_file) {
|
2021-12-29 23:08:41 +00:00
|
|
|
fs.appendFile(msgJSON.selectedNode.log_file, msgStr, () => { });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log(msgStr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
const prepMsgData = (msgJSON, msgStr) => {
|
|
|
|
if (msgJSON.data) {
|
2022-11-15 20:55:44 +00:00
|
|
|
msgStr = msgStr + ': ' + (typeof msgJSON.data === 'object' ? (msgJSON.data.message && typeof msgJSON.data.message === 'string') ?
|
|
|
|
msgJSON.data.message : (msgJSON.data.stack && typeof msgJSON.data.stack === 'string') ?
|
|
|
|
msgJSON.data.stack : JSON.stringify(msgJSON.data) : (typeof msgJSON.data === 'string') ? msgJSON.data : '') + '\r\n';
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-01-16 20:55:50 +00:00
|
|
|
msgStr = msgStr + '.\r\n';
|
2021-12-29 23:08:41 +00:00
|
|
|
}
|
|
|
|
return msgStr;
|
|
|
|
};
|
|
|
|
export const Logger = new LoggerService();
|