node-ansi-logger 2.0.7 → 3.0.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
@@ -22,52 +22,53 @@
22
22
  * limitations under the License.
23
23
  */
24
24
  import path from 'path';
25
- import { colorStringify, debugStringify, historyStringify, mqttStringify, payloadStringify } from './stringify.js';
25
+ import { stringify } from './stringify.js';
26
26
  import * as fs from 'fs';
27
+ import * as os from 'os';
27
28
  // ANSI color codes and styles are defined here for use in the logger
28
- export const RESET = '\x1b[40;0m';
29
- export const BRIGHT = '\x1b[1m';
30
- export const DIM = '\x1b[2m';
31
- export const NORMAL = '\x1b[22m';
32
- export const UNDERLINE = '\x1b[4m';
33
- export const UNDERLINEOFF = '\x1b[24m';
34
- export const BLINK = '\x1b[5m';
35
- export const BLINKOFF = '\x1b[25m';
36
- export const REVERSE = '\x1b[7m';
37
- export const REVERSEOFF = '\x1b[27m';
38
- export const HIDDEN = '\x1b[8m';
39
- export const HIDDENOFF = '\x1b[28m';
40
- export const CURSORSAVE = '\x1b[s';
41
- export const CURSORRESTORE = '\x1b[u';
42
- export const BLACK = '\x1b[30m';
43
- export const RED = '\x1b[31m';
44
- export const GREEN = '\x1b[32m';
45
- export const YELLOW = '\x1b[33m';
46
- export const BLUE = '\x1b[34m';
47
- export const MAGENTA = '\x1b[35m';
48
- export const CYAN = '\x1b[36m';
49
- export const LIGHT_GREY = '\x1b[37m';
50
- export const GREY = '\x1b[90m';
51
- export const WHITE = '\x1b[97m';
29
+ export const RESET = '';
30
+ export const BRIGHT = '';
31
+ export const DIM = '';
32
+ export const NORMAL = '';
33
+ export const UNDERLINE = '';
34
+ export const UNDERLINEOFF = '';
35
+ export const BLINK = '';
36
+ export const BLINKOFF = '';
37
+ export const REVERSE = '';
38
+ export const REVERSEOFF = '';
39
+ export const HIDDEN = '';
40
+ export const HIDDENOFF = '';
41
+ export const CURSORSAVE = '';
42
+ export const CURSORRESTORE = '';
43
+ export const BLACK = '';
44
+ export const RED = '';
45
+ export const GREEN = '';
46
+ export const YELLOW = '';
47
+ export const BLUE = '';
48
+ export const MAGENTA = '';
49
+ export const CYAN = '';
50
+ export const LIGHT_GREY = '';
51
+ export const GREY = '';
52
+ export const WHITE = '';
52
53
  // ANSI color codes short form to use in the logger
53
- export const db = '\x1b[38;5;245m'; // Debug 247
54
- export const nf = '\x1b[38;5;252m'; // Info 255
55
- export const nt = '\x1b[38;5;2m'; // Notice
56
- export const wr = '\x1b[38;5;220m'; // Warn 220
57
- export const er = '\x1b[38;5;1m'; // Error
58
- export const ft = '\x1b[38;5;9m'; // Fatal
59
- export const rs = '\x1b[40;0m'; // Reset colors to default foreground and background
60
- export const rk = '\x1b[K'; // Erase from cursor
54
+ export const db = ''; // Debug 247
55
+ export const nf = ''; // Info 255
56
+ export const nt = ''; // Notice
57
+ export const wr = ''; // Warn 220
58
+ export const er = ''; // Error
59
+ export const ft = ''; // Fatal
60
+ export const rs = ''; // Reset colors to default foreground and background
61
+ export const rk = ''; // Erase from cursor
61
62
  // Used internally by plugins
62
- export const dn = '\x1b[38;5;33m'; // Display name device
63
- export const gn = '\x1b[38;5;35m'; // Display name group
64
- export const idn = '\x1b[48;5;21m\x1b[38;5;255m'; // Inverted display name device
65
- export const ign = '\x1b[48;5;22m\x1b[38;5;255m'; // Inverted display name group
66
- export const zb = '\x1b[38;5;207m'; // Zigbee
67
- export const hk = '\x1b[38;5;79m'; // Homekit
68
- export const pl = '\x1b[32m'; // payload
69
- export const id = '\x1b[37;44m'; // id or ieee_address or UUID
70
- export const or = '\x1b[38;5;208m'; // history
63
+ export const dn = ''; // Display name device
64
+ export const gn = ''; // Display name group
65
+ export const idn = ''; // Inverted display name device
66
+ export const ign = ''; // Inverted display name group
67
+ export const zb = ''; // Zigbee
68
+ export const hk = ''; // Homekit
69
+ export const pl = ''; // payload
70
+ export const id = ''; // id or ieee_address or UUID
71
+ export const or = ''; // history
71
72
  /**
72
73
  * LogLevel enumeration to specify the logging level.
73
74
  */
