Skip to content

Commit

Permalink
refactor: optimize and improve logger
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed May 10, 2024
1 parent 018e5e6 commit ec1f3f0
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 63 deletions.
4 changes: 0 additions & 4 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"noStaticOnlyClass": "off",
"noThisInStatic": "off"
},
"correctness": {
"noNewSymbol": "error",
"noUndeclaredVariables": "error",
Expand Down
19 changes: 10 additions & 9 deletions src/classes/Application.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import process from 'node:process';
import { Detector, Logger } from '#classes';
import { Detector } from '#classes';
import { env } from '#env';
import {
type ChatInputCommand,
type Command,
type Component,
type ContextMenuCommand,
type Event,
loadDirectory
loadDirectory,
logger
} from '#util';
import { PrismaClient } from '@prisma/client';
import {
Expand Down Expand Up @@ -64,10 +65,10 @@ export class Application<Ready extends boolean = boolean> extends Client<Ready>

async initialize() {
if (env.NODE_ENV === 'production') {
Logger.log(`Booting up ${bold(magenta('production'))} build...`);
logger.log(`Booting up ${bold(magenta('production'))} build...`);
disableValidators();
} else {
Logger.log('Booting up...');
logger.log('Booting up...');
}

await Promise.all([this.loadEvents(), this.loadCommands(), this.prisma.$connect()]);
Expand All @@ -80,12 +81,12 @@ export class Application<Ready extends boolean = boolean> extends Client<Ready>

for (const signal of ['SIGINT', 'SIGTERM']) {
process.on(signal, () => {
Logger.log(`Received ${blue(signal)}, shutting down...`);
logger.log(`Received ${blue(signal)}, shutting down...`);
void Promise.all([this.destroy(), this.prisma.$disconnect()]);
});
}

process.on('uncaughtException', error => Logger.error(error));
process.on('uncaughtException', error => logger.error(error));
}

async deployCommands() {
Expand All @@ -100,7 +101,7 @@ export class Application<Ready extends boolean = boolean> extends Client<Ready>
...this.contextMenuCommands.filter(c => c.dev).map(c => c.contextMenuCommandData.toJSON())
]);

Logger.log('Deployed all commands');
logger.log('Deployed all commands');
}

makeDatabaseBackup() {
Expand All @@ -115,7 +116,7 @@ export class Application<Ready extends boolean = boolean> extends Client<Ready>
return event.data.run(this, ...args);
});
}
Logger.log(`Loaded events [${(performance.now() - perf).toFixed(2)}ms]`);
logger.log(`Loaded events [${(performance.now() - perf).toFixed(2)}ms]`);
}

async loadCommands() {
Expand All @@ -132,7 +133,7 @@ export class Application<Ready extends boolean = boolean> extends Client<Ready>
this.components.set(command.name, command.data);
}
}
Logger.log(`Loaded commands [${(performance.now() - perf).toFixed(2)}ms]`);
logger.log(`Loaded commands [${(performance.now() - perf).toFixed(2)}ms]`);
}

isChatInputCommand(command: Command): command is ChatInputCommand {
Expand Down
43 changes: 0 additions & 43 deletions src/classes/Logger.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './Application.js';
export * from './Detector.js';
export * from './Logger.js';
9 changes: 5 additions & 4 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Application, Logger } from '#classes';
import type { Application } from '#classes';
import { logger } from '#util';
import type { Interaction } from 'discord.js';

export function run(client: Application, interaction: Interaction) {
Expand All @@ -13,7 +14,7 @@ export function run(client: Application, interaction: Interaction) {
const command = client.chatInputCommands.get(interaction.commandName);

if (!command) {
return Logger.warn(`Unknown chat input command: ${interaction.commandName}`);
return logger.warn(`Unknown chat input command: ${interaction.commandName}`);
}

if (!client.developers.includes(interaction.user.id) && command.dev) {
Expand Down Expand Up @@ -41,7 +42,7 @@ export function run(client: Application, interaction: Interaction) {
const command = client.contextMenuCommands.get(interaction.commandName);

if (!command) {
return Logger.warn(`Unknown context menu command: ${interaction.commandName}`);
return logger.warn(`Unknown context menu command: ${interaction.commandName}`);
}

if (!client.developers.includes(interaction.user.id) && command.dev) {
Expand Down Expand Up @@ -70,7 +71,7 @@ export function run(client: Application, interaction: Interaction) {
const component = client.components.get(name);

if (!component) {
return Logger.warn(`Unknown component: ${name}`);
return logger.warn(`Unknown component: ${name}`);
}

if (author !== interaction.user.id) {
Expand Down
5 changes: 3 additions & 2 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import process from 'node:process';
import { type Application, Logger } from '#classes';
import type { Application } from '#classes';
import { logger } from '#util';

export async function run(client: Application) {
if (process.argv.includes('--deploy')) {
await client.deployCommands();
process.exit(0);
}
Logger.log('Ready!');
logger.log('Ready!');
}
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as changelog } from './changelog.json' with { type: 'json' };
export * from './fetchTags.js';
export * from './files.js';
export * as logger from './logger.js';
export * from './sendable.js';
export * from './types.js';
34 changes: 34 additions & 0 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import process from 'node:process';
import { env } from '#env';
import { blue, bold, magenta, red, yellow } from 'yoctocolors';

const date = new Intl.DateTimeFormat('sv', {
dateStyle: 'short',
timeStyle: 'medium',
hour12: false
});

export function debug(...args: unknown[]) {
if (env.NODE_ENV === 'production') {
return;
}

console.error(bold(magenta(`${date.format()} DEBUG`)), ...args);
}

export function error(...args: unknown[]) {
console.error(bold(red(`${date.format()} ERROR`)), ...args);
}

export function fatal(...args: unknown[]) {
console.error(bold(red(`${date.format()} FATAL`)), ...args);
process.exit(1);
}

export function log(...args: unknown[]) {
console.log(bold(blue(`${date.format()} LOG`)), ...args);
}

export function warn(...args: unknown[]) {
console.warn(bold(yellow(`${date.format()} WARN`)), ...args);
}

0 comments on commit ec1f3f0

Please sign in to comment.