Skip to content

Commit 36deb7c

Browse files
Manuelpotb
authored andcommitted
feat: add logger
1 parent 705a9fa commit 36deb7c

File tree

8 files changed

+53
-3
lines changed

8 files changed

+53
-3
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SETUP
22
DISCORD_TOKEN=
3+
LOGLEVEL=
34

45
# DB
56
REDIS_URL=

.eslintrc.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ settings:
2121
import/resolver:
2222

2323
rules:
24+
no-restricted-syntax:
25+
- error
26+
- selector: CallExpression[callee.object.name='console'][callee.property.name!=/^(log|warn|error|info|trace)$/]
27+
message: Use logger instead of console
2428
import/exports-last: error
2529
import/first: error
2630
import/no-duplicates: error

src/core/loadModules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { checkUniqueSlashCommandNames } from './checkUniqueSlashCommandNames';
44
import type { CreatedModule } from './createModule';
55
import { env } from './env';
66
import { pushCommands, routeCommands } from './loaderCommands';
7+
import { coreLogger } from './logger';
78
import { routeHandlers } from './routeHandlers';
89

910
export const loadModules = async (
@@ -15,6 +16,7 @@ export const loadModules = async (
1516
const botCommands = modules.flatMap((module) => module.slashCommands ?? []);
1617

1718
checkUniqueSlashCommandNames(botCommands);
19+
coreLogger.info('Routing slashcommands to interactionCreate event.');
1820
routeCommands(client, botCommands);
1921

2022
const clientId = client.application?.id;

src/core/loaderCommands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88

99
import type { BotCommand } from '../types/bot';
1010
import { deleteExistingCommands } from './deleteExistingCommands';
11+
import { coreLogger } from './logger';
1112

1213
interface PushCommandsOptions {
1314
commands: RESTPostAPIChatInputApplicationCommandsJSONBody[];
@@ -62,6 +63,7 @@ export const pushCommands = async ({
6263
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
6364
body: commands,
6465
});
66+
coreLogger.info('All commands are pushed.');
6567
};
6668

6769
export const routeCommands = (client: Client<true>, botCommands: BotCommand[]) =>

src/core/logger.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import createLogger, { type LoggerOptions } from 'pino';
2+
import type pinodev from 'pino-dev';
3+
import type { PrettyOptions } from 'pino-pretty';
4+
5+
type PrettyDevOptions = Parameters<typeof pinodev>[0];
6+
type BotLoggerOptions = LoggerOptions &
7+
(
8+
| { transport?: { target: 'pino-dev'; options: PrettyDevOptions } }
9+
| { transport?: { target: 'pino-pretty'; options: PrettyOptions } }
10+
);
11+
12+
const developmentOptionsOverride: BotLoggerOptions = {
13+
transport: {
14+
target: 'pino-dev',
15+
options: {
16+
colorize: true,
17+
// propertyMap: {
18+
// module: '[module]',
19+
// },
20+
},
21+
},
22+
// transport: {
23+
// target: 'pino-pretty',
24+
// options: {
25+
// customPrettifiers: {
26+
// // module: (name) => `[${name}]`,
27+
// },
28+
// },
29+
// },
30+
level: process.env['LOGLEVEL'] ?? 'debug',
31+
};
32+
33+
const defaultLogger = createLogger({
34+
level: process.env['LOGLEVEL'] ?? 'info',
35+
...(process.env['NODE_ENV'] === 'production' ? {} : developmentOptionsOverride),
36+
});
37+
38+
export const createLoggerForModule = (moduleName: string) =>
39+
defaultLogger.child({ module: moduleName });
40+
41+
export const coreLogger = createLoggerForModule('core');

src/core/routeHandlers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Client, ClientEvents } from 'discord.js';
22

33
import type { EventHandler } from '../types/bot';
44
import type { CreatedModule } from './createModule';
5+
import { coreLogger } from './logger';
56

67
const handleEvent = async (
78
eventHandlers: EventHandler[],

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ if (!client.isReady()) {
4141

4242
await loadModules(client, createdModules);
4343

44-
console.log('Bot started.');
44+
coreLogger.info('Bot fully started.');

src/modules/recurringMessage/recurringMessage.helpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ export const createRecurringMessage = (
5151
() => {
5252
const channel = client.channels.cache.get(channelId);
5353
if (!channel || !channel.isTextBased()) {
54-
console.error(`Channel ${channelId} not found`);
55-
return;
54+
throw new Error(`Channel ${channelId} not found`);
5655
}
5756
void channel.send(message);
5857
},

0 commit comments

Comments
 (0)