add special redis log type

This commit is contained in:
Holly Stubbs 2022-05-10 12:35:36 +01:00
parent 6f5312002e
commit d5fb2261a8
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 59 additions and 29 deletions

View file

@ -1,33 +1,63 @@
const chalk = require("chalk");
module.exports = {
printWebReq:function(s) {
console.log(`${this.getTime()} ${chalk.bgGreen(chalk.black(" WEBREQ "))} ${s}`);
},
const LogType = {
INFO: 0,
WARN: 1,
ERROR: 2
}
printBancho:function(s) {
console.log(`${this.getTime()} ${chalk.bgMagenta(chalk.black(" BANCHO "))} ${s}`);
},
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);
}
}

View file

@ -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!");