add special redis log type
This commit is contained in:
parent
6f5312002e
commit
d5fb2261a8
2 changed files with 59 additions and 29 deletions
|
@ -1,33 +1,63 @@
|
||||||
const chalk = require("chalk");
|
const chalk = require("chalk");
|
||||||
|
|
||||||
module.exports = {
|
const LogType = {
|
||||||
printWebReq:function(s) {
|
INFO: 0,
|
||||||
console.log(`${this.getTime()} ${chalk.bgGreen(chalk.black(" WEBREQ "))} ${s}`);
|
WARN: 1,
|
||||||
},
|
ERROR: 2
|
||||||
|
}
|
||||||
|
|
||||||
printBancho:function(s) {
|
const LogTags = {
|
||||||
console.log(`${this.getTime()} ${chalk.bgMagenta(chalk.black(" BANCHO "))} ${s}`);
|
BANCHO: chalk.bgMagenta(chalk.black(" BANCHO ")),
|
||||||
},
|
WEBREQ: chalk.bgGreen(chalk.black(" WEBREQ ")),
|
||||||
|
CHAT: chalk.bgCyan(chalk.black(" CHATTO ")),
|
||||||
printChat:function(s) {
|
WARN: chalk.bgYellow(chalk.black(" WARNIN ")),
|
||||||
console.log(`${this.getTime()} ${chalk.bgCyan(chalk.black(" CHATTO "))} ${s}`);
|
ERROR: chalk.bgRed(" ERROR! "),
|
||||||
},
|
REDIS: chalk.bgRed(chalk.white(" bREDIS "))
|
||||||
|
|
||||||
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())}]`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function correctValue(i) {
|
function correctValue(i) {
|
||||||
if (i <= 9) return "0"+i;
|
if (i <= 9) return "0"+i;
|
||||||
else return 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
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)
|
// Dup and connect new client for channel subscription (required)
|
||||||
const scoreSubmitUpdateClient = global.promClient.duplicate();
|
const scoreSubmitUpdateClient = global.promClient.duplicate();
|
||||||
await scoreSubmitUpdateClient.connect();
|
await scoreSubmitUpdateClient.connect();
|
||||||
// Subscribe to channel
|
// Subscribe to channel
|
||||||
await scoreSubmitUpdateClient.subscribe(channelName, callback);
|
await scoreSubmitUpdateClient.subscribe(channelName, callback);
|
||||||
consoleHelper.printBancho(`Subscribed to ${channelName} channel`);
|
consoleHelper.printRedis(`Subscribed to ${channelName} channel`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do redis if it's enabled
|
// 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}`
|
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();
|
const connectionStartTime = Date.now();
|
||||||
await global.promClient.connect();
|
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
|
// Score submit update channel
|
||||||
subscribeToChannel("binato:update_user_stats", (message) => {
|
subscribeToChannel("binato:update_user_stats", (message) => {
|
||||||
|
@ -53,7 +53,7 @@ if (config.redis.enabled) {
|
||||||
// Update user info
|
// Update user info
|
||||||
user.updateUserInfo(true);
|
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!");
|
} else consoleHelper.printWarn("Redis is disabled!");
|
||||||
|
|
Loading…
Reference in a new issue