Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logger #264

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"prefer-const": ["warn", { "destructuring": "all" }],
"no-constant-condition": ["error", { "checkLoops": false }],
"import/extensions": ["warn", "always", { "ts": "never" }],
"no-throw-literal": "error"
"no-throw-literal": "error",
"no-console": "error"
}
}
3 changes: 2 additions & 1 deletion API/stats/bestiary.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { errorMessage } = require("../../src/Logger.js");
const constants = require("../constants/bestiary.js");

function formatBestiaryMobs(userProfile, mobs) {
Expand Down Expand Up @@ -72,7 +73,7 @@ function getBestiary(userProfile) {
maxMilestone: totalTiers / 10,
};
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion API/stats/chocolateFactory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// CREDITS: by @Kathund (https://github.com/Kathund)
const { errorMessage } = require("../../src/Logger.js");

module.exports = (profile) => {
try {
Expand All @@ -18,7 +19,7 @@ module.exports = (profile) => {
level: profile.events?.easter?.chocolate_level || 0,
};
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
};
4 changes: 2 additions & 2 deletions API/stats/crimson.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CREDITS: by @Kathund (https://github.com/Kathund)

const { titleCase } = require("../constants/functions.js");
const { errorMessage } = require("../../src/Logger.js");

module.exports = (profile) => {
try {
Expand Down Expand Up @@ -62,7 +62,7 @@ module.exports = (profile) => {
trophyFishing: getTrophyFish(profile),
};
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
};
Expand Down
3 changes: 2 additions & 1 deletion API/stats/dungeons.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const calcSkill = require("../constants/skills.js");
const { titleCase } = require("../constants/functions.js");
const { errorMessage } = require("../../src/Logger.js");

module.exports = (profile) => {
try {
Expand Down Expand Up @@ -95,7 +96,7 @@ module.exports = (profile) => {
},
};
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
};
Expand Down
5 changes: 3 additions & 2 deletions API/stats/hotm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// CREDITS: by @Kathund (https://github.com/Kathund)
const { titleCase } = require("../constants/functions.js");
const { errorMessage } = require("../../src/Logger.js");
const miningConst = require("../constants/mining.js");
const calcSkill = require("../constants/skills.js");
const moment = require("moment");
Expand Down Expand Up @@ -51,7 +52,7 @@ module.exports = (player, profile) => {
forgeItem.timeFinishedText =
timeFinished < Date.now() ? "Finished" : `ending ${moment(timeFinished).fromNow()}`;
} else {
console.log(item);
errorMessage(item);
forgeItem.name = "Unknown Item";
forgeItem.id = `UNKNOWN-${item.id}`;
}
Expand Down Expand Up @@ -84,7 +85,7 @@ module.exports = (player, profile) => {
forge: forgeItems,
};
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
};
3 changes: 2 additions & 1 deletion API/stats/talismans.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { decodeData } = require("../../src/contracts/helperFunctions.js");
const { titleCase } = require("../constants/functions.js");
const { errorMessage } = require("../../src/Logger.js");

module.exports = async (profile) => {
try {
Expand Down Expand Up @@ -75,7 +76,7 @@ module.exports = async (profile) => {
return null;
}
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
};
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
process.on("uncaughtException", (error) => console.log(error));
const app = require("./src/Application.js");

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"scripts": {
"start": "node index.js",
"test": "jest --detectOpenHandles --forceExit",
"lint": "npx eslint .",
"lint:fix": "npx eslint . --fix",
"prettier": "npx prettier --check .",
"prettier:fix": "npx prettier --write .",
"lint": "npx eslint src/",
"lint:fix": "npx eslint src/ --fix",
"prettier": "npx prettier src/ --check",
"prettier:fix": "npx prettier src/ --write",
"nodemon": "nodemon index"
},
"repository": {
Expand Down
27 changes: 14 additions & 13 deletions src/Logger.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-console */
const customLevels = { discord: 0, minecraft: 1, web: 2, warn: 3, error: 4, broadcast: 5, max: 6 };
const { createLogger, format, transports } = require("winston");
const config = require("../config.json");
const chalk = require("chalk");

const discordTransport = new transports.File({ level: "discord", filename: "./logs/discord.log" });
const minecraftTransport = new transports.File({ level: "minecraft", filename: "./logs/minecraft.log" });
const webTransport = new transports.File({ level: "web", filename: "./logs/web.log" });
Expand All @@ -18,7 +18,7 @@ const discordLogger = createLogger({
format.timestamp({ format: getCurrentTime }),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level.toUpperCase()} > ${message}`;
})
}),
),
transports: [discordTransport, combinedTransport],
});
Expand All @@ -30,7 +30,7 @@ const minecraftLogger = createLogger({
format.timestamp({ format: getCurrentTime }),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level.toUpperCase()} > ${message}`;
})
}),
),
transports: [minecraftTransport, combinedTransport],
});
Expand All @@ -42,7 +42,7 @@ const webLogger = createLogger({
format.timestamp({ format: getCurrentTime }),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level.toUpperCase()} > ${message}`;
})
}),
),
transports: [webTransport, combinedTransport],
});
Expand All @@ -54,7 +54,7 @@ const warnLogger = createLogger({
format.timestamp({ format: getCurrentTime }),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level.toUpperCase()} > ${message}`;
})
}),
),
transports: [warnTransport, combinedTransport],
});
Expand All @@ -66,7 +66,7 @@ const errorLogger = createLogger({
format.timestamp({ format: getCurrentTime }),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level.toUpperCase()} > ${message}`;
})
}),
),
transports: [errorTransport, combinedTransport],
});
Expand All @@ -78,7 +78,7 @@ const broadcastLogger = createLogger({
format.timestamp({ format: getCurrentTime }),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level.toUpperCase()} > ${message}`;
})
}),
),
transports: [broadcastTransport, combinedTransport],
});
Expand Down Expand Up @@ -115,12 +115,13 @@ function warnMessage(message) {
return console.log(chalk.bgYellow.black(`[${getCurrentTime()}] Warning >`) + " " + chalk.yellow(message));
}

