Skip to content

Commit

Permalink
upload logs & sentry update
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirLut committed Apr 23, 2024
1 parent 52536fe commit a074f62
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 12 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"typescript": "^5.4.4"
},
"dependencies": {
"@kaname-png/plugin-sentry": "^1.3.1",
"@mikro-orm/core": "^6.1.12",
"@mikro-orm/migrations": "^6.1.12",
"@mikro-orm/postgresql": "^6.1.12",
Expand All @@ -76,6 +77,7 @@
"@sapphire/result": "^2.6.6",
"@sapphire/time-utilities": "^1.7.12",
"@sapphire/utilities": "^3.15.3",
"@sentry/integrations": "^7.112.1",
"@sentry/node": "^7.109.0",
"@sentry/profiling-node": "^1.3.5",
"discord.js": "^14.14.1",
Expand Down
59 changes: 59 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/commands/admin/upload-logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApplyOptions } from '@sapphire/decorators';
import { Command } from '@sapphire/framework';
import { Message } from 'discord.js';

import { OWNER_ID } from '#base/config/constants';

@ApplyOptions<Command.Options>({
name: 'logs',
description: 'Выгрузка логов',
})
export class UploadLogCommand extends Command {
override async messageRun(message: Message) {
if (!OWNER_ID.includes(message.author.id)) return;

message.channel.send({
files: ['./debug.log'],
});
}
}
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '@sapphire/plugin-api';
import '@sapphire/plugin-api/register';
import '@sapphire/plugin-logger';
import '@sapphire/plugin-logger/register';
import '@kaname-png/plugin-sentry/register';

process.env.NODE_ENV ??= 'development';

Expand Down
10 changes: 0 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import * as Sentry from '@sentry/node';
import { ProfilingIntegration } from '@sentry/profiling-node';

import './config';

import { RgdClient } from '#lib/rgd.client';

async function bootstrap() {
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [new ProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
});

const client = new RgdClient();

await client.login(process.env.BOT_TOKEN);
Expand Down
12 changes: 12 additions & 0 deletions src/lib/rgd.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import { join } from 'path';
import { createClient } from 'redis';

import { setupLogFile } from './sapphire';

import MikroOrmConfig from '#base/mikro-orm.config';
import { IS_DEV } from '#config/constants';

Expand Down Expand Up @@ -68,7 +70,17 @@ export class RgdClient<
transformers: [],
},
},
sentry: {
loadSentryErrorListeners: true,
options: {
enabled: true,
},
},
});

///

setupLogFile();
}
override async login(token?: string): Promise<string> {
const orm = await MikroORM.init<PostgreSqlDriver>(MikroOrmConfig);
Expand Down
32 changes: 31 additions & 1 deletion src/lib/sapphire.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { ChatInputCommand } from '@sapphire/framework';
import {
type ChatInputCommand,
container,
LogLevel,
} from '@sapphire/framework';
import { codeBlock } from 'discord.js';
import fs from 'fs';

export async function replyWithError(
interaction: ChatInputCommand.Interaction,
Expand Down Expand Up @@ -38,3 +43,28 @@ export function replyJson(
ephemeral: true,
});
}

export function setupLogFile() {
const write = container.logger.write;
const log_file = fs.createWriteStream('./debug.log', {
flags: 'w',
});

container.logger.write = (...args) => {
const [level, message] = args;

const formatter =
container.logger['formats'].get(level) ??
container.logger['formats'].get(LogLevel.None);

const output = formatter
.run(container.logger['preprocess']([message]))
.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'',
);

log_file.write(output + '\n');
write.call(container.logger, ...args);
};
}
21 changes: 20 additions & 1 deletion src/listeners/ready.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ApplyOptions } from '@sapphire/decorators';
import { Events, Listener, Piece, Store } from '@sapphire/framework';
import {
Events,
Listener,
Piece,
SapphireClient,
Store,
} from '@sapphire/framework';

import { GuildService } from '#base/services/guild.service';
import { InviteService } from '#base/services/invite.service';
Expand All @@ -9,6 +15,7 @@ export class Ready extends Listener {
async run() {
this.printStores();
this.printApiInfo();
this.printPlugins();

this.container.logger.info(
'-> Bot successfully started! Listening to commands...',
Expand Down Expand Up @@ -70,6 +77,18 @@ export class Ready extends Listener {
);
}
}

private printPlugins() {
const plugins = SapphireClient.plugins.registry;

let i = 0;
this.container.logger.info('Plugins list...');
for (const plugin of plugins) {
const line = ++i === plugins.size ? '└─' : '├─';
this.container.logger.info(`${line} ${plugin.name}`);
}
}

private styleStore(store: Store<Piece>, last: boolean) {
return `${last ? '└─' : '├─'} Loaded ${store.size
.toString()
Expand Down
7 changes: 7 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SentryOptions } from '@kaname-png/plugin-sentry';
import { MikroORM } from '@mikro-orm/postgresql';
import { RedisClientType } from 'redis';

Expand All @@ -15,3 +16,9 @@ declare module '@sapphire/pieces' {
shop: RgdShopStore;
}
}

declare module '@sapphire.js' {
export interface SapphireClient {
sentry?: SentryOptions;
}
}

0 comments on commit a074f62

Please sign in to comment.