From 1c3574f6f6078364a67df9f9f5c8d464a536a025 Mon Sep 17 00:00:00 2001 From: Maseshi Date: Fri, 18 Mar 2022 12:56:02 +0700 Subject: [PATCH] Fixed a bug in the log file generator. --- source/events/client/debug.js | 8 ++++++-- source/events/client/error.js | 10 ++++++---- source/events/client/warn.js | 10 ++++++---- source/handlers/process.js | 22 +++++++++++++++++----- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/source/events/client/debug.js b/source/events/client/debug.js index ce23cd77..62f64525 100644 --- a/source/events/client/debug.js +++ b/source/events/client/debug.js @@ -1,4 +1,4 @@ -const { createWriteStream } = require("fs"); +const { createWriteStream, existsSync, mkdirSync } = require("fs"); const { format } = require("util"); module.exports = (info) => { @@ -6,7 +6,11 @@ module.exports = (info) => { const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDay(); - const file = createWriteStream(__dirname + "../../../logs/debug_" + year + "-" + month + "-" + day + ".log", { "flags" : "w" }); + const dir = "./source/logs/"; + + if (!existsSync(dir)) mkdirSync(dir); + + const file = createWriteStream("./source/logs/debug_" + year + "-" + month + "-" + day + ".log", { "flags" : "w" }); file.write(format(info) + "\n"); }; \ No newline at end of file diff --git a/source/events/client/error.js b/source/events/client/error.js index a18db12d..30bb10a5 100644 --- a/source/events/client/error.js +++ b/source/events/client/error.js @@ -1,14 +1,16 @@ -const { createWriteStream } = require("fs"); +const { createWriteStream, existsSync, mkdirSync } = require("fs"); const { format } = require("util"); module.exports = (error) => { const date = new Date(); - const minutes = date.getMinutes(); - const hours = date.getHours(); const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDay(); - const file = createWriteStream(__dirname + "../../../logs/error_" + year + "-" + month + "-" + day + "_" + hours + "-" + minutes + ".log", { "flags" : "w" }); + const dir = "./source/logs/"; + + if (!existsSync(dir)) mkdirSync(dir); + + const file = createWriteStream("./source/logs/error_" + year + "-" + month + "-" + day + ".log", { "flags" : "w" }); file.write(format(error) + "\n"); }; \ No newline at end of file diff --git a/source/events/client/warn.js b/source/events/client/warn.js index 0e039c98..1b7eba9a 100644 --- a/source/events/client/warn.js +++ b/source/events/client/warn.js @@ -1,14 +1,16 @@ -const { createWriteStream } = require("fs"); +const { createWriteStream, existsSync, mkdirSync } = require("fs"); const { format } = require("util"); module.exports = (info) => { const date = new Date(); - const minutes = date.getMinutes(); - const hours = date.getHours(); const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDay(); - const file = createWriteStream(__dirname + "../../../logs/warn_" + year + "-" + month + "-" + day + "_" + hours + "-" + minutes + ".log", { "flags" : "w" }); + const dir = "./source/logs/"; + + if (!existsSync(dir)) mkdirSync(dir); + + const file = createWriteStream("./source/logs/warn_" + year + "-" + month + "-" + day + ".log", { "flags" : "w" }); file.write(format(info) + "\n"); }; \ No newline at end of file diff --git a/source/handlers/process.js b/source/handlers/process.js index f73a92a5..f9760390 100644 --- a/source/handlers/process.js +++ b/source/handlers/process.js @@ -1,6 +1,6 @@ const discord = require("discord.js"); const packages = require("../../package.json"); -const { createWriteStream } = require("fs"); +const { createWriteStream, existsSync, mkdirSync } = require("fs"); const { format } = require("util"); module.exports = (client) => { @@ -25,7 +25,11 @@ module.exports = (client) => { }; process.on("unhandledRejection", (reason, promise) => { - const file = createWriteStream(__dirname + "../../logs/process" + logDateTime(new Date()) + ".log", { "flags" : "w" }); + const dir = "./source/logs/"; + + if (!existsSync(dir)) mkdirSync(dir); + + const file = createWriteStream("./source/logs/process" + logDateTime(new Date()) + ".log", { "flags" : "w" }); console.group(consoleDateTime(new Date()) + " :: \u001b[41;1mUnhandled Rejection/Catch\u001b[0m"); console.group("\u001b[1mFull Error:\u001b[0m "); @@ -38,12 +42,16 @@ module.exports = (client) => { console.info("\u001b[1mDiscord.js:\u001b[0m v" + discord.version); console.info("\u001b[1mNode.js:\u001b[0m " + process.version); console.groupEnd(); - + file.write(format(reason, promise) + "\n"); }); process.on("uncaughtException", (err, origin) => { - const file = createWriteStream(__dirname + "../../logs/" + year + "-" + month + "-" + day + "-process.log", { "flags" : "w" }); + const dir = "./source/logs/"; + + if (!existsSync(dir)) mkdirSync(dir); + + const file = createWriteStream("./source/logs/process" + logDateTime(new Date()) + ".log", { "flags" : "w" }); console.group(consoleDateTime(new Date()) + " :: \u001b[41;1mUncaught Exception/Catch\u001b[0m"); console.group("\u001b[1mFull Error:\u001b[0m "); @@ -61,7 +69,11 @@ module.exports = (client) => { }); process.on("multipleResolves", (type, promise, reason) => { - const file = createWriteStream(__dirname + "../../logs/" + year + "-" + month + "-" + day + "-process.log", { "flags" : "w" }); + const dir = "./source/logs/"; + + if (!existsSync(dir)) mkdirSync(dir); + + const file = createWriteStream("./source/logs/process" + logDateTime(new Date()) + ".log", { "flags" : "w" }); switch (reason.toLocaleString()) { case "":