2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-11-17 15:29:30 +00:00
RTL/server/utils/logger.ts

84 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-12-29 23:08:41 +00:00
/* eslint-disable no-console */
import * as fs from 'fs';
import { LogJSONObj } from '../models/config.model.js';
export class LoggerService {
log = (msgJSON: LogJSONObj) => {
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 {
msgStr = msgStr + '.\r\n';
2021-12-29 23:08:41 +00:00
}
console.error(msgStr);
2024-04-23 01:24:58 +00:00
if (msgJSON.selectedNode && msgJSON.selectedNode.Settings.logFile) {
fs.appendFile(msgJSON.selectedNode.Settings.logFile, msgStr, () => { });
2021-12-29 23:08:41 +00:00
}
break;
case 'WARN':
msgStr = prepMsgData(msgJSON, msgStr);
2024-04-23 01:24:58 +00:00
if (!msgJSON.selectedNode || msgJSON.selectedNode.Settings.logLevel === 'WARN' || msgJSON.selectedNode.Settings.logLevel === 'INFO' || msgJSON.selectedNode.Settings.logLevel === 'DEBUG') {
if (msgJSON.selectedNode && msgJSON.selectedNode.Settings.logFile) {
fs.appendFile(msgJSON.selectedNode.Settings.logFile, msgStr, () => { });
2021-12-29 23:08:41 +00:00
}
}
break;
case 'INFO':
if (!msgJSON.selectedNode && msgJSON.fileName === 'RTL') {
console.log(msgStr + '.\r\n');
2024-04-23 01:24:58 +00:00
} else if (msgJSON.selectedNode && msgJSON.selectedNode.Settings.logLevel === 'INFO') {
msgStr = msgStr + '.\r\n';
console.log(msgStr);
2024-04-23 01:24:58 +00:00
if (msgJSON.selectedNode.Settings.logFile) {
fs.appendFile(msgJSON.selectedNode.Settings.logFile, msgStr, () => { });
2021-12-29 23:08:41 +00:00
}
2024-04-23 01:24:58 +00:00
} else if (msgJSON.selectedNode && msgJSON.selectedNode.Settings.logLevel === 'DEBUG') {
msgStr = prepMsgData(msgJSON, msgStr);
2021-12-29 23:08:41 +00:00
console.log(msgStr);
2024-04-23 01:24:58 +00:00
if (msgJSON.selectedNode.Settings.logFile) {
fs.appendFile(msgJSON.selectedNode.Settings.logFile, msgStr, () => { });
}
}
break;
case 'DEBUG':
if (!msgJSON.selectedNode) {
console.log(msgStr + '.\r\n');
2024-04-23 01:24:58 +00:00
} else if (msgJSON.selectedNode && msgJSON.selectedNode.Settings.logLevel === 'DEBUG') {
msgStr = prepMsgData(msgJSON, msgStr);
console.log(msgStr);
2024-04-23 01:24:58 +00:00
if (msgJSON.selectedNode.Settings.logFile) {
fs.appendFile(msgJSON.selectedNode.Settings.logFile, msgStr, () => { });
2021-12-29 23:08:41 +00:00
}
}
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 {
msgStr = msgStr + '.\r\n';
2021-12-29 23:08:41 +00:00
}
return msgStr;
};
export const Logger = new LoggerService();