function errorMessage(message) {
function errorMessage(error) {
const errorString = `${error.toString()}${error.stack?.replace(error.toString(), "")}`;
if (config.other.logToFiles) {
errorLogger.log("error", message);
errorLogger.log("error", errorString);
}
return console.log(chalk.bgRedBright.black(`[${getCurrentTime()}] Error >`) + " " + chalk.redBright(message));

return console.log(chalk.bgRedBright.black(`[${getCurrentTime()}] Error >`) + " " + chalk.redBright(errorString));
}

function broadcastMessage(message, location) {
Expand Down Expand Up @@ -156,8 +157,8 @@ async function configUpdateMessage(message) {
console.log();
console.log(
`${chalk.bgRedBright.black(`[${getCurrentTime()}] Config Update >`)} ${chalk.redBright("Added")} ${chalk.gray(
message
)} ${chalk.redBright("to config.json")}`
message,
)} ${chalk.redBright("to config.json")}`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function updateCode() {

exec("git pull", (error, stdout, stderr) => {
if (error) {
console.error(`Git pull error: ${error}`);
Logger.errorMessage(error);
return;
}

Expand Down
7 changes: 4 additions & 3 deletions src/contracts/API/mowojangAPI.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { errorMessage } = require("../../Logger.js");
const axios = require("axios");

const uuidCache = new Map();
Expand Down Expand Up @@ -28,7 +29,7 @@ async function getUUID(username) {
} catch (error) {
// eslint-disable-next-line no-throw-literal
if (error.response.data === "Not found") throw "Invalid username.";
console.log(error);
errorMessage(error);
throw error;
}
}
Expand Down Expand Up @@ -57,7 +58,7 @@ async function getUsername(uuid) {

return data.name;
} catch (error) {
console.log(error);
errorMessage(error);
// eslint-disable-next-line no-throw-literal
if (error.response?.data === "Not found") throw "Invalid UUID.";
throw error;
Expand All @@ -75,7 +76,7 @@ async function resolveUsernameOrUUID(username) {
} catch (error) {
// eslint-disable-next-line no-throw-literal
if (error.response.data === "Not found") throw "Invalid Username Or UUID.";
console.log(error);
errorMessage(error);
throw error;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/discord/CommandHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint-disable-next-line import/extensions
const { Routes } = require("discord-api-types/v9");
const { errorMessage } = require("../Logger.js");
const config = require("../../config.json");
const { REST } = require("@discordjs/rest");
const fs = require("fs");
Expand All @@ -26,7 +27,7 @@ class CommandHandler {

rest
.put(Routes.applicationGuildCommands(clientID, config.discord.bot.serverID), { body: commands })
.catch(console.error);
.catch((e) => errorMessage(e));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/discord/commands/verifyCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const hypixelRebornAPI = require("../../contracts/API/HypixelRebornAPI.js");
const { writeFileSync, readFileSync } = require("fs");
const config = require("../../../config.json");
const { EmbedBuilder } = require("discord.js");
const { errorMessage } = require("../../Logger.js");

module.exports = {
name: "verify",
Expand Down Expand Up @@ -94,7 +95,7 @@ module.exports = {

await updateRolesCommand.execute(interaction, user);
} catch (error) {
console.log(error);
errorMessage(error);
// eslint-disable-next-line no-ex-assign
error = error
.toString()
Expand Down Expand Up @@ -133,7 +134,7 @@ module.exports = {
iconURL: "https://i.imgur.com/uUuZx2E.png",
});

await interaction.followUp({ embeds: [verificationTutorialEmbed], ephemeral: true });
await interaction.followUp({ embeds: [verificationTutorialEmbed], ephemeral: true });
}
}
},
Expand Down
5 changes: 2 additions & 3 deletions src/discord/events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
if (command === undefined) {
return;
}

Logger.discordMessage(`${interaction.user.username} - [${interaction.commandName}]`);

if (command.verificationCommand === true && config.verification.enabled === false) {
Expand All @@ -41,8 +41,7 @@ module.exports = {
await command.execute(interaction);
}
} catch (error) {
console.log(error);

Logger.errorMessage(error);
const errrorMessage =
error instanceof HypixelDiscordChatBridgeError
? ""
Expand Down
5 changes: 3 additions & 2 deletions src/discord/handlers/MessageHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { uploadImage } = require("../../contracts/API/imgurAPI.js");
const { demojify } = require("discord-emoji-converter");
const { errorMessage } = require("../../Logger.js");
const config = require("../../../config.json");

class MessageHandler {
Expand Down Expand Up @@ -68,7 +69,7 @@ class MessageHandler {

this.discord.broadcastMessage(messageData);
} catch (error) {
console.log(error);
errorMessage(error);
}
}

Expand Down Expand Up @@ -111,7 +112,7 @@ class MessageHandler {

return mentionedUserName ?? null;
} catch (error) {
console.log(error);
errorMessage(error);
return null;
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/discord/other/updateUsers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const updateRolesCommand = require("../commands/forceUpdateEveryone.js");
const config = require("../../../config.json");
const Logger = require("../../Logger.js");
const cron = require("node-cron");
if (config.verification.autoUpdater) {
Logger.discordMessage(`RoleSync ready, executing every ${config.verification.autoUpdaterInterval} hours.`);
cron.schedule(`0 */${config.verification.autoUpdaterInterval} * * *`, async () => {
Logger.discordMessage("Executing RoleSync...");
await updateRolesCommand.execute(null, true);
Logger.discordMessage("RoleSync successfully executed.");
});
}
const updateRolesCommand = require("../commands/forceUpdateEveryone.js");
const config = require("../../../config.json");
const Logger = require("../../Logger.js");
const cron = require("node-cron");

if (config.verification.autoUpdater) {
Logger.discordMessage(`RoleSync ready, executing every ${config.verification.autoUpdaterInterval} hours.`);
cron.schedule(`0 */${config.verification.autoUpdaterInterval} * * *`, async () => {
Logger.discordMessage("Executing RoleSync...");
await updateRolesCommand.execute(null, true);
Logger.discordMessage("RoleSync successfully executed.");
});
}
Loading
Loading