node-ansi-logger 3.1.0 → 3.1.1

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/dist/logger.js CHANGED
@@ -1,7 +1,33 @@
1
+ /**
2
+ * This file contains the AnsiLogger .
3
+ *
4
+ * @file logger.ts
5
+ * @author Luca Liguori
6
+ * @created 2023-06-01
7
+ * @version 3.0.2
8
+ * @license Apache-2.0
9
+ *
10
+ * Copyright 2024, 2025, 2026 Luca Liguori.
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+ /* eslint-disable no-console */
25
+ // Node.js built-in modules
1
26
  import path from 'node:path';
2
27
  import * as fs from 'node:fs';
3
28
  import * as os from 'node:os';
4
29
  import { stringify } from './stringify.js';
30
+ // ANSI color codes and styles are defined here for use in the logger
5
31
  export const RESET = '';
6
32
  export const BRIGHT = '';
7
33
  export const DIM = '';
@@ -26,23 +52,28 @@ export const CYAN = '';
26
52
  export const LIGHT_GREY = '';
27
53
  export const GREY = '';
28
54
  export const WHITE = '';
29
- export const db = '';
30
- export const nf = '';
31
- export const nt = '';
32
- export const wr = '';
33
- export const er = '';
34
- export const ft = '';
35
- export const rs = '';
36
- export const rk = '';
37
- export const dn = '';
38
- export const gn = '';
39
- export const idn = '';
40
- export const ign = '';
41
- export const zb = '';
42
- export const hk = '';
43
- export const pl = '';
44
- export const id = '';
45
- export const or = '';
55
+ // ANSI color codes short form to use in the logger
56
+ export const db = ''; // Debug 247
57
+ export const nf = ''; // Info 255
58
+ export const nt = ''; // Notice
59
+ export const wr = ''; // Warn 220
60
+ export const er = ''; // Error
61
+ export const ft = ''; // Fatal
62
+ export const rs = ''; // Reset colors to default foreground and background
63
+ export const rk = ''; // Erase from cursor
64
+ // Used internally by plugins
65
+ export const dn = ''; // Display name device
66
+ export const gn = ''; // Display name group
67
+ export const idn = ''; // Inverted display name device
68
+ export const ign = ''; // Inverted display name group
69
+ export const zb = ''; // Zigbee
70
+ export const hk = ''; // Homekit
71
+ export const pl = ''; // payload
72
+ export const id = ''; // id or ieee_address or UUID
73
+ export const or = ''; // history
74
+ /**
75
+ * LogLevel enumeration to specify the logging level.
76
+ */
46
77
  export var LogLevel;
47
78
  (function (LogLevel) {
48
79
  LogLevel["NONE"] = "";
@@ -53,6 +84,9 @@ export var LogLevel;
53
84
  LogLevel["ERROR"] = "error";
54
85
  LogLevel["FATAL"] = "fatal";
55
86
  })(LogLevel || (LogLevel = {}));
87
+ /**
88
+ * TimestampFormat enumeration to specify the format of timestamps in log messages.
89
+ */
56
90
  export var TimestampFormat;
57
91
  (function (TimestampFormat) {
58
92
  TimestampFormat[TimestampFormat["ISO"] = 0] = "ISO";
@@ -63,6 +97,7 @@ export var TimestampFormat;
63
97
  TimestampFormat[TimestampFormat["HOMEBRIDGE"] = 5] = "HOMEBRIDGE";
64
98
  TimestampFormat[TimestampFormat["CUSTOM"] = 6] = "CUSTOM";
65
99
  })(TimestampFormat || (TimestampFormat = {}));
100
+ // Initialize the global variables
66
101
  if (typeof globalThis.__AnsiLoggerCallback__ === 'undefined')
67
102
  globalThis.__AnsiLoggerCallback__ = undefined;
68
103
  if (typeof globalThis.__AnsiLoggerCallbackLoglevel__ === 'undefined')
@@ -73,6 +108,10 @@ if (typeof globalThis.__AnsiLoggerFileLoglevel__ === 'undefined')
73
108
  globalThis.__AnsiLoggerFileLoglevel__ = undefined;
74
109
  if (typeof globalThis.__AnsiLoggerFileLogSize__ === 'undefined')
75
110
  globalThis.__AnsiLoggerFileLogSize__ = undefined;
