analogger 1.35.0 → 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -690,8 +690,9 @@ anaLogger.setDefaultContext({
690
690
 
691
691
  #### Targets
692
692
 
693
- Targets allow defining some log categories. For example, they can be developers, roles, etc.
694
- setActiveTarget() allows hiding logs from other devs or roles.
693
+ Targets allow filtering logs by targets.
694
+ For example, they can be developers, testers, roles, etc.
695
+ and setActiveTarget() will ignore logs for non-targeted users.
695
696
 
696
697
  ##### Examples
697
698
 
@@ -712,21 +713,31 @@ anaLogger.setTargets("GROUP1", "GROUP2", "TOM", "TIM"/*, "ALL", "USER"*/);
712
713
  // Assign an active target
713
714
  anaLogger.setActiveTarget("TOM"); // <- You are "TOM"
714
715
 
715
- // Seen as TOM
716
+ // Seen because you're TOM
716
717
  anaLogger.log({target: "TOM"}, `Testing log 1`); // You will see this
717
718
 
718
719
  // Not seen (only for TIM)
719
- anaLogger.log({target: "TOM"}, `Testing log 2`); // You will not see this
720
+ anaLogger.log({target: "MATT"}, `Testing log 2`); // You will not see this
720
721
 
722
+ // Here we set the allowed targets
723
+ anaLogger.setTargets({TOM: "TOM", GROUP1: "GROUP1", GROUP2: "GROUP2"});
724
+ anaLogger.setActiveTargets(["TOM", "GROUP1"]);
725
+ anaLogger.log({target: "TOM"}, `You will see this`);
726
+ anaLogger.log({target: "TIM"}, `You will not see this`);
727
+ anaLogger.log({target: "GROUP1"}, `You will see this`);
721
728
 
722
- anaLogger.setActiveTarget(["TOM", "GROUP1"]);
723
- anaLogger.log({target: "TOM"}, `Testing log 3`); // You will see this
724
- anaLogger.log({target: "TIM"}, `Testing log 4`); // You will not see this
725
- anaLogger.log({target: "GROUP1"}, `Testing log 5`); // You will see this
729
+ // To work, we should have called `anaLogger.setTargets(["NonDefinedTarget"])`
730
+ anaLogger.setActiveTarget("NonDefinedTarget");
731
+ anaLogger.log({lid: "WEB35388"}, `You will not see this`);
732
+ anaLogger.log({lid: "WEB35388", target: "NonDefinedTarget"}, `You will not see this`);
726
733
 
727
- // No target defined. Everybody sees this
734
+ // Clear the active target, so we can see everything
735
+ anaLogger.setActiveTarget(null);
736
+ anaLogger.log({lid: "WEB35388"}, `You will see this`);
737
+
738
+ // No target defined the active target will see this
728
739
  anaLogger.log({context: null}, `Testing log 6`); // You will see this
729
- anaLogger.log(`Testing log 4`); // You will see this. No context = "ALL"
740
+ anaLogger.log(`Testing log 4`); // You will see this
730
741
 
731
742
 
732
743
  ```
@@ -740,6 +751,7 @@ Examples:
740
751
 
741
752
  ```javascript
742
753
  anaLogger.setTargets({DEV1: "192.168.12.45", DEV: "192.168.12.46"});
754
+ // or anaLogger.setTargets("192.168.12.45", "192.168.12.46")
743
755
  anaLogger.setActiveTarget(require('ip').address());
744
756
  ```
745
757
  <br/>
@@ -749,6 +761,7 @@ anaLogger.setActiveTarget(require('ip').address());
749
761
  ```javascript
750
762
  // Example 2: File
751
763
  anaLogger.setTargets({DEV1: "fg890234ru20u93r2303092pkid0293"});
764
+ // or anaLogger.setTargets("fg890234ru20u93r2303092pkid0293")
752
765
  anaLogger.setActiveTarget(require('./something.json').key);
753
766
  ```
754
767
 
package/ana-logger.d.cts CHANGED
@@ -43,7 +43,7 @@ declare class ____AnaLogger {
43
43
  instanceName: string;
44
44
  logIndex: number;
45
45
  logCounter: number;
46
- activeTargets: any[];
46
+ activeTargets: string[][];
47
47
  indexColor: number;
48
48
  format: string;
49
49
  keepLog: boolean;
@@ -181,13 +181,18 @@ declare class ____AnaLogger {
181
181
  getContexts(): readonly any[];
182
182
  setTargets(targetTable?: {}): void;
183
183
  addTargets(targets: any): void;
184
- getTargets(): Readonly<{}>;
184
+ getTargets(): Readonly<{
185
+ ALL: string;
186
+ USER: string;
187
+ NONE: string;
188
+ }>;
185
189
  /**
186
190
  * Set one or more active targets
187
191
  * @param targets
188
192
  */
189
193
  setActiveTargets(targets?: any): void;
190
- getActiveTarget(): any[];
194
+ getActiveTargets(): string[][];
195
+ getActiveTarget(): string | string[];
191
196
  /**
192
197
  * Set only one active target
193
198
  * NOTE: Kept for backward compatibility.
@@ -37,7 +37,8 @@ const DEFAULT = {
37
37
 
38
38
  export const DEFAULT_LOG_TARGETS = {
39
39
  ALL : "ALL",
40
- USER: "USER"
40
+ USER: "USER",
41
+ NONE: "NONE"
41
42
  };
42
43
 
43
44
  export const DEFAULT_LOG_LEVELS = {
@@ -473,10 +474,10 @@ class ____AnaLogger
473
474
  logCounter = 0;
474
475
 
475
476
  #contexts = [];
476
- #targets = {};
477
+ #targets = {...DEFAULT_LOG_TARGETS};
477
478
  #levels = {};
478
479
 
479
- activeTargets = [];
480
+ activeTargets = [Object.values(DEFAULT_LOG_TARGETS)];
480
481
 
481
482
  indexColor = 0;
482
483
 
@@ -1301,6 +1302,16 @@ class ____AnaLogger
1301
1302
  */
1302
1303
  setActiveTargets(targets = null)
1303
1304
  {
1305
+ if (targets === undefined || targets === null) {
1306
+ this.activeTargets = [DEFAULT_LOG_TARGETS.ALL];
1307
+ return;
1308
+ }
1309
+
1310
+ // If targets is a function, call it to get the actual targets
1311
+ if (typeof targets === "function") {
1312
+ targets = targets.call(this);
1313
+ }
1314
+
1304
1315
  if (targets === null)
1305
1316
  {
1306
1317
  this.activeTargets = [DEFAULT_LOG_TARGETS.ALL];
@@ -1310,24 +1321,43 @@ class ____AnaLogger
1310
1321
  {
1311
1322
  targets = targets.split(",");
1312
1323
  }
1313
- else if (typeof targets === "object" || typeof targets === "function")
1324
+ else if (typeof targets === "function")
1314
1325
  {
1315
- return;
1326
+ // If targets is a closure, call it to get the actual targets
1327
+ targets = targets.call(this);
1328
+ }
1329
+ else if (typeof targets === "object")
1330
+ {
1331
+ targets = Object.values(targets);
1332
+ }
1333
+ else if (!Array.isArray(targets))
1334
+ {
1335
+ return ;
1316
1336
  }
1317
1337
 
1338
+ const filteredTargets = [];
1318
1339
  for (let i = 0; i < targets.length; ++i)
1319
1340
  {
1320
- targets[i] = targets[i].trim();
1341
+ const target = targets[i].trim();
1342
+ if (Object.values(this.#targets).includes(target)) {
1343
+ filteredTargets.push(target);
1344
+ }
1321
1345
  }
1322
1346
 
1323
- this.activeTargets = targets;
1347
+ this.activeTargets = filteredTargets;
1324
1348
  }
1325
1349
 
1326
- getActiveTarget()
1350
+ getActiveTargets()
1327
1351
  {
1328
1352
  return this.activeTargets;
1329
1353
  }
1330
1354
 
1355
+ getActiveTarget()
1356
+ {
1357
+ const activeTargets = this.getActiveTargets() || [];
1358
+ return activeTargets[0] || DEFAULT_LOG_TARGETS.NONE;
1359
+ }
1360
+
1331
1361
  /**
1332
1362
  * Set only one active target
1333
1363
  * NOTE: Kept for backward compatibility.
@@ -1339,7 +1369,8 @@ class ____AnaLogger
1339
1369
  this.activeTargets = [];
1340
1370
  this.setActiveTargets(target);
1341
1371
  // In case of strings
1342
- this.activeTargets = [this.activeTargets[0]];
1372
+ const activeTarget = this.activeTargets[0] || DEFAULT_LOG_TARGETS.NONE;
1373
+ this.activeTargets = [activeTarget];
1343
1374
  }
1344
1375
 
1345
1376
  setLogLevel(name, level)
@@ -1364,10 +1395,9 @@ class ____AnaLogger
1364
1395
 
1365
1396
  isTargetAllowed(target)
1366
1397
  {
1367
- // If target or activeTargets undefined, allow everything
1368
- if (!target || !this.activeTargets || !this.activeTargets.length)
1398
+ if (target === DEFAULT_LOG_TARGETS.NONE)
1369
1399
  {
1370
- return true;
1400
+ return false;
1371
1401
  }
1372
1402
 
1373
1403
  if (target === DEFAULT_LOG_TARGETS.ALL)
@@ -1375,12 +1405,12 @@ class ____AnaLogger
1375
1405
  return true;
1376
1406
  }
1377
1407
 
1378
- if (this.activeTargets.includes(DEFAULT_LOG_TARGETS.ALL))
1408
+ if (this.getActiveTarget() === DEFAULT_LOG_TARGETS.ALL)
1379
1409
  {
1380
1410
  return true;
1381
1411
  }
1382
1412
 
1383
- return this.activeTargets.includes(target);
1413
+ return this.activeTargets.includes(target);
1384
1414
  }
1385
1415
 
1386
1416
 
@@ -2196,11 +2226,16 @@ class ____AnaLogger
2196
2226
  }
2197
2227
  }
2198
2228
 
2199
- if (typeof str === 'object' && !Array.isArray(str) && str !== null) {
2200
- if (str.contextName) {
2201
- const obj = this.#contexts[str.contextName];
2202
- if (obj) {
2203
- str = Object.assign({}, obj, str);
2229
+ if (typeof str==="object" && !Array.isArray(str) && str!==null) {
2230
+ if (this.isExtendedOptionsPassed(str)) {
2231
+ if (str.contextName) {
2232
+ const obj = this.#contexts[str.contextName];
2233
+ if (obj) {
2234
+ str = Object.assign({}, obj, str);
2235
+ }
2236
+ }
2237
+ if (!str.target) {
2238
+ str.target = this.getActiveTarget();
2204
2239
  }
2205
2240
  }
2206
2241
  }
@@ -2268,7 +2303,7 @@ class ____AnaLogger
2268
2303
  {
2269
2304
  options = this.extractContextFromInput(options);
2270
2305
  // If the first parameter is not of context format,
2271
- // We use the default context and display
2306
+ // We use the default context
2272
2307
  if (!this.isExtendedOptionsPassed(options))
2273
2308
  {
2274
2309
  const defaultContext = this.generateDefaultContext();
package/demo.cjs CHANGED
@@ -16,6 +16,10 @@ anaLogger.log({lid: "WEB35382", color: "yellow"}, `Log with target`);
16
16
  anaLogger.log({contextName: "TEST"}, `Test Log example TEST`);
17
17
  anaLogger.log({contextName: "TEST"}, `Test Log example TEST 2`);
18
18
 
19
+ anaLogger.log({lid: "WEB35380", color: "yellow"}, {MY: "TEST"}, `Log with target`);
20
+ anaLogger.log({lid: "WEB35380", color: "yellow"}, {MY: "TEST"}, {MY: "TEST2"}, `Log with target`);
21
+ anaLogger.log({MY: "TEST"}, {MY: "TEST2"}, `Log with target`);
22
+
19
23
  anaLogger.setDefaultContext({
20
24
  "contextName": "DEFAULT",
21
25
  "target": "USER",
@@ -35,18 +39,45 @@ anaLogger.setDefaultContext({
35
39
  return `${logCounter}: ${formattedMessage}`;
36
40
  }
37
41
  });
38
- anaLogger.log({lid: "WEB35382", color: "yellow", target: "DEFAULT"}, `Log with target 1`);
39
- anaLogger.log({lid: "WEB35384", color: "yellow", target: "USER"}, `Log with non-existent target 2`);
40
- anaLogger.log({lid: "WEB35386", color: "yellow", target: undefined}, `Log with undefined target 3`);
41
- anaLogger.log({lid: "WEB35388", color: "yellow"}, `Log without target 3`);
42
42
 
43
+ anaLogger.setTargets({DEV1: "Me", DEV2: "You"});
44
+ anaLogger.setActiveTarget("Me");
45
+ anaLogger.log({lid: "WEB35382", color: "yellow"}, `You should see this`);
46
+
47
+ anaLogger.log({lid: "WEB35384", color: "yellow", target: "DEFAULT"}, `You should not see this`);
48
+ anaLogger.log({lid: "WEB35386", color: "yellow", target: "USER"}, `You should not see this`);
49
+ anaLogger.log({lid: "WEB35388", color: "yellow", target: undefined}, `You should see this`);
50
+ anaLogger.log({lid: "WEB35390", color: "yellow"}, `You should see this`);
51
+ anaLogger.log({lid: "WEB35392", color: "yellow", target: "You"}, `You should not see this`);
52
+ anaLogger.log({lid: "WEB35394", color: "yellow", target: "Me"}, `You should see this`);
53
+
54
+ // To work, we should have called `anaLogger.setTargets(["NonDefinedTarget"])`
55
+ anaLogger.setActiveTarget("NonDefinedTarget");
56
+ anaLogger.log({lid: "WEB35396"}, `You cannot see this`);
57
+ anaLogger.log({lid: "WEB35398", target: "NonDefinedTarget"}, `You cannot see this`);
58
+
59
+ anaLogger.setTargets({DEV1: "NonDefinedTarget"});
60
+ anaLogger.setActiveTarget("NonDefinedTarget");
61
+ anaLogger.log({lid: "WEB35400", target: "NonDefinedTarget"}, `You should see this`);
62
+
63
+ // Here we set the allowed targets
64
+ anaLogger.setTargets({TOM: "TOM", GROUP1: "GROUP1", GROUP2: "GROUP2"});
65
+ anaLogger.setActiveTargets(["TOM", "GROUP1"]);
66
+ anaLogger.log({target: "TOM"}, `TOM can see this`);
67
+ anaLogger.log({target: "TIM"}, `TIM shouldn't see this`);
68
+ anaLogger.log({target: "GROUP1"}, `GROUP1 can see this`);
69
+
70
+ // setActiveTargets here will work even if we set the targets before
43
71
  anaLogger.setActiveTargets("localhost, api");
72
+ anaLogger.log({lid: "WEB35382", color: "yellow"}, `Log without target`);
44
73
  anaLogger.log({lid: "WEB35382", target: "localhost", color: "yellow"}, `Log with target`);
45
- anaLogger.log({lid: "WEB35382", target: "localhost"}, `Log with target`);
74
+ anaLogger.log({lid: "WEB35382", target: "api"}, `Log with target`);
46
75
  anaLogger.log({lid: "WEB35382", color: "green", target: "localhost"}, `Log without target`);
47
76
 
77
+ anaLogger.setTargets(["localhost", "api"]);
78
+ anaLogger.setActiveTargets("localhost, api");
48
79
  anaLogger.log({contextName: "TEST", target: "localhost"}, `Test Log example TEST`);
49
- anaLogger.log({contextName: "TEST", target: "localhost"}, `Test Log example TEST 2`);
80
+ anaLogger.log({contextName: "TEST", target: "api"}, `Test Log example TEST 2`);
50
81
 
51
82
  // Check archive
52
83
  for (let i = 1; i < 10; ++i) {