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

Annotation of /branches/3.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: 3166 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { MessageEmbed, WebhookClient } from "discord.js";
21     import { appendFile } from "fs/promises";
22     import path from "path";
23     import Service from "../utils/structures/Service";
24     import { splitMessage } from "../utils/util";
25    
26     export enum LogLevel {
27     LOG = 'log',
28     INFO = 'info',
29     WARN = 'warn',
30     CRITICAL = 'critical',
31     ERROR = 'error'
32     }
33    
34     export default class DebugLogger extends Service {
35     private joinLeaveLogFile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'logs/join-leave.log');
36     private appLogFile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'logs/app.log');
37    
38     async logApp(level: LogLevel, message: string) {
39     await this.log(this.appLogFile, level, message);
40     }
41    
42     async logLeaveJoin(level: LogLevel, message: string) {
43     await this.log(this.joinLeaveLogFile, level, message);
44     }
45    
46     async log(stream: string, level: LogLevel, message: string) {
47     await appendFile(stream, `[${new Date().toISOString()}] [${level}] ${message}\n`);
48     }
49    
50     async logToHomeServer(message: string, logLevel: LogLevel = LogLevel.ERROR) {
51     if (!process.env.DEBUG_WEKHOOK_URL)
52     return;
53    
54     const webhookClient = new WebhookClient({ url: process.env.DEBUG_WEKHOOK_URL! });
55     const splitted = splitMessage(message);
56     const embed = new MessageEmbed({
57     color: logLevel === LogLevel.WARN ? 'GOLD' : 0xf14a60,
58     title: logLevel === LogLevel.WARN ? 'Core Warning' : 'Fatal Error',
59     description: splitted.shift(),
60     });
61    
62     if (splitted.length === 0) {
63     embed.setTimestamp();
64     }
65    
66     try {
67     await webhookClient.send({
68     embeds: [
69     embed
70     ]
71     });
72    
73     for (const index in splitted) {
74     const embed = new MessageEmbed({
75     color: logLevel === LogLevel.WARN ? 'GOLD' : 0xf14a60,
76     description: splitted[index],
77     });
78    
79     if (parseInt(index) === (splitted.length - 1)) {
80     embed.setTimestamp();
81     }
82    
83     await webhookClient.send({
84     embeds: [
85     embed
86     ]
87     });
88     }
89    
90     await webhookClient.destroy();
91     }
92     catch (e) {
93     console.log(e);
94     }
95     }
96     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26