From d5fb2261a895b64f7b71960cba16d042bd6a3694 Mon Sep 17 00:00:00 2001 From: Holly Date: Tue, 10 May 2022 12:35:36 +0100 Subject: [PATCH] add special redis log type --- consoleHelper.js | 78 ++++++++++++++++++++++++++++------------- server/serverHandler.js | 10 +++--- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/consoleHelper.js b/consoleHelper.js index cf89a90..76de979 100644 --- a/consoleHelper.js +++ b/consoleHelper.js @@ -1,33 +1,63 @@ const chalk = require("chalk"); -module.exports = { - printWebReq:function(s) { - console.log(`${this.getTime()} ${chalk.bgGreen(chalk.black(" WEBREQ "))} ${s}`); - }, - - printBancho:function(s) { - console.log(`${this.getTime()} ${chalk.bgMagenta(chalk.black(" BANCHO "))} ${s}`); - }, +const LogType = { + INFO: 0, + WARN: 1, + ERROR: 2 +} - printChat:function(s) { - console.log(`${this.getTime()} ${chalk.bgCyan(chalk.black(" CHATTO "))} ${s}`); - }, - - printWarn:function(s) { - console.warn(`${this.getTime()} ${chalk.bgYellow(chalk.black(" WARNIN "))} ${chalk.yellow(s)}`); - }, - - printError:function(s) { - console.error(`${this.getTime()} ${chalk.bgRed((" ERROR! "))} ${chalk.red(s)}`); - }, - - getTime:function() { - const time = new Date(); - return chalk.green(`[${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}]`); - } +const LogTags = { + BANCHO: chalk.bgMagenta(chalk.black(" BANCHO ")), + WEBREQ: chalk.bgGreen(chalk.black(" WEBREQ ")), + CHAT: chalk.bgCyan(chalk.black(" CHATTO ")), + WARN: chalk.bgYellow(chalk.black(" WARNIN ")), + ERROR: chalk.bgRed(" ERROR! "), + REDIS: chalk.bgRed(chalk.white(" bREDIS ")) } function correctValue(i) { if (i <= 9) return "0"+i; else return i; +} + +function getTime() { + const time = new Date(); + return chalk.green(`[${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}]`); +} + +function log(tag = "", log = "", logType = LogType.INFO) { + switch (logType) { + case LogType.INFO: + return console.log(`${getTime()} ${tag} ${log}`); + case LogType.WARN: + return console.warn(`${getTime()} ${tag} ${log}`); + case LogType.ERROR: + return console.error(`${getTime()} ${tag} ${log}`); + } +} + +module.exports = { + printWebReq:function(s) { + log(LogTags.WEBREQ, s); + }, + + printBancho:function(s) { + log(LogTags.BANCHO, s); + }, + + printRedis:function(s) { + log(LogTags.REDIS, s); + }, + + printChat:function(s) { + log(LogTags.CHAT, s); + }, + + printWarn:function(s) { + log(LogTags.WARN, chalk.yellow(s), LogType.WARN); + }, + + printError:function(s) { + log(LogTags.ERROR, chalk.red(s), LogType.ERROR); + } } \ No newline at end of file diff --git a/server/serverHandler.js b/server/serverHandler.js index 080c8e6..5d551af 100644 --- a/server/serverHandler.js +++ b/server/serverHandler.js @@ -24,13 +24,13 @@ global.botUser.location[1] = -32; global.DatabaseHelper = new DatabaseHelperClass(config.database.address, config.database.port, config.database.username, config.database.password, config.database.name); -async function subscribeToChannel(channelName = "", callback = function(message) {}) { +async function subscribeToChannel(channelName = "", callback = function(message = "") {}) { // Dup and connect new client for channel subscription (required) const scoreSubmitUpdateClient = global.promClient.duplicate(); await scoreSubmitUpdateClient.connect(); // Subscribe to channel await scoreSubmitUpdateClient.subscribe(channelName, callback); - consoleHelper.printBancho(`Subscribed to ${channelName} channel`); + consoleHelper.printRedis(`Subscribed to ${channelName} channel`); } // Do redis if it's enabled @@ -41,11 +41,11 @@ if (config.redis.enabled) { url: `redis://${config.redis.password.replaceAll(" ", "") == "" ? "" : `${config.redis.password}@`}${config.redis.address}:${config.redis.port}/${config.redis.database}` }); - global.promClient.on('error', e => consoleHelper.printBancho(e)); + global.promClient.on('error', e => consoleHelper.printRedis(e)); const connectionStartTime = Date.now(); await global.promClient.connect(); - consoleHelper.printBancho(`Connected to redis server. Took ${Date.now() - connectionStartTime}ms`); + consoleHelper.printRedis(`Connected to redis server. Took ${Date.now() - connectionStartTime}ms`); // Score submit update channel subscribeToChannel("binato:update_user_stats", (message) => { @@ -53,7 +53,7 @@ if (config.redis.enabled) { // Update user info user.updateUserInfo(true); - consoleHelper.printBancho(`[Redis] Score submission stats update request received for ${user.username}`); + consoleHelper.printRedis(`Score submission stats update request received for ${user.username}`); }); })(); } else consoleHelper.printWarn("Redis is disabled!");