/[sudobot]/branches/7.x/src/utils/logger.ts
ViewVC logotype

Annotation of /branches/7.x/src/utils/logger.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: 4112 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-2023 OSN Developers.
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 chalk from "chalk";
21     import Client from "../core/Client";
22     import { developmentMode } from "./utils";
23    
24     export enum LogLevel {
25     DEBUG,
26     INFO,
27     WARN,
28     ERROR,
29     CRITICAL,
30     SUCCESS,
31     EVENT
32     }
33    
34     export function logWithLevel(level: LogLevel = LogLevel.DEBUG, ...message: any[]) {
35     const logServerEnabled = Client.instance?.configManager?.systemConfig?.log_server?.enabled;
36    
37     if (level !== LogLevel.CRITICAL && level !== LogLevel.ERROR && process.env.SUDO_ENV?.toLowerCase() === "testing") {
38     return;
39     }
40    
41     if (level === LogLevel.DEBUG) {
42     if (!developmentMode()) return;
43    
44     console.debug(`${chalk.gray("[system:debug]")}`, ...message);
45     } else if (level === LogLevel.INFO) {
46     console.info(`${chalk.cyan("[system:info]")}`, ...message);
47     } else if (level === LogLevel.WARN) {
48     console.warn(`${chalk.yellow("[system:warn]")}`, ...message);
49     } else if (level === LogLevel.ERROR) {
50     console.error(`${chalk.red("[system:error]")}`, ...message);
51     } else if (level === LogLevel.SUCCESS) {
52     console.error(`${chalk.green("[system:success]")}`, ...message);
53     } else if (level === LogLevel.EVENT) {
54     console.error(`${chalk.green("[system:event]")}`, ...message);
55     } else if (level === LogLevel.CRITICAL) {
56     console.error(`${chalk.redBright("[system:critical]")}`, ...message);
57    
58     if (logServerEnabled) {
59     Client.instance?.logServer?.send(`[system:critical] ${message.join(" ")}`);
60     }
61    
62     console.log("Critical error occurred. Exiting.");
63     process.exit(-1);
64     }
65    
66     if (logServerEnabled) {
67     // `[system:${LogLevel[level].toLowerCase()}] ${message.join(" ")}`
68     Client.instance?.logServer?.log?.(level, message.join(" "));
69     }
70     }
71    
72     export function logStringWithLevel(level: LogLevel = LogLevel.DEBUG, ...message: any[]) {
73     if (level !== LogLevel.CRITICAL && level !== LogLevel.ERROR && process.env.SUDO_ENV?.toLowerCase() === "testing") {
74     return;
75     }
76    
77     if (level === LogLevel.DEBUG) {
78     if (!developmentMode()) return;
79    
80     return `${chalk.gray("[system:debug]")} ` + message.join(" ");
81     } else if (level === LogLevel.INFO) {
82     return `${chalk.cyan("[system:info]")} ` + message.join(" ");
83     } else if (level === LogLevel.WARN) {
84     return `${chalk.yellow("[system:warn]")} ` + message.join(" ");
85     } else if (level === LogLevel.ERROR) {
86     return `${chalk.red("[system:error]")} ` + message.join(" ");
87     } else if (level === LogLevel.SUCCESS) {
88     return `${chalk.green("[system:success]")} ` + message.join(" ");
89     } else if (level === LogLevel.EVENT) {
90     return `${chalk.green("[system:event]")} ` + message.join(" ");
91     } else if (level === LogLevel.CRITICAL) {
92     return `${chalk.redBright("[system:critical]")} ` + message.join(" ");
93     }
94     }
95    
96     export function log(...message: any[]) {
97     return logWithLevel(LogLevel.DEBUG, ...message);
98     }
99    
100     export function logError(...message: any[]) {
101     return logWithLevel(LogLevel.ERROR, ...message);
102     }
103    
104     export function logInfo(...message: any[]) {
105     return logWithLevel(LogLevel.INFO, ...message);
106     }
107    
108     export function logWarn(...message: any[]) {
109     return logWithLevel(LogLevel.WARN, ...message);
110     }
111    
112     export function logSuccess(...message: any[]) {
113     return logWithLevel(LogLevel.SUCCESS, ...message);
114     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26