@@ -103,18 +104,24 @@ if (typeof globalThis.__AnsiLoggerFilePath__ === 'undefined')
103
104
  globalThis.__AnsiLoggerFilePath__ = undefined;
104
105
  if (typeof globalThis.__AnsiLoggerFileLoglevel__ === 'undefined')
105
106
  globalThis.__AnsiLoggerFileLoglevel__ = undefined;
107
+ if (typeof globalThis.__AnsiLoggerFileLogSize__ === 'undefined')
108
+ globalThis.__AnsiLoggerFileLogSize__ = undefined;
106
109
  /**
107
110
  * AnsiLogger provides a customizable logging utility with ANSI color support.
108
111
  * It allows for various configurations such as enabling debug logs, customizing log name, and more.
109
112
  */
110
113
  export class AnsiLogger {
111
- _hbLog;
114
+ _extLog;
112
115
  _logName;
113
116
  _logFilePath;
117
+ _logFileSize;
114
118
  _logLevel;
115
119
  _logWithColors;
116
120
  _logTimestampFormat;
117
121
  _logCustomTimestampFormat;
122
+ _logTimeStampColor = '';
123
+ _logNameColor = '';
124
+ _maxFileSize = 100000000; // 100MB
118
125
  logStartTime;
119
126
  callback = undefined;
120
127
  /**
@@ -122,7 +129,7 @@ export class AnsiLogger {
122
129
  * @param {AnsiLoggerParams} optionalParams - Configuration options for the logger.
123
130
  */
124
131
  constructor(params) {
125
- this._hbLog = params.hbLog;
132
+ this._extLog = params.extLog;
126
133
  this._logName = params.logName ?? 'NodeAnsiLogger';
127
134
  this._logLevel = params.logLevel ?? (params.logDebug === true ? "debug" /* LogLevel.DEBUG */ : "info" /* LogLevel.INFO */);
128
135
  this._logWithColors = params.logWithColors ?? true;
@@ -144,25 +151,6 @@ export class AnsiLogger {
144
151
  set logName(name) {
145
152
  this._logName = name;
146
153
  }
147
- /**
148
- * Sets the name of the logger.
149
- * @param {string} name - The new name for the logger.
150
- * @deprecated Use logName getter and setter instead.
151
- */
152
- setLogName(name) {
153
- this.logName = name;
154
- }
155
- /**
156
- * Enables or disables debug logging.
157
- * @param {boolean} logDebug - Flag to enable or disable debug logging.
158
- * @deprecated Use logLevel getter and setter instead.
159
- */
160
- setLogDebug(logDebug) {
161
- if (logDebug)
162
- this.logLevel = "debug" /* LogLevel.DEBUG */;
163
- else
164
- this.logLevel = "info" /* LogLevel.INFO */;
165
- }
166
154
  /**
167
155
  * Gets the log level of the logger.
168
156
  * @returns {LogLevel} The log level.
@@ -191,6 +179,50 @@ export class AnsiLogger {
191
179
  set logWithColors(logWithColors) {
192
180
  this._logWithColors = logWithColors;
193
181
  }
182
+ /**
183
+ * Gets the log name color string of the logger.
184
+ *
185
+ * @returns {string} The log name color string.
186
+ */
187
+ get logNameColor() {
188
+ return this._logNameColor;
189
+ }
190
+ /**
191
+ * Sets the log name color string for the logger.
192
+ *
193
+ * @param {string} color - The logger name color string to set.
194
+ */
195
+ set logNameColor(color) {
196
+ this._logNameColor = color;
197
+ }
198
+ /**
199
+ * Gets the log timestamp format of the logger.
200
+ * @returns {TimestampFormat} The log timestamp format.
201
+ */
202
+ get logTimestampFormat() {
203
+ return this._logTimestampFormat;
204
+ }
205
+ /**
206
+ * Sets the log timestamp format for the logger.
207
+ * @param {TimestampFormat} logTimestampFormat - The log timestamp format to set.
208
+ */
209
+ set logTimestampFormat(logTimestampFormat) {
210
+ this._logTimestampFormat = logTimestampFormat;
211
+ }
212
+ /**
213
+ * Gets the custom log timestamp format of the logger.
214
+ * @returns {string} The custom log timestamp format.
215
+ */
216
+ get logCustomTimestampFormat() {
217
+ return this._logCustomTimestampFormat;
218
+ }
219
+ /**
220
+ * Sets the custom log timestamp format for the logger.
221
+ * @param {string} logCustomTimestampFormat - The custom log timestamp format to set.
222
+ */
223
+ set logCustomTimestampFormat(logCustomTimestampFormat) {
224
+ this._logCustomTimestampFormat = logCustomTimestampFormat;
225
+ }
194
226
  /**
195
227
  * Gets the file path of the log.
196
228
  *
@@ -202,19 +234,21 @@ export class AnsiLogger {
202
234
  /**
203
235
  * Sets the file path for logging.
204
236
  *
205
- * @param {string | undefined} value - The file path to set for logging.
237
+ * @param {string | undefined} filePath - The file path to set for logging.
206
238
  */
207
- set logFilePath(value) {
208
- if (value && typeof value === 'string' && value !== '') {
239
+ set logFilePath(filePath) {
240
+ if (filePath && typeof filePath === 'string' && filePath !== '') {
209
241
  // Convert relative path to absolute path
210
242
  try {
211
- this._logFilePath = path.resolve(value);
243
+ this._logFilePath = path.resolve(filePath);
212
244
  }
213
245
  catch (error) {
214
- console.error(`Error resolving log file path: ${error instanceof Error ? error.message : error}`);
246
+ console.error(`Error resolving log file path ${CYAN}${filePath}${er}: ${error instanceof Error ? error.message : error}`);
215
247
  this._logFilePath = undefined;
248
+ this._logFileSize = undefined;
216
249
  return;
217
250
  }
251
+ // Check if the file exists and unlink
218
252
  if (this._logFilePath && fs.existsSync(this._logFilePath)) {
219
253
  try {
220
254
  fs.unlinkSync(this._logFilePath);
@@ -222,34 +256,38 @@ export class AnsiLogger {
222
256
  catch (error) {
223
257
  console.error(`${er}Error unlinking the log file ${CYAN}${this._logFilePath}${er}: ${error instanceof Error ? error.message : error}`);
224
258
  this._logFilePath = undefined;
259
+ this._logFileSize = undefined;
260
+ return;
225
261
  }
226
262
  }
263
+ this._logFileSize = 0;
227
264
  }
228
265
  else {
229
266
  this._logFilePath = undefined;
267
+ this._logFileSize = undefined;
230
268
  }
231
269
  }
232
270
  /**
233
- * Enables or disables logging with ANSI colors.
234
- * @param {boolean} logWithColors - Flag to enable or disable ANSI color logging.
235
- * @deprecated Use logWithColors getter and setter instead.
271
+ * Gets the size of log file.
272
+ *
273
+ * @returns {number | undefined} The size of log file, or undefined if not set.
236
274
  */
237
- setlogWithColors(logWithColors) {
238
- this._logWithColors = logWithColors;
275
+ get logFileSize() {
276
+ return this._logFilePath && this._logFileSize ? this._logFileSize : undefined;
239
277
  }
240
278
  /**
241
- * Sets the timestamp format for log messages.
242
- * @param {TimestampFormat} format - The timestamp format to use.
279
+ * Gets the max file size of the file loggers.
280
+ * @returns {number} The current maxFileSize.
243
281
  */
244
- setLogTimestampFormat(format) {
245
- this._logTimestampFormat = format;
282
+ get maxFileSize() {
283
+ return this._maxFileSize;
246
284
  }
247
285
  /**
248
- * Sets a custom timestamp format for log messages.
249
- * @param {string} format - The custom timestamp format string.
286
+ * Sets the max file size of the file loggers.
287
+ * @param {number} maxFileSize - The maxFileSize to set.
250
288
  */
251
- setLogCustomTimestampFormat(format) {
252
- this._logCustomTimestampFormat = format;
289
+ set maxFileSize(maxFileSize) {
290
+ this._maxFileSize = Math.min(maxFileSize, 100000000); // 100MB
253
291
  }
254
292
  /**
255
293
  * Starts a timer with an optional message.
@@ -286,8 +324,8 @@ export class AnsiLogger {
286
324
  }
287
325
  /**
288
326
  * Sets the global callback function to be used by the logger.
289
- * @param {AnsiLoggerCallback} callback - The callback function.
290
- * @param {LogLevel} callbackLevel - The log level of the log file.
327
+ * @param {AnsiLoggerCallback | undefined} callback - The callback function.
328
+ * @param {LogLevel} [callbackLevel=LogLevel.DEBUG] - The log level of the log file (default LogLevel.DEBUG).
291
329
  *
292
330
  * @returns {AnsiLoggerCallback | undefined} The path name of the log file.
293
331
  */
@@ -321,18 +359,30 @@ export class AnsiLogger {
321
359
  return undefined;
322
360
  }
323
361
  }
362
+ /**
363
+ * Sets the global callback log level for the logger.
364
+ *
365
+ * @param {LogLevel} logLevel - The log level to set. Defaults to LogLevel.DEBUG.
366
+ *
367
+ * @returns {LogLevel | undefined} The log level that was set.
368
+ */
369
+ static setGlobalCallbackLevel(logLevel = "debug" /* LogLevel.DEBUG */) {
370
+ __AnsiLoggerCallbackLoglevel__ = logLevel;
371
+ return __AnsiLoggerCallbackLoglevel__;
372
+ }
324
373
  /**
325
374
  * Sets the global logfile to be used by the logger.
326
375
  * @param {string} logfilePath - The path name of the log file.
327
- * @param {LogLevel} logfileLevel - The log level of the log file.
328
- * @param {boolean} unlink - Whether to unlink (delete) the log file if it exists Default false.
376
+ * @param {LogLevel} logfileLevel - Optional: the log level of the log file. Default LogLevel.DEBUG.
377
+ * @param {boolean} unlink - Optional: whether to unlink (delete) the log file if it exists. Default false.
329
378
  *
330
379
  * @returns {string | undefined} The absolute path name of the log file.
331
380
  */
332
381
  static setGlobalLogfile(logfilePath, logfileLevel = "debug" /* LogLevel.DEBUG */, unlink = false) {
333
- if (logfilePath) {
382
+ if (logfilePath && typeof logfilePath === 'string' && logfilePath !== '') {
334
383
  // Convert relative path to absolute path
335
384
  logfilePath = path.resolve(logfilePath);
385
+ // Check if the file exists and unlink it if requested
336
386
  if (unlink && fs.existsSync(logfilePath)) {
337
387
  try {
338
388
  fs.unlinkSync(logfilePath);
@@ -341,10 +391,14 @@ export class AnsiLogger {
341
391
  console.error(`${er}Error unlinking the log file ${CYAN}${logfilePath}${er}: ${error instanceof Error ? error.message : error}`);
342
392
  }
343
393
  }
394
+ __AnsiLoggerFilePath__ = logfilePath;
395
+ __AnsiLoggerFileLoglevel__ = logfileLevel;
396
+ __AnsiLoggerFileLogSize__ = 0;
397
+ return __AnsiLoggerFilePath__;
344
398
  }
345
- __AnsiLoggerFilePath__ = logfilePath;
346
- __AnsiLoggerFileLoglevel__ = logfileLevel;
347
- return __AnsiLoggerFilePath__;
399
+ __AnsiLoggerFilePath__ = undefined;
400
+ __AnsiLoggerFileLogSize__ = undefined;
401
+ return undefined;
348
402
  }
349
403
  /**
350
404
  * Gets the global logfile currently used by the logger.
@@ -360,9 +414,9 @@ export class AnsiLogger {
360
414
  }
361
415
  }
362
416
  /**
363
- * Gets the global logfile log level used by the logger.
417
+ * Gets the global logfile log level used by the loggers.
364
418
  *
365
- * @returns {LogLevel | undefined} The log level of the log file.
419
+ * @returns {LogLevel | undefined} The log level of the global logfile.
366
420
  */
367
421
  static getGlobalLogfileLevel() {
368
422
  if (__AnsiLoggerFileLoglevel__) {
@@ -372,6 +426,17 @@ export class AnsiLogger {
372
426
  return undefined;
373
427
  }
374
428
  }
429
+ /**
430
+ * Sets the global logfile log level used by the loggers.
431
+ *
432
+ * @param {LogLevel} logfileLevel - The global logfile log level used by the loggers.
433
+ *
434
+ * @returns {LogLevel | undefined} The log level of the global logfile.
435
+ */
436
+ static setGlobalLogfileLevel(logfileLevel) {
437
+ __AnsiLoggerFileLoglevel__ = logfileLevel;
438
+ return __AnsiLoggerFileLoglevel__;
439
+ }
375
440
  /**
376
441
  * Determines whether a log message with the given level should be logged based on the configured log level.
377
442
  *
@@ -382,6 +447,8 @@ export class AnsiLogger {
382
447
  */
383
448
  shouldLog(level, configuredLevel) {
384
449
  switch (level) {
450
+ case "" /* LogLevel.NONE */:
451
+ return false;
385
452
  case "debug" /* LogLevel.DEBUG */:
386
453
  if (configuredLevel === "debug" /* LogLevel.DEBUG */) {
387
454
  return true;
@@ -495,6 +562,15 @@ export class AnsiLogger {
495
562
  return timestamp;
496
563
  }
497
564
  }
565
+ /**
566
+ * Writes a log message to a file.
567
+ *
568
+ * @param {string} filePath - The path of the file to write the log message to.
569
+ * @param {LogLevel} level - The log level of the message.
570
+ * @param {string} message - The log message.
571
+ * @param {...any[]} parameters - Additional parameters to include in the log message.
572
+ * @returns {number} - The length of the log message including the appended newline character.
573
+ */
498
574
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
499
575
  logToFile(filePath, level, message, ...parameters) {
500
576
  const parametersString = parameters
@@ -503,30 +579,30 @@ export class AnsiLogger {
503
579
  return 'null';
504
580
  if (parameter === undefined)
505
581
  return 'undefined';
506
- if (Array.isArray(parameter)) {
507
- return ('[' +
508
- parameter.map((item) => (item === null ? 'null' : item === undefined ? 'undefined' : typeof item === 'object' ? debugStringify(item) : item.toString())).join(', ') +
509
- ']');
510
- }
511
582
  switch (typeof parameter) {
512
583
  case 'object':
513
- return debugStringify(parameter);
584
+ return stringify(parameter);
514
585
  case 'string':
515
586
  return parameter;
516
587
  case 'undefined':
517
588
  return 'undefined';
518
589
  case 'function':
519
- return 'function';
590
+ return '(function)';
520
591
  default:
521
592
  return parameter.toString();
522
593
  }
523
594
  })
524
595
  .join(' ');
525
- let messageLog = `[${this.getTimestamp()}] [${this._logName}] [${level}] ` + message + parametersString;
596
+ let messageLog = `[${this.getTimestamp()}] [${this._logName}] [${level}] ` + message + ' ' + parametersString;
526
597
  // messageLog = messageLog.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '').replace(/[\t\n\r]/g, '');
527
- // eslint-disable-next-line no-control-regex
528
- messageLog = messageLog.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
529
- fs.appendFileSync(filePath, messageLog + '\n');
598
+ messageLog = messageLog
599
+ // eslint-disable-next-line no-control-regex
600
+ .replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '')
601
+ .replaceAll('\t', ' ')
602
+ .replaceAll('\r', '')
603
+ .replaceAll('\n', os.EOL);
604
+ fs.appendFileSync(filePath, messageLog + os.EOL);
605
+ return messageLog.length + 1;
530
606
  }
531
607
  /**
532
608
  * Logs a message with a specific level (e.g. debug, info, notice, warn, error, fatal) and additional parameters.
@@ -539,12 +615,14 @@ export class AnsiLogger {
539
615
  */
540
616
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
541
617
  log(level, message, ...parameters) {
542
- const ts = '\x1b[38;5;245m'; // TimeStamp White medium
543
- const ln = '\x1b[38;5;31m'; // LogName Cyan
544
- const s1ln = '\x1b[38;5;0;48;5;31m'; // Highlight LogName Black on Cyan
545
- const s2ln = '\x1b[38;5;0;48;5;255m'; // Highlight LogName Black on White
546
- const s3ln = '\x1b[38;5;0;48;5;220m'; // Highlight LogName Black on Yellow
547
- const s4ln = '\x1b[38;5;0;48;5;9m'; // Highlight LogName Black on Red
618
+ const s1ln = ''; // Highlight LogName Black on Cyan
619
+ const s2ln = ''; // Highlight LogName Black on White
620
+ const s3ln = ''; // Highlight LogName Black on Yellow
621
+ const s4ln = ''; // Highlight LogName Black on Red
622
+ if (typeof level !== 'string' || level.startsWith === undefined || typeof message !== 'string' || message.startsWith === undefined) {
623
+ return;
624
+ }
625
+ // Local callback
548
626
  try {
549
627
  if (this.callback !== undefined && this.shouldLog(level, this._logLevel)) {
550
628
  // Convert parameters to string and append to message
@@ -556,6 +634,7 @@ export class AnsiLogger {
556
634
  catch (error) {
557
635
  console.error('Error executing local callback:', error);
558
636
  }
637
+ // Global callback
559
638
  try {
560
639
  if (__AnsiLoggerCallback__ && __AnsiLoggerCallback__ !== undefined && this.shouldLog(level, __AnsiLoggerCallbackLoglevel__)) {
561
640
  // Convert parameters to string and append to message
@@ -567,34 +646,45 @@ export class AnsiLogger {
567
646
  catch (error) {
568
647
  console.error('Error executing global callback:', error);
569
648
  }
649
+ // Local file logger
570
650
  try {
571
- if (this.logFilePath && this.shouldLog(level, this._logLevel)) {
572
- this.logToFile(this.logFilePath, level, message, ...parameters);
651
+ if (this.logFilePath !== undefined && this._logFileSize !== undefined && this._logFileSize < this._maxFileSize && this.shouldLog(level, this._logLevel)) {
652
+ const size = this.logToFile(this.logFilePath, level, message, ...parameters);
653
+ this._logFileSize += size;
654
+ if (this._logFileSize >= this._maxFileSize) {
655
+ fs.appendFileSync(this.logFilePath, 'Logging on file has been stoppped because the file size is greater then 100MB.\n');
656
+ }
573
657
  }
574
658
  }
575
659
  catch (error) {
576
660
  console.error(`Error writing to the local log file ${this.logFilePath}:`, error);
577
661
  }
662
+ // Global file logger
578
663
  try {
579
- if (__AnsiLoggerFilePath__ && __AnsiLoggerFilePath__ !== undefined && this.shouldLog(level, __AnsiLoggerFileLoglevel__)) {
580
- this.logToFile(__AnsiLoggerFilePath__, level, message, ...parameters);
664
+ if (__AnsiLoggerFilePath__ &&
665
+ __AnsiLoggerFilePath__ !== undefined &&
666
+ __AnsiLoggerFileLogSize__ !== undefined &&
667
+ __AnsiLoggerFileLogSize__ < this._maxFileSize &&
668
+ this.shouldLog(level, __AnsiLoggerFileLoglevel__)) {
669
+ const size = this.logToFile(__AnsiLoggerFilePath__, level, message, ...parameters);
670
+ __AnsiLoggerFileLogSize__ += size;
671
+ if (__AnsiLoggerFileLogSize__ >= this._maxFileSize) {
672
+ fs.appendFileSync(__AnsiLoggerFilePath__, 'Logging on file has been stoppped because the file size is greater then 100MB.\n');
673
+ }
581
674
  }
582
675
  }
583
676
  catch (error) {
584
677
  console.error(`Error writing to the global log file ${__AnsiLoggerFilePath__}:`, error);
585
678
  }
586
- if (this._hbLog !== undefined) {
679
+ if (this._extLog !== undefined) {
587
680
  if (level !== "" /* LogLevel.NONE */) {
588
- this._hbLog.log(level, message, ...parameters);
681
+ this._extLog.log(level, message, ...parameters);
589
682
  }
590
683
  }
591
684
  else {
592
685
  if (this._logWithColors) {
593
- let logNameColor = ln;
594
- if (typeof message !== 'string' || message.startsWith === undefined) {
595
- logNameColor = ln;
596
- }
597
- else if (message.startsWith('****')) {
686
+ let logNameColor = this._logNameColor;
687
+ if (message.startsWith('****')) {
598
688
  logNameColor = s4ln;
599
689
  message = message.slice(4);
600
690
  }
@@ -613,32 +703,32 @@ export class AnsiLogger {
613
703
  switch (level) {
614
704
  case "debug" /* LogLevel.DEBUG */:
615
705
  if (this.shouldLog(level, this._logLevel)) {
616
- console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${db}`, message + rs + rk, ...parameters);
706
+ console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${db}`, message + rs + rk, ...parameters);
617
707
  }
618
708
  break;
619
709
  case "info" /* LogLevel.INFO */:
620
710
  if (this.shouldLog(level, this._logLevel)) {
621
- console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${nf}`, message + rs + rk, ...parameters);
711
+ console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${nf}`, message + rs + rk, ...parameters);
622
712
  }
623
713
  break;
624
714
  case "notice" /* LogLevel.NOTICE */:
625
715
  if (this.shouldLog(level, this._logLevel)) {
626
- console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${nt}`, message + rs + rk, ...parameters);
716
+ console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${nt}`, message + rs + rk, ...parameters);
627
717
  }
628
718
  break;
629
719
  case "warn" /* LogLevel.WARN */:
630
720
  if (this.shouldLog(level, this._logLevel)) {
631
- console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${wr}`, message + rs + rk, ...parameters);
721
+ console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${wr}`, message + rs + rk, ...parameters);
632
722
  }
633
723
  break;
634
724
  case "error" /* LogLevel.ERROR */:
635
725
  if (this.shouldLog(level, this._logLevel)) {
636
- console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${er}`, message + rs + rk, ...parameters);
726
+ console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${er}`, message + rs + rk, ...parameters);
637
727
  }
638
728
  break;
639
729
  case "fatal" /* LogLevel.FATAL */:
640
730
  if (this.shouldLog(level, this._logLevel)) {
641
- console.log(`${rs}${ts}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${ft}`, message + rs + rk, ...parameters);
731
+ console.log(`${rs}${this._logTimeStampColor}[${this.getTimestamp()}] ${logNameColor}[${this._logName}]${rs}${ft}`, message + rs + rk, ...parameters);
642
732
  }
643
733
  break;
644
734
  default:
@@ -754,94 +844,102 @@ export class AnsiLogger {
754
844
  }
755
845
  }
756
846
  // Use with node dist/logger.js --testAnsiLoggerColors to test ANSI colors
847
+ /*
757
848
  if (process.argv.includes('--testAnsiLoggerColors')) {
758
- for (let i = 0; i < 256; i++) {
759
- console.log(`\x1b[38;5;${i}mForeground color ${i.toString().padStart(3, ' ')} \x1b[1mbright\x1b[0m`);
760
- }
761
- console.log(`${db}Debug message${rs}`);
762
- console.log(`${nf}Info message${rs}`);
763
- console.log(`${nt}Notice message${rs}`);
764
- console.log(`${wr}Warn message${rs}`);
765
- console.log(`${er}Error message${rs}`);
766
- console.log(`${ft}Fatal message${rs}`);
767
- console.log(`${nf}Stringify payload: ${payloadStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined })}${rs}`);
768
- console.log(`${nf}Stringify color: ${colorStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined })}${rs}`);
769
- console.log(`${nf}Stringify history: ${historyStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined })}${rs}`);
770
- console.log(`${nf}Stringify mqtt: ${mqttStringify({
771
- number: 1234,
772
- string: 'Text',
773
- boolean: true,
774
- null: null,
775
- undefined: undefined,
776
- function: () => {
777
- //
778
- },
779
- })}${rs}`);
780
- console.log(`${db}Stringify debug: ${debugStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined, object: { number: 1234, string: 'Text', boolean: true } })}${rs}`);
781
- const logger = new AnsiLogger({ logName: 'TestLogger', logLevel: "debug" /* LogLevel.DEBUG */, logWithColors: true, logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */ });
782
- AnsiLogger.setGlobalLogfile('test.log');
783
- logger.debug('Debug message');
784
- logger.info('Info message');
785
- logger.notice('Notice message');
786
- logger.warn('Warn message');
787
- logger.error('Error message');
788
- logger.fatal('Fatal message');
789
- const obj = {
790
- logName: 'TestLogger',
791
- logLevel: "debug" /* LogLevel.DEBUG */,
792
- logWithColors: true,
793
- logTimestampFormat: 4 /* TimestampFormat.TIME_MILLIS */,
794
- };
795
- logger.log("debug" /* LogLevel.DEBUG */, `Debug message with params: ${rs}\n`, obj);
796
- logger.log("debug" /* LogLevel.DEBUG */, `Debug message with params: ${rs}\n`, obj, 123, 212121111111111122121n, 'Text', true, null, undefined, () => {
849
+ for (let i = 0; i < 256; i++) {
850
+ console.log(`[38;5;${i}mForeground color ${i.toString().padStart(3, ' ')} bright`);
851
+ }
852
+ console.log(`${db}Debug message${rs}`);
853
+ console.log(`${nf}Info message${rs}`);
854
+ console.log(`${nt}Notice message${rs}`);
855
+ console.log(`${wr}Warn message${rs}`);
856
+ console.log(`${er}Error message${rs}`);
857
+ console.log(`${ft}Fatal message${rs}`);
858
+ console.log(`${nf}Stringify payload: ${payloadStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined })}${rs}`);
859
+ console.log(`${nf}Stringify color: ${colorStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined })}${rs}`);
860
+ console.log(`${nf}Stringify history: ${historyStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined })}${rs}`);
861
+ console.log(
862
+ `${nf}Stringify mqtt: ${mqttStringify({
863
+ number: 1234,
864
+ string: 'Text',
865
+ boolean: true,
866
+ null: null,
867
+ undefined: undefined,
868
+ function: () => {
797
869
  //
798
- });
799
- logger.log("debug" /* LogLevel.DEBUG */, `Debug message with array params: ${rs}\n`, obj, [123, 'abc', { a: 1, b: 2 }], 123, 212121111111111122121n, 'Text', true, null, undefined, () => {
800
- //
801
- });
802
- logger.log("debug" /* LogLevel.DEBUG */, `Debug message without params: ${debugStringify(obj)}`);
870
+ },
871
+ })}${rs}`,
872
+ );
873
+ console.log(
874
+ `${db}Stringify debug: ${debugStringify({ number: 1234, string: 'Text', boolean: true, null: null, undefined: undefined, object: { number: 1234, string: 'Text', boolean: true } })}${rs}`,
875
+ );
876
+
877
+ const logger = new AnsiLogger({ logName: 'TestLogger', logLevel: LogLevel.DEBUG, logWithColors: true, logTimestampFormat: TimestampFormat.TIME_MILLIS });
878
+ logger.logFilePath = 'test-local.log';
879
+ AnsiLogger.setGlobalLogfile('test-global.log', LogLevel.DEBUG, true);
880
+ logger.debug('Debug message');
881
+ logger.info('Info message');
882
+ logger.notice('Notice message');
883
+ logger.warn('Warn message');
884
+ logger.error('Error message');
885
+ logger.fatal('Fatal message');
886
+ const obj: object = {
887
+ logName: 'TestLogger',
888
+ logLevel: LogLevel.DEBUG,
889
+ logWithColors: true,
890
+ logTimestampFormat: TimestampFormat.TIME_MILLIS,
891
+ };
892
+ logger.log(LogLevel.DEBUG, `Debug message with params: ${rs}\n`, obj);
893
+ logger.log(LogLevel.DEBUG, `Debug message with params: ${rs}\n`, obj, 123, 212121111111111122121n, 'Text', true, null, undefined, () => {
894
+ //
895
+ });
896
+ logger.log(LogLevel.DEBUG, `Debug message with array params: ${rs}\n`, obj, [123, 'abc', { a: 1, b: 2 }], 123, 212121111111111122121n, 'Text', true, null, undefined, () => {
897
+ //
898
+ });
899
+ logger.log(LogLevel.DEBUG, `Debug message without params: ${debugStringify(obj)}`);
803
900
  }
901
+ */
804
902
  /*
805
- \x1b[0m - Reset (clear color)
806
- \x1b[1m - Bold
807
- \x1b[3m - Italic
808
- \x1b[4m - Underline
809
- \x1b[K - Erase the line from cursor
903
+  - Reset (clear color)
904
+  - Bold
905
+  - Italic
906
+  - Underline
907
+  - Erase the line from cursor
810
908
 
811
- \x1b[30m - Black
812
- \x1b[31m - Red
813
- \x1b[32m - Green
814
- \x1b[33m - Yellow
815
- \x1b[34m - Blue
816
- \x1b[35m - Magenta
817
- \x1b[36m - Cyan
818
- \x1b[37m - White
909
+  - Black
910
+  - Red
911
+  - Green
912
+  - Yellow
913
+  - Blue
914
+  - Magenta
915
+  - Cyan
916
+  - White
819
917
 
820
- \x1b[90-97m - Bright
918
+ [90-97m - Bright
821
919
 
822
- \x1b[40m - Black background
823
- \x1b[41m - Red background
824
- \x1b[42m - Green background
825
- \x1b[43m - Yellow background
826
- \x1b[44m - Blue background
827
- \x1b[45m - Magenta background
828
- \x1b[46m - Cyan background
829
- \x1b[47m - White background
920
+  - Black background
921
+  - Red background
922
+  - Green background
923
+  - Yellow background
924
+  - Blue background
925
+  - Magenta background
926
+  - Cyan background
927
+  - White background
830
928
 
831
- \x1b[100-107m - Bright background
929
+ [100-107m - Bright background
832
930
 
833
- \x1b[38;2;255;105;50m // Orange
931
+  // Orange
834
932
 
835
933
  RGB foreground
836
- \x1b[38;2;<R>;<G>;<B>m
934
+ [38;2;<R>;<G>;<B>m
837
935
 
838
936
  RGB background
839
- \x1b[48;2;<R>;<G>;<B>m
937
+ [48;2;<R>;<G>;<B>m
840
938
 
841
939
  256 colors foreground
842
- \x1b[38;5;<FG COLOR>m
940
+ [38;5;<FG COLOR>m
843
941
 
844
942
  256 colors background
845
- \x1b[48;5;<BG COLOR>m
943
+ [48;5;<BG COLOR>m
846
944
  */
847
945
  //# sourceMappingURL=logger.js.map