/[sudobot]/branches/8.x/src/utils/Logger.ts
ViewVC logotype

Annotation of /branches/8.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: 6080 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     import chalk from "chalk";
20     import Client from "../core/Client";
21     import { AnyFunction } from "../types/Utils";
22     import { developmentMode } from "./utils";
23    
24     export enum LogLevel {
25     Debug,
26     Info,
27     Warn,
28     Error,
29     Fatal,
30     Critical,
31     Success,
32     Event
33     }
34    
35     export class Logger {
36     private readonly formatter = Intl.DateTimeFormat("en-US", {
37     dateStyle: "long",
38     timeStyle: "long"
39     });
40    
41     public constructor(private readonly name: string, private readonly logTime: boolean = false) {
42     for (const key in this) {
43     if (typeof this[key] === "function") {
44     (this[key] as AnyFunction) = (this[key] as AnyFunction).bind(this);
45     }
46     }
47     }
48    
49     public log(level: LogLevel, ...args: unknown[]) {
50     const levelName = LogLevel[level].toLowerCase();
51     const methodName =
52     level === LogLevel.Info || level === LogLevel.Success || level === LogLevel.Event
53     ? "info"
54     : level === LogLevel.Debug
55     ? "debug"
56     : level === LogLevel.Warn
57     ? "warn"
58     : "error";
59     const beginning = `${
60     this.logTime ? `${chalk.gray(this.formatter.format(new Date()))} ` : ""
61     }${this.colorize(`${this.name}:${levelName}`, level)}`;
62     this.print(methodName, beginning, ...args);
63    
64     if (level === LogLevel.Fatal) {
65     console.log("Critical error occurred. Exiting.");
66     process.exit(-1);
67     }
68     }
69    
70     public print(methodName: "log" | "info" | "warn" | "error" | "debug", ...args: unknown[]) {
71     const logServerEnabled = Client.instance?.configManager?.systemConfig?.log_server?.enabled;
72    
73     if (process.env.SUPRESS_LOGS) {
74     return;
75     }
76    
77     console[methodName].call(console, ...args);
78     const message = args
79     .map(arg => (typeof arg === "string" ? arg : JSON.stringify(arg, null, 2)))
80     .join(" ");
81    
82     if (logServerEnabled) {
83     Client.instance.logServer?.log(message);
84     }
85     }
86    
87     private colorize(text: string, level: LogLevel) {
88     switch (level) {
89     case LogLevel.Debug:
90     return chalk.gray.dim(text);
91    
92     case LogLevel.Critical:
93     case LogLevel.Fatal:
94     return chalk.redBright(text);
95    
96     case LogLevel.Error:
97     return chalk.red(text);
98    
99     case LogLevel.Warn:
100     return chalk.yellowBright(text);
101    
102     case LogLevel.Info:
103     return chalk.blueBright(text);
104    
105     case LogLevel.Success:
106     case LogLevel.Event:
107     return chalk.greenBright(text);
108     }
109     }
110    
111     public debug(...args: unknown[]) {
112     if (!developmentMode()) {
113     return;
114     }
115    
116     this.log(LogLevel.Debug, ...args);
117     }
118    
119     public info(...args: unknown[]) {
120     this.log(LogLevel.Info, ...args);
121     }
122    
123     public warn(...args: unknown[]) {
124     this.log(LogLevel.Warn, ...args);
125     }
126    
127     public error(...args: unknown[]) {
128     this.log(LogLevel.Error, ...args);
129     }
130    
131     public fatal(...args: unknown[]) {
132     this.log(LogLevel.Fatal, ...args);
133     }
134    
135     public critical(...args: unknown[]) {
136     this.log(LogLevel.Critical, ...args);
137     }
138    
139     public success(...args: unknown[]) {
140     this.log(LogLevel.Success, ...args);
141     }
142    
143     public event(...args: unknown[]) {
144     this.log(LogLevel.Event, ...args);
145     }
146     }
147    
148     /**
149     * Logs a debug message to the console.
150     *
151     * @deprecated Use the logger instance instead.
152     */
153     export const logDebug = (...args: unknown[]) => Client.getLogger().debug(...args);
154    
155     /**
156     * Alias of logDebug.
157     *
158     * @deprecated Use the logger instance instead.
159     */
160     export const log = logDebug;
161    
162     /**
163     * Logs an info message to the console.
164     *
165     * @deprecated Use the logger instance instead.
166     */
167     export const logInfo = (...args: unknown[]) => Client.getLogger().info(...args);
168    
169     /**
170     * Logs a warning to the console.
171     *
172     * @deprecated Use the logger instance instead.
173     */
174     export const logWarn = (...args: unknown[]) => Client.getLogger().warn(...args);
175    
176     /**
177     * Logs an error to the console.
178     *
179     * @deprecated Use the logger instance instead.
180     */
181    
182     export const logError = (...args: unknown[]) => Client.getLogger().error(...args);
183    
184     /**
185     * Logs a fatal error to the console and exits the process.
186     *
187     * @deprecated Use the logger instance instead.
188     */
189    
190     export const logFatal = (...args: unknown[]) => Client.getLogger().fatal(...args);
191    
192     /**
193     * Logs a critical error to the console.
194     *
195     * @deprecated Use the logger instance instead.
196     */
197    
198     export const logCritical = (...args: unknown[]) => Client.getLogger().critical(...args);
199    
200     /**
201     * Logs a success message to the console.
202     *
203     * @deprecated Use the logger instance instead.
204     */
205    
206     export const logSuccess = (...args: unknown[]) => Client.getLogger().success(...args);
207    
208     /**
209     * Logs an event message to the console.
210     *
211     * @deprecated Use the logger instance instead.
212     */
213    
214     export const logEvent = (...args: unknown[]) => Client.getLogger().event(...args);
215    
216     /**
217     * Logs a message to the console with a specified log level.
218     *
219     * @deprecated Use the logger instance instead.
220     */
221    
222     export const logWithLevel = (level: LogLevel, ...args: unknown[]) =>
223     Client.getLogger().log(level, ...args);

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26