@feizk/logger 1.5.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,43 +21,27 @@ logger.error('This is an error');
21
21
  logger.debug('This is a debug message');
22
22
  ```
23
23
 
24
- ### Options
24
+ ## Options
25
25
 
26
- Customize the logger with constructor options:
26
+ | Option | Type | Default | Description |
27
+ | --------------- | -------------------------------- | ----------- | ------------------------------------- |
28
+ | `enableColors` | `boolean` | `true` | Enable colored output |
29
+ | `formatTimestamp`| `function \| undefined` | `ISO format`| Custom timestamp formatter function |
30
+ | `formatLog` | `function \| undefined` | `undefined` | Custom log formatter function |
31
+ | `level` | `'debug' \| 'info' \| 'warn' \| 'error'` | `'debug'` | Minimum log level |
32
+ | `discord` | `object \| undefined` | `undefined` | Discord transport options |
27
33
 
28
- ```typescript
29
- const logger = new Logger({
30
- enableColors: true, // Default: true
31
- formatTimestamp: undefined, // Custom timestamp formatter function, Default: ISO format
32
- formatLog: undefined, // Custom log formatter function, Default: undefined
33
- level: 'debug', // 'debug' | 'info' | 'warn' | 'error', Default: 'debug'
34
- });
35
- ```
34
+ ### Discord Options
36
35
 
37
- #### Examples
36
+ The `discord` option configures Discord webhook integration. It has the following properties:
38
37
 
39
- ```typescript
40
- // Disable colors
41
- const noColorLogger = new Logger({ enableColors: false });
42
-
43
- // Use locale timestamp
44
- const localeLogger = new Logger({
45
- formatTimestamp: (types) => [types.Locale, new Date().toLocaleString()],
46
- });
47
-
48
- // Custom timestamp
49
- const customLogger = new Logger({
50
- formatTimestamp: () => [TIMESTAMP_TYPES.Custom, 'custom-time'],
51
- });
52
-
53
- // Filter logs below info level
54
- const infoLogger = new Logger({ level: 'info' });
55
- infoLogger.debug('Not logged');
56
- infoLogger.info('Logged'); // and higher
57
-
58
- // Change level dynamically
59
- logger.setLevel('error');
60
- ```
38
+ - `enable`: `boolean`, default `false` - Enable Discord transport
39
+ - `webhookURL`: `string`, default `''` - Discord webhook URL
40
+ - `formatEmbed`: `function \| undefined`, default `undefined` - Custom embed formatter function
41
+ - `batchSize`: `number`, default `10` - Number of embeds per batch request
42
+ - `batchDelay`: `number`, default `2000` - Delay in ms between batch sends
43
+ - `maxRetries`: `number`, default `3` - Maximum retry attempts for failed sends
44
+ - `retryDelayBase`: `number`, default `1000` - Base delay in ms for exponential backoff
61
45
 
62
46
  ## API
63
47
 
@@ -70,3 +54,7 @@ logger.setLevel('error');
70
54
  - `setLevel(level: LogLevel)`: Sets the minimum log level for filtering messages.
71
55
 
72
56
  All messages include a timestamp and are colored accordingly (unless disabled via options).
57
+
58
+ ## License
59
+
60
+ MIT
package/dist/index.d.mts CHANGED
@@ -5,11 +5,21 @@ interface TimestampTypes {
5
5
  Locale: 'locale';
6
6
  Custom: 'custom';
7
7
  }
8
+ interface DiscordOptions {
9
+ enable: boolean;
10
+ webhookURL: string;
11
+ batchSize?: number;
12
+ batchDelay?: number;
13
+ maxRetries?: number;
14
+ retryDelayBase?: number;
15
+ formatEmbed?: (level: LogLevel, timestamp: string, message: string) => Record<string, unknown>;
16
+ }
8
17
  interface LoggerOptions {
18
+ level?: LogLevel;
9
19
  enableColors?: boolean;
20
+ discord?: DiscordOptions;
10
21
  formatTimestamp?: (types: TimestampTypes, date?: Date) => [TimestampType, string];
11
22
  formatLog?: (level: string, timestamp: string, args: unknown[]) => string;
12
- level?: LogLevel;
13
23
  }
14
24
 
15
25
  /**
@@ -18,12 +28,31 @@ interface LoggerOptions {
18
28
  declare class Logger {
19
29
  private options;
20
30
  private level;
31
+ private discordQueue;
32
+ private isProcessing;
33
+ private processTimeout?;
21
34
  constructor(options?: LoggerOptions);
22
35
  /**
23
36
  * Sets the minimum log level for filtering messages.
24
37
  * @param level - The log level to set.
25
38
  */
26
39
  setLevel(level: LogLevel): void;
40
+ /**
41
+ * Sends a log message to Discord via webhook if configured.
42
+ * @param level - The log level.
43
+ * @param timestamp - The formatted timestamp.
44
+ * @param args - The log arguments.
45
+ */
46
+ private sendToDiscord;
47
+ /**
48
+ * Processes the Discord queue by sending batches of embeds.
49
+ */
50
+ private processQueue;
51
+ /**
52
+ * Sends a batch of embeds to Discord.
53
+ * @param embeds - The embeds to send.
54
+ */
55
+ private sendBatch;
27
56
  /**
28
57
  * Checks if a log level should be output based on the current log level.
29
58
  * @param level - The log level to check.
@@ -54,4 +83,4 @@ declare class Logger {
54
83
 
55
84
  declare const TIMESTAMP_TYPES: TimestampTypes;
56
85
 
57
- export { type LogLevel, Logger, type LoggerOptions, TIMESTAMP_TYPES, type TimestampType, type TimestampTypes };
86
+ export { type DiscordOptions, type LogLevel, Logger, type LoggerOptions, TIMESTAMP_TYPES, type TimestampType, type TimestampTypes };
package/dist/index.d.ts CHANGED
@@ -5,11 +5,21 @@ interface TimestampTypes {
5
5
  Locale: 'locale';
6
6
  Custom: 'custom';
7
7
  }
8
+ interface DiscordOptions {
9
+ enable: boolean;
10
+ webhookURL: string;
11
+ batchSize?: number;
12
+ batchDelay?: number;
13
+ maxRetries?: number;
14
+ retryDelayBase?: number;
15
+ formatEmbed?: (level: LogLevel, timestamp: string, message: string) => Record<string, unknown>;
16
+ }
8
17
  interface LoggerOptions {
18
+ level?: LogLevel;
9
19
  enableColors?: boolean;
20
+ discord?: DiscordOptions;
10
21
  formatTimestamp?: (types: TimestampTypes, date?: Date) => [TimestampType, string];
11
22
  formatLog?: (level: string, timestamp: string, args: unknown[]) => string;
12
- level?: LogLevel;
13
23
  }
14
24
 
15
25
  /**
@@ -18,12 +28,31 @@ interface LoggerOptions {
18
28
  declare class Logger {
19
29
  private options;
20
30
  private level;
31
+ private discordQueue;
32
+ private isProcessing;
33
+ private processTimeout?;
21
34
  constructor(options?: LoggerOptions);
22
35
  /**
23
36
  * Sets the minimum log level for filtering messages.
24
37
  * @param level - The log level to set.
25
38
  */
26
39
  setLevel(level: LogLevel): void;
40
+ /**
41
+ * Sends a log message to Discord via webhook if configured.
42
+ * @param level - The log level.
43
+ * @param timestamp - The formatted timestamp.
44
+ * @param args - The log arguments.
45
+ */
46
+ private sendToDiscord;
47
+ /**
48
+ * Processes the Discord queue by sending batches of embeds.
49
+ */
50
+ private processQueue;
51
+ /**
52
+ * Sends a batch of embeds to Discord.
53
+ * @param embeds - The embeds to send.
54
+ */
55
+ private sendBatch;
27
56
  /**
28
57
  * Checks if a log level should be output based on the current log level.
29
58
  * @param level - The log level to check.
@@ -54,4 +83,4 @@ declare class Logger {
54
83
 
55
84
  declare const TIMESTAMP_TYPES: TimestampTypes;
56
85
 
57
- export { type LogLevel, Logger, type LoggerOptions, TIMESTAMP_TYPES, type TimestampType, type TimestampTypes };
86
+ export { type DiscordOptions, type LogLevel, Logger, type LoggerOptions, TIMESTAMP_TYPES, type TimestampType, type TimestampTypes };
package/dist/index.js CHANGED
@@ -56,6 +56,18 @@ function getColor(level, enableColors) {
56
56
  };
57
57
  return colors[level] || level;
58
58
  }
59
+ function getDiscordColor(level) {
60
+ const colors = {
61
+ debug: 9807270,
62
+ info: 3447003,
63
+ warn: 15965202,
64
+ error: 15158332
65
+ };
66
+ return colors[level];
67
+ }
68
+ function generateId() {
69
+ return Math.random().toString(36).substr(2, 8).toUpperCase();
70
+ }
59
71
  function formatLog(level, timestamp, args, options) {
60
72
  const { formatLog: formatLog2, enableColors = true } = options;
61
73
  const coloredLevel = getColor(level, enableColors);
@@ -75,10 +87,13 @@ var LOG_LEVEL_PRIORITIES = {
75
87
  };
76
88
  var Logger = class {
77
89
  constructor(options = {}) {
90
+ this.discordQueue = [];
91
+ this.isProcessing = false;
78
92
  this.options = {
79
93
  enableColors: options.enableColors ?? true,
80
94
  formatTimestamp: options.formatTimestamp ?? defaultFormatTimestamp,
81
- formatLog: options.formatLog
95
+ formatLog: options.formatLog,
96
+ discord: options.discord
82
97
  };
83
98
  this.level = options.level ?? "debug";
84
99
  }
@@ -89,6 +104,73 @@ var Logger = class {
89
104
  setLevel(level) {
90
105
  this.level = level;
91
106
  }
107
+ /**
108
+ * Sends a log message to Discord via webhook if configured.
109
+ * @param level - The log level.
110
+ * @param timestamp - The formatted timestamp.
111
+ * @param args - The log arguments.
112
+ */
113
+ sendToDiscord(level, timestamp, args) {
114
+ const discord = this.options.discord;
115
+ if (!discord?.enable) return;
116
+ try {
117
+ new URL(discord.webhookURL);
118
+ } catch {
119
+ return;
120
+ }
121
+ const message = args.map((arg) => typeof arg === "string" ? arg : JSON.stringify(arg)).join(" ");
122
+ const title = `${level.toUpperCase()}-${generateId()}`;
123
+ const embed = discord.formatEmbed ? discord.formatEmbed(level, timestamp, message) : {
124
+ title,
125
+ description: message,
126
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
127
+ color: getDiscordColor(level)
128
+ };
129
+ this.discordQueue.push({ embed, retryCount: 0 });
130
+ if (!this.isProcessing) {
131
+ this.isProcessing = true;
132
+ setTimeout(() => this.processQueue(), 0);
133
+ }
134
+ }
135
+ /**
136
+ * Processes the Discord queue by sending batches of embeds.
137
+ */
138
+ processQueue() {
139
+ if (this.discordQueue.length === 0) {
140
+ this.isProcessing = false;
141
+ return;
142
+ }
143
+ this.isProcessing = true;
144
+ const discord = this.options.discord;
145
+ const batchSize = discord.batchSize ?? 10;
146
+ const batch = this.discordQueue.splice(0, batchSize);
147
+ this.sendBatch(batch.map((item) => item.embed)).then(() => {
148
+ const delay = discord.batchDelay ?? 2e3;
149
+ this.processTimeout = setTimeout(() => this.processQueue(), delay);
150
+ }).catch(() => {
151
+ const maxRetries = discord.maxRetries ?? 3;
152
+ const retryItems = batch.filter((item) => item.retryCount < maxRetries).map((item) => ({
153
+ ...item,
154
+ retryCount: item.retryCount + 1
155
+ }));
156
+ this.discordQueue.unshift(...retryItems);
157
+ const retryDelayBase = discord.retryDelayBase ?? 1e3;
158
+ const delay = retryDelayBase * Math.pow(2, batch[0]?.retryCount ?? 0);
159
+ this.processTimeout = setTimeout(() => this.processQueue(), delay);
160
+ });
161
+ }
162
+ /**
163
+ * Sends a batch of embeds to Discord.
164
+ * @param embeds - The embeds to send.
165
+ */
166
+ async sendBatch(embeds) {
167
+ const discord = this.options.discord;
168
+ await fetch(discord.webhookURL, {
169
+ method: "POST",
170
+ headers: { "Content-Type": "application/json" },
171
+ body: JSON.stringify({ embeds })
172
+ });
173
+ }
92
174
  /**
93
175
  * Checks if a log level should be output based on the current log level.
94
176
  * @param level - The log level to check.
@@ -108,6 +190,7 @@ var Logger = class {
108
190
  TIMESTAMP_TYPES
109
191
  );
110
192
  console.log(...formatLog("[INFO]", timestamp, args, this.options));
193
+ this.sendToDiscord("info", timestamp, args);
111
194
  }
112
195
  /**
113
196
  * Logs a warning message.
@@ -120,6 +203,7 @@ var Logger = class {
120
203
  TIMESTAMP_TYPES
121
204
  );
122
205
  console.log(...formatLog("[WARN]", timestamp, args, this.options));
206
+ this.sendToDiscord("warn", timestamp, args);
123
207
  }
124
208
  /**
125
209
  * Logs an error message.
@@ -132,6 +216,7 @@ var Logger = class {
132
216
  TIMESTAMP_TYPES
133
217
  );
134
218
  console.log(...formatLog("[ERROR]", timestamp, args, this.options));
219
+ this.sendToDiscord("error", timestamp, args);
135
220
  }
136
221
  /**
137
222
  * Logs a debug message.
@@ -144,6 +229,7 @@ var Logger = class {
144
229
  TIMESTAMP_TYPES
145
230
  );
146
231
  console.log(...formatLog("[DEBUG]", timestamp, args, this.options));
232
+ this.sendToDiscord("debug", timestamp, args);
147
233
  }
148
234
  };
149
235
  // Annotate the CommonJS export names for ESM import in node:
@@ -151,4 +237,3 @@ var Logger = class {
151
237
  Logger,
152
238
  TIMESTAMP_TYPES
153
239
  });
154
- //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -19,6 +19,18 @@ function getColor(level, enableColors) {
19
19
  };
20
20
  return colors[level] || level;
21
21
  }
22
+ function getDiscordColor(level) {
23
+ const colors = {
24
+ debug: 9807270,
25
+ info: 3447003,
26
+ warn: 15965202,
27
+ error: 15158332
28
+ };
29
+ return colors[level];
30
+ }
31
+ function generateId() {
32
+ return Math.random().toString(36).substr(2, 8).toUpperCase();
33
+ }
22
34
  function formatLog(level, timestamp, args, options) {
23
35
  const { formatLog: formatLog2, enableColors = true } = options;
24
36
  const coloredLevel = getColor(level, enableColors);
@@ -38,10 +50,13 @@ var LOG_LEVEL_PRIORITIES = {
38
50
  };
39
51
  var Logger = class {
40
52
  constructor(options = {}) {
53
+ this.discordQueue = [];
54
+ this.isProcessing = false;
41
55
  this.options = {
42
56
  enableColors: options.enableColors ?? true,
43
57
  formatTimestamp: options.formatTimestamp ?? defaultFormatTimestamp,
44
- formatLog: options.formatLog
58
+ formatLog: options.formatLog,
59
+ discord: options.discord
45
60
  };
46
61
  this.level = options.level ?? "debug";
47
62
  }
@@ -52,6 +67,73 @@ var Logger = class {
52
67
  setLevel(level) {
53
68
  this.level = level;
54
69
  }
70
+ /**
71
+ * Sends a log message to Discord via webhook if configured.
72
+ * @param level - The log level.
73
+ * @param timestamp - The formatted timestamp.
74
+ * @param args - The log arguments.
75
+ */
76
+ sendToDiscord(level, timestamp, args) {
77
+ const discord = this.options.discord;
78
+ if (!discord?.enable) return;
79
+ try {
80
+ new URL(discord.webhookURL);
81
+ } catch {
82
+ return;
83
+ }
84
+ const message = args.map((arg) => typeof arg === "string" ? arg : JSON.stringify(arg)).join(" ");
85
+ const title = `${level.toUpperCase()}-${generateId()}`;
86
+ const embed = discord.formatEmbed ? discord.formatEmbed(level, timestamp, message) : {
87
+ title,
88
+ description: message,
89
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
90
+ color: getDiscordColor(level)
91
+ };
92
+ this.discordQueue.push({ embed, retryCount: 0 });
93
+ if (!this.isProcessing) {
94
+ this.isProcessing = true;
95
+ setTimeout(() => this.processQueue(), 0);
96
+ }
97
+ }
98
+ /**
99
+ * Processes the Discord queue by sending batches of embeds.
100
+ */
101
+ processQueue() {
102
+ if (this.discordQueue.length === 0) {
103
+ this.isProcessing = false;
104
+ return;
105
+ }
106
+ this.isProcessing = true;
107
+ const discord = this.options.discord;
108
+ const batchSize = discord.batchSize ?? 10;
109
+ const batch = this.discordQueue.splice(0, batchSize);
110
+ this.sendBatch(batch.map((item) => item.embed)).then(() => {
111
+ const delay = discord.batchDelay ?? 2e3;
112
+ this.processTimeout = setTimeout(() => this.processQueue(), delay);
113
+ }).catch(() => {
114
+ const maxRetries = discord.maxRetries ?? 3;
115
+ const retryItems = batch.filter((item) => item.retryCount < maxRetries).map((item) => ({
116
+ ...item,
117
+ retryCount: item.retryCount + 1
118
+ }));
119
+ this.discordQueue.unshift(...retryItems);
120
+ const retryDelayBase = discord.retryDelayBase ?? 1e3;
121
+ const delay = retryDelayBase * Math.pow(2, batch[0]?.retryCount ?? 0);
122
+ this.processTimeout = setTimeout(() => this.processQueue(), delay);
123
+ });
124
+ }
125
+ /**
126
+ * Sends a batch of embeds to Discord.
127
+ * @param embeds - The embeds to send.
128
+ */
129
+ async sendBatch(embeds) {
130
+ const discord = this.options.discord;
131
+ await fetch(discord.webhookURL, {
132
+ method: "POST",
133
+ headers: { "Content-Type": "application/json" },
134
+ body: JSON.stringify({ embeds })
135
+ });
136
+ }
55
137
  /**
56
138
  * Checks if a log level should be output based on the current log level.
57
139
  * @param level - The log level to check.
@@ -71,6 +153,7 @@ var Logger = class {
71
153
  TIMESTAMP_TYPES
72
154
  );
73
155
  console.log(...formatLog("[INFO]", timestamp, args, this.options));
156
+ this.sendToDiscord("info", timestamp, args);
74
157
  }
75
158
  /**
76
159
  * Logs a warning message.
@@ -83,6 +166,7 @@ var Logger = class {
83
166
  TIMESTAMP_TYPES
84
167
  );
85
168
  console.log(...formatLog("[WARN]", timestamp, args, this.options));
169
+ this.sendToDiscord("warn", timestamp, args);
86
170
  }
87
171
  /**
88
172
  * Logs an error message.
@@ -95,6 +179,7 @@ var Logger = class {
95
179
  TIMESTAMP_TYPES
96
180
  );
97
181
  console.log(...formatLog("[ERROR]", timestamp, args, this.options));
182
+ this.sendToDiscord("error", timestamp, args);
98
183
  }
99
184
  /**
100
185
  * Logs a debug message.
@@ -107,10 +192,10 @@ var Logger = class {
107
192
  TIMESTAMP_TYPES
108
193
  );
109
194
  console.log(...formatLog("[DEBUG]", timestamp, args, this.options));
195
+ this.sendToDiscord("debug", timestamp, args);
110
196
  }
111
197
  };
112
198
  export {
113
199
  Logger,
114
200
  TIMESTAMP_TYPES
115
201
  };
116
- //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feizk/logger",
3
- "version": "1.5.1",
3
+ "version": "1.7.0",
4
4
  "description": "A simple logger package with colored outputs and timestamps",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts","../src/utils.ts","../src/logger.ts"],"sourcesContent":["export { Logger } from './logger';\nexport { TIMESTAMP_TYPES } from './utils';\nexport type {\n LoggerOptions,\n LogLevel,\n TimestampTypes,\n TimestampType,\n} from './types';\n","import chalk from 'chalk';\nimport type { LoggerOptions, TimestampTypes } from './types';\n\nexport const TIMESTAMP_TYPES: TimestampTypes = {\n ISO: 'iso',\n Locale: 'locale',\n Custom: 'custom',\n};\n\nexport function formatTimestamp(\n formatTimestampFn: NonNullable<LoggerOptions['formatTimestamp']>,\n types: TimestampTypes,\n date: Date = new Date(),\n): string {\n const [, timestamp] = formatTimestampFn(types, date);\n return timestamp;\n}\n\nexport function getColor(level: string, enableColors: boolean): string {\n if (!enableColors) return level;\n const colors: Record<string, string> = {\n '[INFO]': chalk.blue(level),\n '[WARN]': chalk.yellow(level),\n '[ERROR]': chalk.red(level),\n '[DEBUG]': chalk.gray(level),\n };\n return colors[level] || level;\n}\n\nexport function formatLog(\n level: string,\n timestamp: string,\n args: unknown[],\n options: LoggerOptions,\n): [string, ...unknown[]] {\n const { formatLog, enableColors = true } = options;\n const coloredLevel = getColor(level, enableColors);\n if (formatLog) {\n return [formatLog(coloredLevel, timestamp, args)];\n }\n return [`${coloredLevel} ${timestamp}`, ...args];\n}\n","import type { LoggerOptions, LogLevel, TimestampTypes } from './types';\nimport { formatTimestamp, formatLog, TIMESTAMP_TYPES } from './utils';\n\nimport type { TimestampType } from './types';\n\nconst defaultFormatTimestamp = (\n types: TimestampTypes,\n date: Date = new Date(),\n): [TimestampType, string] => [types.ISO, date.toISOString()];\n\nconst LOG_LEVEL_PRIORITIES: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\n/**\n * A simple logger with colored outputs and timestamps.\n */\nexport class Logger {\n private options: LoggerOptions;\n private level: LogLevel;\n\n constructor(options: LoggerOptions = {}) {\n this.options = {\n enableColors: options.enableColors ?? true,\n formatTimestamp: options.formatTimestamp ?? defaultFormatTimestamp,\n formatLog: options.formatLog,\n };\n this.level = options.level ?? 'debug';\n }\n\n /**\n * Sets the minimum log level for filtering messages.\n * @param level - The log level to set.\n */\n setLevel(level: LogLevel): void {\n this.level = level;\n }\n\n /**\n * Checks if a log level should be output based on the current log level.\n * @param level - The log level to check.\n * @returns True if the message should be logged.\n */\n private shouldLog(level: LogLevel): boolean {\n return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.level];\n }\n\n /**\n * Logs an info message.\n * @param args - The arguments to log.\n */\n info(...args: unknown[]): void {\n if (!this.shouldLog('info')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[INFO]', timestamp, args, this.options));\n }\n\n /**\n * Logs a warning message.\n * @param args - The arguments to log.\n */\n warn(...args: unknown[]): void {\n if (!this.shouldLog('warn')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[WARN]', timestamp, args, this.options));\n }\n\n /**\n * Logs an error message.\n * @param args - The arguments to log.\n */\n error(...args: unknown[]): void {\n if (!this.shouldLog('error')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[ERROR]', timestamp, args, this.options));\n }\n\n /**\n * Logs a debug message.\n * @param args - The arguments to log.\n */\n debug(...args: unknown[]): void {\n if (!this.shouldLog('debug')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[DEBUG]', timestamp, args, this.options));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAkB;AAGX,IAAM,kBAAkC;AAAA,EAC7C,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,QAAQ;AACV;AAEO,SAAS,gBACd,mBACA,OACA,OAAa,oBAAI,KAAK,GACd;AACR,QAAM,CAAC,EAAE,SAAS,IAAI,kBAAkB,OAAO,IAAI;AACnD,SAAO;AACT;AAEO,SAAS,SAAS,OAAe,cAA+B;AACrE,MAAI,CAAC,aAAc,QAAO;AAC1B,QAAM,SAAiC;AAAA,IACrC,UAAU,aAAAA,QAAM,KAAK,KAAK;AAAA,IAC1B,UAAU,aAAAA,QAAM,OAAO,KAAK;AAAA,IAC5B,WAAW,aAAAA,QAAM,IAAI,KAAK;AAAA,IAC1B,WAAW,aAAAA,QAAM,KAAK,KAAK;AAAA,EAC7B;AACA,SAAO,OAAO,KAAK,KAAK;AAC1B;AAEO,SAAS,UACd,OACA,WACA,MACA,SACwB;AACxB,QAAM,EAAE,WAAAC,YAAW,eAAe,KAAK,IAAI;AAC3C,QAAM,eAAe,SAAS,OAAO,YAAY;AACjD,MAAIA,YAAW;AACb,WAAO,CAACA,WAAU,cAAc,WAAW,IAAI,CAAC;AAAA,EAClD;AACA,SAAO,CAAC,GAAG,YAAY,IAAI,SAAS,IAAI,GAAG,IAAI;AACjD;;;ACpCA,IAAM,yBAAyB,CAC7B,OACA,OAAa,oBAAI,KAAK,MACM,CAAC,MAAM,KAAK,KAAK,YAAY,CAAC;AAE5D,IAAM,uBAAiD;AAAA,EAC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AAA;AAAA,EAMA,SAAS,MAAuB;AAC9B,QAAI,CAAC,KAAK,UAAU,OAAO,EAAG;AAC9B,UAAM,YAAY;AAAA,MAChB,KAAK,QAAQ;AAAA,MACb;AAAA,IACF;AACA,YAAQ,IAAI,GAAG,UAAU,WAAW,WAAW,MAAM,KAAK,OAAO,CAAC;AAAA,EACpE;AACF;","names":["chalk","formatLog"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils.ts","../src/logger.ts"],"sourcesContent":["import chalk from 'chalk';\nimport type { LoggerOptions, TimestampTypes } from './types';\n\nexport const TIMESTAMP_TYPES: TimestampTypes = {\n ISO: 'iso',\n Locale: 'locale',\n Custom: 'custom',\n};\n\nexport function formatTimestamp(\n formatTimestampFn: NonNullable<LoggerOptions['formatTimestamp']>,\n types: TimestampTypes,\n date: Date = new Date(),\n): string {\n const [, timestamp] = formatTimestampFn(types, date);\n return timestamp;\n}\n\nexport function getColor(level: string, enableColors: boolean): string {\n if (!enableColors) return level;\n const colors: Record<string, string> = {\n '[INFO]': chalk.blue(level),\n '[WARN]': chalk.yellow(level),\n '[ERROR]': chalk.red(level),\n '[DEBUG]': chalk.gray(level),\n };\n return colors[level] || level;\n}\n\nexport function formatLog(\n level: string,\n timestamp: string,\n args: unknown[],\n options: LoggerOptions,\n): [string, ...unknown[]] {\n const { formatLog, enableColors = true } = options;\n const coloredLevel = getColor(level, enableColors);\n if (formatLog) {\n return [formatLog(coloredLevel, timestamp, args)];\n }\n return [`${coloredLevel} ${timestamp}`, ...args];\n}\n","import type { LoggerOptions, LogLevel, TimestampTypes } from './types';\nimport { formatTimestamp, formatLog, TIMESTAMP_TYPES } from './utils';\n\nimport type { TimestampType } from './types';\n\nconst defaultFormatTimestamp = (\n types: TimestampTypes,\n date: Date = new Date(),\n): [TimestampType, string] => [types.ISO, date.toISOString()];\n\nconst LOG_LEVEL_PRIORITIES: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\n/**\n * A simple logger with colored outputs and timestamps.\n */\nexport class Logger {\n private options: LoggerOptions;\n private level: LogLevel;\n\n constructor(options: LoggerOptions = {}) {\n this.options = {\n enableColors: options.enableColors ?? true,\n formatTimestamp: options.formatTimestamp ?? defaultFormatTimestamp,\n formatLog: options.formatLog,\n };\n this.level = options.level ?? 'debug';\n }\n\n /**\n * Sets the minimum log level for filtering messages.\n * @param level - The log level to set.\n */\n setLevel(level: LogLevel): void {\n this.level = level;\n }\n\n /**\n * Checks if a log level should be output based on the current log level.\n * @param level - The log level to check.\n * @returns True if the message should be logged.\n */\n private shouldLog(level: LogLevel): boolean {\n return LOG_LEVEL_PRIORITIES[level] >= LOG_LEVEL_PRIORITIES[this.level];\n }\n\n /**\n * Logs an info message.\n * @param args - The arguments to log.\n */\n info(...args: unknown[]): void {\n if (!this.shouldLog('info')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[INFO]', timestamp, args, this.options));\n }\n\n /**\n * Logs a warning message.\n * @param args - The arguments to log.\n */\n warn(...args: unknown[]): void {\n if (!this.shouldLog('warn')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[WARN]', timestamp, args, this.options));\n }\n\n /**\n * Logs an error message.\n * @param args - The arguments to log.\n */\n error(...args: unknown[]): void {\n if (!this.shouldLog('error')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[ERROR]', timestamp, args, this.options));\n }\n\n /**\n * Logs a debug message.\n * @param args - The arguments to log.\n */\n debug(...args: unknown[]): void {\n if (!this.shouldLog('debug')) return;\n const timestamp = formatTimestamp(\n this.options.formatTimestamp!,\n TIMESTAMP_TYPES,\n );\n console.log(...formatLog('[DEBUG]', timestamp, args, this.options));\n }\n}\n"],"mappings":";AAAA,OAAO,WAAW;AAGX,IAAM,kBAAkC;AAAA,EAC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