/[sudobot]/branches/2.x/src/services/DebugLogger.ts
ViewVC logotype

Annotation of /branches/2.x/src/services/DebugLogger.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2338 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import DiscordClient from "../client/Client";
2     import { Guild, MessageEmbed, WebhookClient } from "discord.js";
3     import { appendFile } from "fs/promises";
4     import Service from "../utils/structures/Service";
5     import { splitMessage } from "../utils/util";
6    
7     export enum LogLevel {
8     LOG = 'log',
9     INFO = 'info',
10     WARN = 'warn',
11     CRITICAL = 'critical',
12     ERROR = 'error'
13     }
14    
15     export default class DebugLogger extends Service {
16     private joinLeaveLogFile = __dirname + '/../../logs/join-leave.log';
17     private appLogFile = __dirname + '/../../logs/app.log';
18    
19     async logApp(level: LogLevel, message: string) {
20     await this.log(this.appLogFile, level, message);
21     }
22    
23     async logLeaveJoin(level: LogLevel, message: string) {
24     await this.log(this.joinLeaveLogFile, level, message);
25     }
26    
27     async log(stream: string, level: LogLevel, message: string) {
28     await appendFile(stream, `[${new Date().toISOString()}] [${level}] ${message}\n`);
29     }
30    
31     async logToHomeServer(message: string, logLevel: LogLevel = LogLevel.ERROR) {
32     if (!process.env.DEBUG_WEKHOOK_URL)
33     return;
34    
35     const webhookClient = new WebhookClient({ url: process.env.DEBUG_WEKHOOK_URL! });
36     const splitted = splitMessage(message);
37     const embed = new MessageEmbed({
38     color: logLevel === LogLevel.WARN ? 'GOLD' : 0xf14a60,
39     title: logLevel === LogLevel.WARN ? 'Core Warning' : 'Fatal Error',
40     description: splitted.shift(),
41     });
42    
43     if (splitted.length === 0) {
44     embed.setTimestamp();
45     }
46    
47     try {
48     await webhookClient.send({
49     embeds: [
50     embed
51     ]
52     });
53    
54     for (const index in splitted) {
55     const embed = new MessageEmbed({
56     color: logLevel === LogLevel.WARN ? 'GOLD' : 0xf14a60,
57     description: splitted[index],
58     });
59    
60     if (parseInt(index) === (splitted.length - 1)) {
61     embed.setTimestamp();
62     }
63    
64     await webhookClient.send({
65     embeds: [
66     embed
67     ]
68     });
69     }
70     }
71     catch (e) {
72     console.log(e);
73     }
74     }
75     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26