Skip to content

Commit

Permalink
[Task] #41 prepare Log for RateLImit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Feb 10, 2024
1 parent 3a066a5 commit 22f2896
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/scripts/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,23 @@ export default {
}
},
error: (content: string | Response.Error) => {
fs.appendFileSync(logPath, `${date} \t|\t [ERROR]: ${JSON.stringify(content)} \n`);
// logfile
const applyErrorPrefix = !/^\[\w+\]/.test(typeof content == "string" ? content : content.message);
const logMessageTemplate = `${date} \t|\t${applyErrorPrefix ? ' [ERROR]' : ''} ${typeof content == "string" ? content : JSON.stringify(content.message) } \n`;
fs.appendFileSync(logPath, logMessageTemplate);
if (process.env.NODE_ENV == "production") { return; }

// console
if (typeof content != "string" && Object.hasOwnProperty.call(content, "message")) {
const messageAsString = JSON.stringify(content.message);
if (content.stack) { // replace redundant information
content.stack = content.stack.replace(messageAsString,"");
content.stack = content.stack.replace(messageAsString, "");
}
const consoleMessage = structuredClone(content); // create clone so response output is not "further" affected
consoleMessage.message = messageAsString; // gitbash output improvement (w/o objects in arrays appear as [Object])
content = consoleMessage;
}
console.error(content); // log string right away or processed Object

}
}

0 comments on commit 22f2896

Please sign in to comment.