/[sudobot]/trunk/src/services/DebugLogger.ts
ViewVC logotype

Diff of /trunk/src/services/DebugLogger.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 226 by rakin, Mon Jul 29 17:29:06 2024 UTC revision 393 by rakin, Mon Jul 29 17:29:59 2024 UTC
# Line 1  Line 1 
1  import DiscordClient from "../client/Client";  /**
2  import { Guild } from "discord.js";  * 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";  import { appendFile } from "fs/promises";
22    import path from "path";
23  import Service from "../utils/structures/Service";  import Service from "../utils/structures/Service";
24    import { splitMessage } from "../utils/util";
25    
26  export enum LogLevel {  export enum LogLevel {
27      LOG = 'log',      LOG = 'log',
# Line 12  export enum LogLevel { Line 32  export enum LogLevel {
32  }  }
33    
34  export default class DebugLogger extends Service {  export default class DebugLogger extends Service {
35      private joinLeaveLogFile = __dirname + '/../../logs/join-leave.log';      private joinLeaveLogFile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'logs/join-leave.log');
36      private appLogFile = __dirname + '/../../logs/app.log';      private appLogFile = path.join(process.env.SUDO_PREFIX ?? (__dirname + '/../../'), 'logs/app.log');
37            
38      async logApp(level: LogLevel, message: string) {      async logApp(level: LogLevel, message: string) {
39          await this.log(this.appLogFile, level, message);          await this.log(this.appLogFile, level, message);
# Line 26  export default class DebugLogger extends Line 46  export default class DebugLogger extends
46      async log(stream: string, level: LogLevel, message: string) {      async log(stream: string, level: LogLevel, message: string) {
47          await appendFile(stream, `[${new Date().toISOString()}] [${level}] ${message}\n`);          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  }  }

Legend:
Removed from v.226  
changed lines
  Added in v.393

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26