Skip to content

Commit

Permalink
[5.4.3] Made console logs more clean (#198)
Browse files Browse the repository at this point in the history
Now console logs look as follows:

```
[10.01.2024 - 21:18]         | Spawned shard 0
[10.01.2024 - 21:18] [WARN ] | Bot running on OPLevel: 1
[10.01.2024 - 21:18] [WARN ] | Debug mode is [ENABLED]!
[10.01.2024 - 21:18] [WARN ] | Booting up the bot...
                               DiscordMusicBot/v5.4.1 (ID: {redacted})
[10.01.2024 - 21:18] [WARN ] | Loading error handlers...
[10.01.2024 - 21:18] [INFO ] | Loaded debug error handlers!
[10.01.2024 - 21:18] [DEBUG] | Music engine "Erela" has been loaded
[10.01.2024 - 21:18] [DEBUG] | Prisma ORM has been loaded
[10.01.2024 - 21:18] [INFO ] | Slash commands have been loaded. Waiting for bot                                                                                                                                                                                                 to finish initializing...
[10.01.2024 - 21:18] [INFO ] | Schedules have been loaded.
[10.01.2024 - 21:18] [INFO ] | Event listeners have been loaded.
[10.01.2024 - 21:18] [INFO ] | WS is now listening on port 3201
[10.01.2024 - 21:18] [INFO ] | Successfully logged in as Arch Music Bot#0893
[10.01.2024 - 21:18] [INFO ] | API is now listening on port 3200
[10.01.2024 - 21:18] [DEBUG] | Node: LocalNode | Lavalink node is connected.
[10.01.2024 - 21:18] [INFO ] | Slash commands have been pushed to application
[10.01.2024 - 21:18] [SILLY] | DiscordMusicBot/v5.4.1 (ID: {redacted})                                                                                                                                                                                                 is online!
prisma:info Starting a postgresql pool with 9 connections.
[11.01.2024 - 00:00] [INFO ] | 'logs.log' has been purged.
```


## Status and versioning classification:

- Code changes have been tested against the Discord API, or there are no
code changes
- I know how to update typings and have done so, or typings don't need
updating
  • Loading branch information
brianferri authored Jan 12, 2024
2 parents dd185a2 + 08322a9 commit ad2351d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
5 changes: 2 additions & 3 deletions djs-bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
const colors = require("colors");
const getConfig = require("./util/getConfig");
const { ShardingManager } = require('discord.js');
const { getCurrentTimeString } = require("./util/dates");

try {
// Gets the config file and passes it (as if returned) to the function in `.then( () => {} )`
getConfig().then((conf) => {
const manager = new ShardingManager('./bot.js', { token: conf.token, respawn: true, totalShards: "auto", timeout: -1 });
manager.on('shardCreate', shard => {
let d = new Date();
let time = `[${d.getDate()}:${d.getMonth()}:${d.getFullYear()} - ${d.getHours()}:${d.getMinutes()}]`;
console.log(colors.gray(time) + colors.cyan(" | " + `Spawned shard ${shard.id}`));
console.log(colors.gray(getCurrentTimeString()) + colors.cyan(`${' '.repeat(9)}| Spawned shard ${shard.id}`));
});
manager.spawn({ amount: "auto", delay: 5500, timeout: -1 }).catch((err) => {
console.log(colors.red("\tError spawning shards: " + err));
Expand Down
2 changes: 1 addition & 1 deletion djs-bot/lib/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Bot extends Client {
this.LoadSchedules();
this.LoadEvents();

this.warn("Booting up the bot...\n\t" + this.denom);
this.warn("Booting up the bot...\n" + ' '.repeat(31) + this.denom);

LoadErrorHandler(this);
LoadDebugListeners(this);
Expand Down
14 changes: 4 additions & 10 deletions djs-bot/lib/Logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const winston = require("winston");
const colors = require("colors");
const { getCurrentTimeString } = require("../util/dates");

const map = {
log: { level: "debug", message: "info: ", color: "green" },
Expand All @@ -22,20 +23,12 @@ class Logger {
});
}

/**
* @returns String formatted date for logging
*/
getCurrentTimeString() {
let d = new Date();
return `[${d.getDate()}:${d.getMonth()}:${d.getFullYear()} - ${d.getHours()}:${d.getMinutes()}]`;
}

/**
* @param {Formatting} formatting
* @param {...any} args
*/
printLog(formatting, ...args) {
const time = this.getCurrentTimeString();
const time = getCurrentTimeString();

this.logger.log({
time: `${time}`,
Expand All @@ -45,7 +38,8 @@ class Logger {

if (formatting.color) {
console.log(
colors.gray(time) + colors[formatting.color || "green"](` [${formatting.level.toUpperCase()}] | `) + args.join(" ")
colors.gray(time) +
colors[formatting.color || "green"](` [${formatting.level.toUpperCase().padEnd(5, ' ')}] | `) + args.join(" ")
);
}
}
Expand Down
27 changes: 26 additions & 1 deletion djs-bot/util/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,32 @@ function thisWeek(begOffset = { days: 0 }, endOffset = { days: 0 }) {
return dateSpan("week", "week", begOffset, endOffset);
}

/**
* Returns formatted time string
*
* @example [10.01.2024 - 21:14]
*/
function getCurrentTimeString() {
const date = new Date();

const formatNumber = (num) => num.toLocaleString('en-US', {
minimumIntegerDigits: 2,
useGrouping: false
});

const formattedDate = {
date: formatNumber(date.getDate()),
month: formatNumber(date.getMonth() + 1),
year: date.getFullYear(),
hours: formatNumber(date.getHours()),
minutes: formatNumber(date.getMinutes()),
}

return `[${formattedDate.date}.${formattedDate.month}.${formattedDate.year} - ${formattedDate.hours}:${formattedDate.minutes}]`;
}

module.exports = {
thisWeek,
dateSpan,
};
getCurrentTimeString,
};

0 comments on commit ad2351d

Please sign in to comment.