111
+ /**
112
+ * AnsiLogger provides a customizable logging utility with ANSI color support.
113
+ * It allows for various configurations such as enabling debug logs, customizing log name, and more.
114
+ */
76
115
  export class AnsiLogger {
77
116
  _extLog;
78
117
  _logName;
@@ -84,59 +123,135 @@ export class AnsiLogger {
84
123
  _logCustomTimestampFormat;
85
124
  _logTimeStampColor = '';
86
125
  _logNameColor = '';
87
- _maxFileSize = 100000000;
126
+ _maxFileSize = 100000000; // 100MB
88
127
  logStartTime;
89
128
  callback = undefined;
129
+ /**
130
+ * Constructs a new AnsiLogger instance with optional configuration parameters.
131
+ *
132
+ * @param {AnsiLoggerParams} params - Configuration options for the logger.
133
+ */
90
134
  constructor(params) {
91
135
  this._extLog = params.extLog;
92
136
  this._logName = params.logName ?? 'NodeAnsiLogger';
93
- this._logLevel = params.logLevel ?? (params.logDebug === true ? "debug" : "info");
137
+ this._logLevel = params.logLevel ?? (params.logDebug === true ? "debug" /* LogLevel.DEBUG */ : "info" /* LogLevel.INFO */);
94
138
  this._logWithColors = params.logWithColors ?? true;
95
- this._logTimestampFormat = params.logTimestampFormat ?? 3;
139
+ this._logTimestampFormat = params.logTimestampFormat ?? 3 /* TimestampFormat.LOCAL_DATE_TIME */;
96
140
  this._logCustomTimestampFormat = params.logCustomTimestampFormat ?? 'yyyy-MM-dd HH:mm:ss';
97
141
  this.logStartTime = 0;
98
142
  }
143
+ /**
144
+ * Gets the name of the logger.
145
+ *
146
+ * @returns {string} The logger name.
147
+ */
99
148
  get logName() {
100
149
  return this._logName;
101
150
  }
151
+ /**
152
+ * Sets the log name for the logger.
153
+ *
154
+ * @param {string} name - The logger name to set.
155
+ */
102
156
  set logName(name) {
103
157
  this._logName = name;
104
158
  }
159
+ /**
160
+ * Gets the log level of the logger.
161
+ *
162
+ * @returns {LogLevel} The log level.
163
+ */
105
164
  get logLevel() {
106
165
  return this._logLevel;
107
166
  }
167
+ /**
168
+ * Sets the log level for the logger.
169
+ *
170
+ * @param {LogLevel} logLevel - The log level to set.
171
+ */
108
172
  set logLevel(logLevel) {
109
173
  this._logLevel = logLevel;
110
174
  }
175
+ /**
176
+ * Gets the logWithColors flag of the logger.
177
+ *
178
+ * @returns {boolean} The logWithColors parameter.
179
+ */
111
180
  get logWithColors() {
112
181
  return this._logWithColors;
113
182
  }
183
+ /**
184
+ * Sets the logWithColors flag of the logger.
185
+ *
186
+ * @param {boolean} logWithColors - The logWithColors parameter to set.
187
+ */
114
188
  set logWithColors(logWithColors) {
115
189
  this._logWithColors = logWithColors;
116
190
  }
191
+ /**
192
+ * Gets the log name color string of the logger.
193
+ *
194
+ * @returns {string} The log name color string.
195
+ */
117
196
  get logNameColor() {
118
197
  return this._logNameColor;
119
198
  }
199
+ /**
200
+ * Sets the log name color string for the logger.
201
+ *
202
+ * @param {string} color - The logger name color string to set.
203
+ */
120
204
  set logNameColor(color) {
121
205
  this._logNameColor = color;
122
206
  }
207
+ /**
208
+ * Gets the log timestamp format of the logger.
209
+ *
210
+ * @returns {TimestampFormat} The log timestamp format.
211
+ */
123
212
  get logTimestampFormat() {
124
213
  return this._logTimestampFormat;
125
214
  }
215
+ /**
216
+ * Sets the log timestamp format for the logger.
217
+ *
218
+ * @param {TimestampFormat} logTimestampFormat - The log timestamp format to set.
219
+ */
126
220
  set logTimestampFormat(logTimestampFormat) {
127
221
  this._logTimestampFormat = logTimestampFormat;
128
222
  }
223
+ /**
224
+ * Gets the custom log timestamp format of the logger.
225
+ *
226
+ * @returns {string} The custom log timestamp format.
227
+ */
129
228
  get logCustomTimestampFormat() {
130
229
  return this._logCustomTimestampFormat;
131
230
  }
231
+ /**
232
+ * Sets the custom log timestamp format for the logger.
233
+ *
234
+ * @param {string} logCustomTimestampFormat - The custom log timestamp format to set.
235
+ */
132
236
  set logCustomTimestampFormat(logCustomTimestampFormat) {
133
237
  this._logCustomTimestampFormat = logCustomTimestampFormat;
134
238
  }
239
+ /**
240
+ * Gets the file path of the log.
241
+ *
242
+ * @returns {string | undefined} The file path of the log, or undefined if not set.
243
+ */
135
244
  get logFilePath() {
136
245
  return this._logFilePath;
137
246
  }
247
+ /**
248
+ * Sets the file path for logging.
249
+ *
250
+ * @param {string | undefined} filePath - The file path to set for logging.
251
+ */
138
252
  set logFilePath(filePath) {
139
253
  if (filePath && typeof filePath === 'string' && filePath !== '') {
254
+ // Convert relative path to absolute path
140
255
  try {
141
256
  this._logFilePath = path.resolve(filePath);
142
257
  }
@@ -146,6 +261,7 @@ export class AnsiLogger {
146
261
  this._logFileSize = undefined;
147
262
  return;
148
263
  }
264
+ // Check if the file exists and unlink
149
265
  if (this._logFilePath && fs.existsSync(this._logFilePath)) {
150
266
  try {
151
267
  fs.unlinkSync(this._logFilePath);
@@ -164,19 +280,44 @@ export class AnsiLogger {
164
280
  this._logFileSize = undefined;
165
281
  }
166
282
  }
283
+ /**
284
+ * Gets the size of log file.
285
+ *
286
+ * @returns {number | undefined} The size of log file, or undefined if not set.
287
+ */
167
288
  get logFileSize() {
168
289
  return this._logFilePath && this._logFileSize ? this._logFileSize : undefined;
169
290
  }
291
+ /**
292
+ * Gets the max file size of the file loggers.
293
+ *
294
+ * @returns {number} The current maxFileSize.
295
+ */
170
296
  get maxFileSize() {
171
297
  return this._maxFileSize;
172
298
  }
299
+ /**
300
+ * Sets the max file size of the file loggers.
301
+ *
302
+ * @param {number} maxFileSize - The maxFileSize to set.
303
+ */
173
304
  set maxFileSize(maxFileSize) {
174
- this._maxFileSize = Math.min(maxFileSize, 500000000);
305
+ this._maxFileSize = Math.min(maxFileSize, 500000000); // 500MB
175
306
  }
307
+ /**
308
+ * Starts a timer with an optional message.
309
+ *
310
+ * @param {string} message - The message to log when starting the timer.
311
+ */
176
312
  startTimer(message) {
177
313
  this.logStartTime = Date.now();
178
314
  this.info(`Timer started ${message}`);
179
315
  }
316
+ /**
317
+ * Stops the timer started by startTimer and logs the elapsed time.
318
+ *
319
+ * @param {string} message - The message to log along with the elapsed time.
320
+ */
180
321
  stopTimer(message) {
181
322
  if (this.logStartTime !== 0) {
182
323
  const timePassed = Date.now() - this.logStartTime;
@@ -184,17 +325,40 @@ export class AnsiLogger {
184
325
  }
185
326
  this.logStartTime = 0;
186
327
  }
328
+ /**
329
+ * Sets the callback function to be used by the logger.
330
+ *
331
+ * @param {AnsiLoggerCallback} callback - The callback function.
332
+ */
187
333
  setCallback(callback) {
188
334
  this.callback = callback;
189
335
  }
336
+ /**
337
+ * Gets the callback function currently used by the logger.
338
+ *
339
+ * @returns {AnsiLoggerCallback | undefined} The callback function.
340
+ */
190
341
  getCallback() {
191
342
  return this.callback;
192
343
  }
193
- static setGlobalCallback(callback, callbackLevel = "debug") {
344
+ /**
345
+ * Sets the global callback function to be used by the logger.
346
+ *
347
+ * @param {AnsiLoggerCallback | undefined} callback - The callback function.
348
+ * @param {LogLevel} [callbackLevel] - The log level of the log file (default LogLevel.DEBUG).
349
+ *
350
+ * @returns {AnsiLoggerCallback | undefined} The path name of the log file.
351
+ */
352
+ static setGlobalCallback(callback, callbackLevel = "debug" /* LogLevel.DEBUG */) {
194
353
  __AnsiLoggerCallback__ = callback;
195
354
  __AnsiLoggerCallbackLoglevel__ = callbackLevel;
196
355
  return __AnsiLoggerCallback__;
197
356
  }
357
+ /**
358
+ * Gets the global callback function currently used by the logger.
359
+ *
360
+ * @returns {AnsiLoggerCallback | undefined} The callback function.
361
+ */
198
362
  static getGlobalCallback() {
199
363
  if (__AnsiLoggerCallback__) {
200
364
  return __AnsiLoggerCallback__;
@@ -203,6 +367,11 @@ export class AnsiLogger {
203
367
  return undefined;
204
368
  }
205
369
  }
370
+ /**
371
+ * Gets the global callback log level used by the logger.
372
+ *
373
+ * @returns {LogLevel | undefined} The log level of the global callback.
374
+ */
206
375
  static getGlobalCallbackLevel() {
207
376
  if (__AnsiLoggerCallbackLoglevel__) {
208
377
  return __AnsiLoggerCallbackLoglevel__;
@@ -211,13 +380,31 @@ export class AnsiLogger {
211
380
  return undefined;
212
381
  }
213
382
  }
214
- static setGlobalCallbackLevel(logLevel = "debug") {
383
+ /**
384
+ * Sets the global callback log level for the logger.
385
+ *
386
+ * @param {LogLevel} logLevel - The log level to set. Defaults to LogLevel.DEBUG.
387
+ *
388
+ * @returns {LogLevel | undefined} The log level that was set.
389
+ */
390
+ static setGlobalCallbackLevel(logLevel = "debug" /* LogLevel.DEBUG */) {
215
391
  __AnsiLoggerCallbackLoglevel__ = logLevel;
216
392
  return __AnsiLoggerCallbackLoglevel__;
217
393
  }
218
- static setGlobalLogfile(logfilePath, logfileLevel = "debug", unlink = false) {
394
+ /**
395
+ * Sets the global logfile to be used by the logger.
396
+ *
397
+ * @param {string} logfilePath - The path name of the log file.
398
+ * @param {LogLevel} logfileLevel - Optional: the log level of the log file. Default LogLevel.DEBUG.
399
+ * @param {boolean} unlink - Optional: whether to unlink (delete) the log file if it exists. Default false.
400
+ *
401
+ * @returns {string | undefined} The absolute path name of the log file.
402
+ */
403
+ static setGlobalLogfile(logfilePath, logfileLevel = "debug" /* LogLevel.DEBUG */, unlink = false) {
219
404
  if (logfilePath && typeof logfilePath === 'string' && logfilePath !== '') {
405
+ // Convert relative path to absolute path
220
406
  logfilePath = path.resolve(logfilePath);
407
+ // Check if the file exists and unlink it if requested
221
408
  if (unlink && fs.existsSync(logfilePath)) {
222
409
  try {
223
410
  fs.unlinkSync(logfilePath);
@@ -235,6 +422,11 @@ export class AnsiLogger {
235
422
  __AnsiLoggerFileLogSize__ = undefined;
236
423
  return undefined;
237
424
  }
425
+ /**
426
+ * Gets the global logfile currently used by the logger.
427
+ *
428
+ * @returns {string | undefined} The path name of the log file.
429
+ */
238
430
  static getGlobalLogfile() {
239
431
  if (__AnsiLoggerFilePath__) {
240
432
  return __AnsiLoggerFilePath__;
@@ -243,6 +435,11 @@ export class AnsiLogger {
243
435
  return undefined;
244
436
  }
245
437
  }
438
+ /**
439
+ * Gets the global logfile log level used by the loggers.
440
+ *
441
+ * @returns {LogLevel | undefined} The log level of the global logfile.
442
+ */
246
443
  static getGlobalLogfileLevel() {
247
444
  if (__AnsiLoggerFileLoglevel__) {
248
445
  return __AnsiLoggerFileLoglevel__;
@@ -251,50 +448,65 @@ export class AnsiLogger {
251
448
  return undefined;
252
449
  }
253
450
  }
451
+ /**
452
+ * Sets the global logfile log level used by the loggers.
453
+ *
454
+ * @param {LogLevel} logfileLevel - The global logfile log level used by the loggers.
455
+ *
456
+ * @returns {LogLevel | undefined} The log level of the global logfile.
457
+ */
254
458
  static setGlobalLogfileLevel(logfileLevel) {
255
459
  __AnsiLoggerFileLoglevel__ = logfileLevel;
256
460
  return __AnsiLoggerFileLoglevel__;
257
461
  }
462
+ /**
463
+ * Determines whether a log message with the given level should be logged based on the configured log level.
464
+ *
465
+ * @param {LogLevel} level - The level of the log message.
466
+ * @param {LogLevel | undefined} configuredLevel - The configured log level.
467
+ *
468
+ * @returns {boolean} A boolean indicating whether the log message should be logged.
469
+ */
258
470
  shouldLog(level, configuredLevel) {
259
471
  switch (level) {
260
- case "":
472
+ case "" /* LogLevel.NONE */:
261
473
  return false;
262
- case "debug":
263
- if (configuredLevel === "debug") {
474
+ case "debug" /* LogLevel.DEBUG */:
475
+ if (configuredLevel === "debug" /* LogLevel.DEBUG */) {
264
476
  return true;
265
477
  }
266
478
  break;
267
- case "info":
268
- if (configuredLevel === "debug" || configuredLevel === "info") {
479
+ case "info" /* LogLevel.INFO */:
480
+ if (configuredLevel === "debug" /* LogLevel.DEBUG */ || configuredLevel === "info" /* LogLevel.INFO */) {
269
481
  return true;
270
482
  }
271
483
  break;
272
- case "notice":
273
- if (configuredLevel === "debug" || configuredLevel === "info" || configuredLevel === "notice") {
484
+ case "notice" /* LogLevel.NOTICE */:
485
+ if (configuredLevel === "debug" /* LogLevel.DEBUG */ || configuredLevel === "info" /* LogLevel.INFO */ || configuredLevel === "notice" /* LogLevel.NOTICE */) {
274
486
  return true;
275
487
  }
276
488
  break;
277
- case "warn":
278
- if (configuredLevel === "debug" || configuredLevel === "info" || configuredLevel === "notice" || configuredLevel === "warn") {
489
+ case "warn" /* LogLevel.WARN */:
490
+ if (configuredLevel === "debug" /* LogLevel.DEBUG */ || configuredLevel === "info" /* LogLevel.INFO */ || configuredLevel === "notice" /* LogLevel.NOTICE */ || configuredLevel === "warn" /* LogLevel.WARN */) {
279
491
  return true;
280
492
  }
281
493
  break;
282
- case "error":
283
- if (configuredLevel === "debug" ||
284
- configuredLevel === "info" ||
285
- configuredLevel === "notice" ||
286
- configuredLevel === "warn" ||
287
- configuredLevel === "error") {
494
+ case "error" /* LogLevel.ERROR */:
495
+ if (configuredLevel === "debug" /* LogLevel.DEBUG */ ||
496
+ configuredLevel === "info" /* LogLevel.INFO */ ||
497
+ configuredLevel === "notice" /* LogLevel.NOTICE */ ||
498
+ configuredLevel === "warn" /* LogLevel.WARN */ ||
499
+ configuredLevel === "error" /* LogLevel.ERROR */) {
288
500
  return true;
289
501
  }
290
502
  break;
291
- case "fatal":
292
- if (configuredLevel === "debug" ||
293
- configuredLevel === "info" ||
294
- configuredLevel === "notice" ||
295
- configuredLevel === "warn" ||
296
- configuredLevel === "error" ||
297
- configuredLevel === "fatal") {
503
+ case "fatal" /* LogLevel.FATAL */:
504
+ if (configuredLevel === "debug" /* LogLevel.DEBUG */ ||
505
+ configuredLevel === "info" /* LogLevel.INFO */ ||
506
+ configuredLevel === "notice" /* LogLevel.NOTICE */ ||
507
+ configuredLevel === "warn" /* LogLevel.WARN */ ||
508
+ configuredLevel === "error" /* LogLevel.ERROR */ ||
509
+ configuredLevel === "fatal" /* LogLevel.FATAL */) {
298
510
  return true;
299
511
  }
300
512
  break;
@@ -303,13 +515,23 @@ export class AnsiLogger {
303
515
  }
304
516
  return false;
305
517
  }
518
+ /**
519
+ * Formats a Date object into a custom string format.
520
+ *
521
+ * @param {Date} date - The Date object to format.
522
+ * @param {string} formatString - The string format to use.
523
+ * @returns {string} The formatted date.
524
+ * It only handles years, months, days, hours, minutes, and seconds
525
+ * with this format 'yyyy-MM-dd HH:mm:ss'
526
+ */
306
527
  formatCustomTimestamp(date, formatString) {
307
528
  const year = date.getFullYear();
308
- const month = date.getMonth() + 1;
529
+ const month = date.getMonth() + 1; // getMonth() returns 0-11
309
530
  const day = date.getDate();
310
531
  const hours = date.getHours();
311
532
  const minutes = date.getMinutes();
312
533
  const seconds = date.getSeconds();
534
+ // Replace format tokens with actual values. Add more as needed.
313
535
  return formatString
314
536
  .replace('yyyy', year.toString())
315
537
  .replace('MM', month.toString().padStart(2, '0'))
@@ -318,9 +540,21 @@ export class AnsiLogger {
318
540
  .replace('mm', minutes.toString().padStart(2, '0'))
319
541
  .replace('ss', seconds.toString().padStart(2, '0'));
320
542
  }
543
+ /**
544
+ * Returns the current timestamp as a string.
545
+ *
546
+ * @returns {string} The current timestamp.
547
+ */
321
548
  now() {
322
549
  return this.getTimestamp();
323
550
  }
551
+ /**
552
+ * Returns the timestamp based on the configured format.
553
+ * If the log start time is set, it returns the time passed since the start time.
554
+ * Otherwise, it returns the current timestamp based on the configured format.
555
+ *
556
+ * @returns {string} The timestamp string.
557
+ */
324
558
  getTimestamp() {
325
559
  if (this.logStartTime !== 0) {
326
560
  const timePassed = Date.now() - this.logStartTime;
@@ -329,23 +563,23 @@ export class AnsiLogger {
329
563
  else {
330
564
  let timestamp;
331
565
  switch (this._logTimestampFormat) {
332
- case 1:
566
+ case 1 /* TimestampFormat.LOCAL_DATE */:
333
567
  timestamp = new Date().toLocaleDateString();
334
568
  break;
335
- case 2:
569
+ case 2 /* TimestampFormat.LOCAL_TIME */:
336
570
  timestamp = new Date().toLocaleTimeString();
337
571
  break;
338
- case 5:
339
- case 3:
572
+ case 5 /* TimestampFormat.HOMEBRIDGE */:
573
+ case 3 /* TimestampFormat.LOCAL_DATE_TIME */:
340
574
  timestamp = new Date().toLocaleString();
341
575
  break;
342
- case 0:
576
+ case 0 /* TimestampFormat.ISO */:
343
577
  timestamp = new Date().toISOString();
344
578
  break;
345
- case 4:
579
+ case 4 /* TimestampFormat.TIME_MILLIS */:
346
580
  timestamp = `${new Date().getHours().toString().padStart(2, '0')}:${new Date().getMinutes().toString().padStart(2, '0')}:${new Date().getSeconds().toString().padStart(2, '0')}.${new Date().getMilliseconds().toString().padStart(3, '0')}`;
347
581
  break;
348
- case 6:
582
+ case 6 /* TimestampFormat.CUSTOM */:
349
583
  timestamp = this.formatCustomTimestamp(new Date(), this._logCustomTimestampFormat);
350
584
  break;
351
585
  default:
@@ -355,6 +589,16 @@ export class AnsiLogger {
355
589
  return timestamp;
356
590
  }
357
591
  }
592
+ /**
593
+ * Writes a log message to a file.
594
+ *
595
+ * @param {string} filePath - The path of the file to write the log message to.
596
+ * @param {LogLevel} level - The log level of the message.
597
+ * @param {string} message - The log message.
598
+ * @param {...any[]} parameters - Additional parameters to include in the log message.
599
+ * @returns {number} - The length of the log message including the appended newline character.
600
+ */
601
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
358
602
  logToFile(filePath, level, message, ...parameters) {
359
603
  const parametersString = parameters
360
604
  .map((parameter) => {
@@ -367,6 +611,8 @@ export class AnsiLogger {
367
611
  return stringify(parameter);
368
612
  case 'string':
369
613
  return parameter;
614
+ // case 'undefined':
615
+ // return 'undefined';
370
616
  case 'function':
371
617
  return '(function)';
372
618
  default:
@@ -375,7 +621,9 @@ export class AnsiLogger {
375
621
  })
376
622
  .join(' ');
377
623
  let messageLog = `[${this.getTimestamp()}] [${this._logName}] [${level}] ` + message + ' ' + parametersString;
624
+ // messageLog = messageLog.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '').replace(/[\t\n\r]/g, '');
378
625
  messageLog = messageLog
626
+ // eslint-disable-next-line no-control-regex
379
627
  .replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '')
380
628
  .replaceAll('\t', ' ')
381
629
  .replaceAll('\r', '')
@@ -383,16 +631,28 @@ export class AnsiLogger {
383
631
  fs.appendFileSync(filePath, messageLog + os.EOL);
384
632
  return messageLog.length + 1;
385
633
  }
634
+ /**
635
+ * Logs a message with a specific level (e.g. debug, info, notice, warn, error, fatal) and additional parameters.
636
+ * This method formats the log message with ANSI colors based on the log level and other logger settings.
637
+ * It supports dynamic parameters for more detailed and formatted logging.
638
+ *
639
+ * @param {LogLevel} level - The severity level of the log message.
640
+ * @param {string} message - The primary log message to be displayed.
641
+ * @param {...any[]} parameters - Additional parameters to be logged. Supports any number of parameters.
642
+ */
643
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
386
644
  log(level, message, ...parameters) {
387
- const s1ln = '';
388
- const s2ln = '';
389
- const s3ln = '';
390
- const s4ln = '';
645
+ const s1ln = ''; // Highlight LogName Black on Cyan
646
+ const s2ln = ''; // Highlight LogName Black on White
647
+ const s3ln = ''; // Highlight LogName Black on Yellow
648
+ const s4ln = ''; // Highlight LogName Black on Red
391
649
  if (typeof level !== 'string' || level.startsWith === undefined || typeof message !== 'string' || message.startsWith === undefined) {
392
650
  return;
393
651
  }
652
+ // Local callback
394
653
  try {
395
654
  if (this.callback !== undefined && this.shouldLog(level, this._logLevel)) {
655
+ // Convert parameters to string and append to message
396
656
  const parametersString = parameters.length > 0 ? ' ' + parameters.join(' ') : '';
397
657
  const newMessage = message + parametersString;
398
658
  this.callback(level, this.getTimestamp(), this._logName, newMessage);
@@ -401,8 +661,10 @@ export class AnsiLogger {
401
661
  catch (error) {
402
662
  console.error('Error executing local callback:', error);
403
663
  }
664
+ // Global callback
404
665
  try {
405
666
  if (__AnsiLoggerCallback__ && __AnsiLoggerCallback__ !== undefined && this.shouldLog(level, __AnsiLoggerCallbackLoglevel__)) {
667
+ // Convert parameters to string and append to message
406
668
  const parametersString = parameters.length > 0 ? ' ' + parameters.join(' ') : '';
407
669
  const newMessage = message + parametersString;
408
670
  __AnsiLoggerCallback__(level, this.getTimestamp(), this._logName, newMessage);
@@ -411,6 +673,7 @@ export class AnsiLogger {
411
673
  catch (error) {
412
674
  console.error('Error executing global callback:', error);
413
675
  }
676
+ // Local file logger
414
677
  try {
415
678
  if (this.logFilePath !== undefined && this._logFileSize !== undefined && this._logFileSize < this._maxFileSize && this.shouldLog(level, this._logLevel)) {
416
679
  const size = this.logToFile(this.logFilePath, level, message, ...parameters);
@@ -423,6 +686,7 @@ export class AnsiLogger {
423
686
  catch (error) {
424
687
  console.error(`Error writing to the local log file ${this.logFilePath}:`, error);
425
688
  }
689
+ // Global file logger
426
690
  try {
427
691
  if (__AnsiLoggerFilePath__ &&
428
692
  __AnsiLoggerFilePath__ !== undefined &&
@@ -440,7 +704,7 @@ export class AnsiLogger {
440
704
  console.error(`Error writing to the global log file ${__AnsiLoggerFilePath__}:`, error);
441
705
  }
442
706
  if (this._extLog !== undefined) {
443
- if (level !== "") {
707
+ if (level !== "" /* LogLevel.NONE */) {
444
708
  this._extLog.log(level, message, ...parameters);
445
709
  }
446
710
  }
@@ -464,32 +728,32 @@ export class AnsiLogger {
464
728
  message = message.slice(1);
465
729
  }
466
730
  switch (level) {
467
- case "debug":
731
+ case "debug" /* LogLevel.DEBUG */:
468
732
  if (this.shouldLog(level, this._logLevel)) {
469
733
  console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${db}`, message + rs + rk, ...parameters);
470
734
  }
471
735
  break;
472
- case "info":
736
+ case "info" /* LogLevel.INFO */:
473
737
  if (this.shouldLog(level, this._logLevel)) {
474
738
  console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${nf}`, message + rs + rk, ...parameters);
475
739
  }
476
740
  break;
477
- case "notice":
741
+ case "notice" /* LogLevel.NOTICE */:
478
742
  if (this.shouldLog(level, this._logLevel)) {
479
743
  console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${nt}`, message + rs + rk, ...parameters);
480
744
  }
481
745
  break;
482
- case "warn":
746
+ case "warn" /* LogLevel.WARN */:
483
747
  if (this.shouldLog(level, this._logLevel)) {
484
748
  console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${wr}`, message + rs + rk, ...parameters);
485
749
  }
486
750
  break;
487
- case "error":
751
+ case "error" /* LogLevel.ERROR */:
488
752
  if (this.shouldLog(level, this._logLevel)) {
489
753
  console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${er}`, message + rs + rk, ...parameters);
490
754
  }
491
755
  break;
492
- case "fatal":
756
+ case "fatal" /* LogLevel.FATAL */:
493
757
  if (this.shouldLog(level, this._logLevel)) {
494
758
  console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${ft}`, message + rs + rk, ...parameters);
495
759
  }
@@ -500,42 +764,42 @@ export class AnsiLogger {
500
764
  }
501
765
  else {
502
766
  switch (level) {
503
- case "debug":
504
- if (this._logLevel === "debug") {
767
+ case "debug" /* LogLevel.DEBUG */:
768
+ if (this._logLevel === "debug" /* LogLevel.DEBUG */) {
505
769
  console.log(`[${this.getTimestamp()}] [${this._logName}] [${level}] ${message}`, ...parameters);
506
770
  }
507
771
  break;
508
- case "info":
509
- if (this._logLevel === "debug" || this._logLevel === "info") {
772
+ case "info" /* LogLevel.INFO */:
773
+ if (this._logLevel === "debug" /* LogLevel.DEBUG */ || this._logLevel === "info" /* LogLevel.INFO */) {
510
774
  console.log(`[${this.getTimestamp()}] [${this._logName}] [${level}] ${message}`, ...parameters);
511
775
  }
512
776
  break;
513
- case "notice":
514
- if (this._logLevel === "debug" || this._logLevel === "info" || this._logLevel === "notice") {
777
+ case "notice" /* LogLevel.NOTICE */:
778
+ if (this._logLevel === "debug" /* LogLevel.DEBUG */ || this._logLevel === "info" /* LogLevel.INFO */ || this._logLevel === "notice" /* LogLevel.NOTICE */) {
515
779
  console.log(`[${this.getTimestamp()}] [${this._logName}] [${level}] ${message}`, ...parameters);
516
780
  }
517
781
  break;
518
- case "warn":
519
- if (this._logLevel === "debug" || this._logLevel === "info" || this._logLevel === "notice" || this._logLevel === "warn") {
782
+ case "warn" /* LogLevel.WARN */:
783
+ if (this._logLevel === "debug" /* LogLevel.DEBUG */ || this._logLevel === "info" /* LogLevel.INFO */ || this._logLevel === "notice" /* LogLevel.NOTICE */ || this._logLevel === "warn" /* LogLevel.WARN */) {
520
784
  console.log(`[${this.getTimestamp()}] [${this._logName}] [${level}] ${message}`, ...parameters);
521
785
  }
522
786
  break;
523
- case "error":
524
- if (this._logLevel === "debug" ||
525
- this._logLevel === "info" ||
526
- this._logLevel === "notice" ||
527
- this._logLevel === "warn" ||
528
- this._logLevel === "error") {
787
+ case "error" /* LogLevel.ERROR */:
788
+ if (this._logLevel === "debug" /* LogLevel.DEBUG */ ||
789
+ this._logLevel === "info" /* LogLevel.INFO */ ||
790
+ this._logLevel === "notice" /* LogLevel.NOTICE */ ||
791
+ this._logLevel === "warn" /* LogLevel.WARN */ ||
792
+ this._logLevel === "error" /* LogLevel.ERROR */) {
529
793
  console.log(`[${this.getTimestamp()}] [${this._logName}] [${level}] ${message}`, ...parameters);
530
794
  }
531
795
  break;
532
- case "fatal":
533
- if (this._logLevel === "debug" ||
534
- this._logLevel === "info" ||
535
- this._logLevel === "notice" ||
536
- this._logLevel === "warn" ||
537
- this._logLevel === "error" ||
538
- this._logLevel === "fatal") {
796
+ case "fatal" /* LogLevel.FATAL */:
797
+ if (this._logLevel === "debug" /* LogLevel.DEBUG */ ||
798
+ this._logLevel === "info" /* LogLevel.INFO */ ||
799
+ this._logLevel === "notice" /* LogLevel.NOTICE */ ||
800
+ this._logLevel === "warn" /* LogLevel.WARN */ ||
801
+ this._logLevel === "error" /* LogLevel.ERROR */ ||
802
+ this._logLevel === "fatal" /* LogLevel.FATAL */) {
539
803
  console.log(`[${this.getTimestamp()}] [${this._logName}] [${level}] ${message}`, ...parameters);
540
804
  }
541
805
  break;
@@ -545,22 +809,108 @@ export class AnsiLogger {
545
809
  }
546
810
  }
547
811
  }
812
+ /**
813
+ * Logs a debug message if debug logging is enabled. This is a convenience method that delegates to the `log` method with the `LogLevel.DEBUG` level.
814
+ *
815
+ * @param {string} message - The message to log.
816
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
817
+ */
818
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
548
819
  debug(message, ...parameters) {
549
- this.log("debug", message, ...parameters);
550
- }
820
+ this.log("debug" /* LogLevel.DEBUG */, message, ...parameters);
821
+ }
822
+ /**
823
+ * Logs an informational message. This is a convenience method that delegates to the `log` method with the `LogLevel.INFO` level.
824
+ *
825
+ * @param {string} message - The message to log.
826
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
827
+ */
828
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
551
829
  info(message, ...parameters) {
552
- this.log("info", message, ...parameters);
553
- }
830
+ this.log("info" /* LogLevel.INFO */, message, ...parameters);
831
+ }
832
+ /**
833
+ * Logs a notice message. This is a convenience method that delegates to the `log` method with the `LogLevel.NOTICE` level.
834
+ *
835
+ * @param {string} message - The message to log.
836
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
837
+ */
838
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
554
839
  notice(message, ...parameters) {
555
- this.log("notice", message, ...parameters);
556
- }
840
+ this.log("notice" /* LogLevel.NOTICE */, message, ...parameters);
841
+ }
842
+ /**
843
+ * Logs a warning message. This is a convenience method that delegates to the `log` method with the `LogLevel.WARN` level.
844
+ *
845
+ * @param {string} message - The message to log.
846
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
847
+ */
848
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
557
849
  warn(message, ...parameters) {
558
- this.log("warn", message, ...parameters);
559
- }
850
+ this.log("warn" /* LogLevel.WARN */, message, ...parameters);
851
+ }
852
+ /**
853
+ * Logs an error message. This is a convenience method that delegates to the `log` method with the `LogLevel.ERROR` level.
854
+ *
855
+ * @param {string} message - The message to log.
856
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
857
+ */
858
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
560
859
  error(message, ...parameters) {
561
- this.log("error", message, ...parameters);
562
- }
860
+ this.log("error" /* LogLevel.ERROR */, message, ...parameters);
861
+ }
862
+ /**
863
+ * Logs a fatal message. This is a convenience method that delegates to the `log` method with the `LogLevel.FATAL` level.
864
+ *
865
+ * @param {string} message - The message to log.
866
+ * @param {...any[]} parameters - Additional parameters to be included in the log message. Supports any number of parameters.
867
+ */
868
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
563
869
  fatal(message, ...parameters) {
564
- this.log("fatal", message, ...parameters);
870
+ this.log("fatal" /* LogLevel.FATAL */, message, ...parameters);
565
871
  }
566
872
  }
873
+ /*
874
+  - Reset (clear color)
875
+  - Bold
876
+  - Italic
877
+  - Underline
878
+  - Erase the line from cursor
879
+
880
+  - Black
881
+  - Red
882
+  - Green
883
+  - Yellow
884
+  - Blue
885
+  - Magenta
886
+  - Cyan
887
+  - White
888
+
889
+ [90-97m - Bright
890
+
891
+  - Black background
892
+  - Red background
893
+  - Green background
894
+  - Yellow background
895
+  - Blue background
896
+  - Magenta background
897
+  - Cyan background
898
+  - White background
899
+
900
+ [100-107m - Bright background
901
+
902
+  // Orange
903
+
904
+ RGB foreground
905
+ [38;2;<R>;<G>;<B>m
906
+
907
+ RGB background
908
+ [48;2;<R>;<G>;<B>m
909
+
910
+ 256 colors foreground
911
+ [38;5;<FG COLOR>m
912
+
913
+ 256 colors background
914
+ [48;5;<BG COLOR>m
915
+ */
916
+ //# sourceMappingURL=logger.js.map