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