analogger 1.1.8 → 1.3.2

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/.eslintrc.cjs ADDED
@@ -0,0 +1,25 @@
1
+ module.exports = {
2
+ env: {
3
+ browser: true,
4
+ node: true
5
+ },
6
+ parser: "@babel/eslint-parser",
7
+ parserOptions: {
8
+ "ecmaVersion": 2020
9
+ },
10
+ rules: {
11
+ "comma-dangle": 0,
12
+ "comma-style": ["error", "last"],
13
+ "no-bitwise": "off",
14
+ "no-console": "off",
15
+ "no-nested-ternary": "off",
16
+ "no-plusplus": "off",
17
+ "prefer-const": "off",
18
+ "spaced-comment": "off",
19
+ "no-debugger": "error",
20
+ "quotes": ["error", "double"],
21
+ "semi": 2,
22
+ "no-unused-vars": "error",
23
+ "max-len": ["error", {code: 200}]
24
+ }
25
+ };
package/.nycrc ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "lines": 95,
3
+ "statements": 95,
4
+ "functions": 95,
5
+ "branches": 50,
6
+ "reporter": [
7
+ "json",
8
+ "text-summary"
9
+ ],
10
+ "exclude": [
11
+ "coverage/**",
12
+ "docs/**",
13
+ "test{,s}/**",
14
+ "test{,-*}.{js,cjs,mjs,ts}",
15
+ "lib/browser/**",
16
+ "package-scripts.js",
17
+ "scripts/**"
18
+ ],
19
+ "check-coverage": true
20
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [1.3.2](https://github.com/thimpat/analogger/compare/v1.3.1...v1.3.2) (2022-02-09)
2
+
3
+ ## [1.3.1](https://github.com/thimpat/analogger/compare/v1.3.0...v1.3.1) (2022-02-09)
4
+
5
+ # [1.3.0](https://github.com/thimpat/analogger/compare/v1.2.0...v1.3.0) (2022-02-08)
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
- ![example workflow](https://github.com/thimpat/analogger/actions/workflows/test.yml/badge.svg)
3
- ![example workflow](https://github.com/thimpat/analogger/actions/workflows/versioning.yml/badge.svg)
2
+ ![Test workflow](https://github.com/thimpat/analogger/actions/workflows/test.yml/badge.svg)
3
+ ![nycrc Coverage](https://img.shields.io/nycrc/thimpat/analogger?preferredThreshold=lines)
4
+ ![Version workflow](https://github.com/thimpat/analogger/actions/workflows/versioning.yml/badge.svg)
5
+ [![npm version](https://badge.fury.io/js/analogger.svg)](https://badge.fury.io/js/analogger)
4
6
 
5
7
  Analogger is a very simple logger for both Node and the Browser.
6
8
  It is a library using both CJS and ESM.
@@ -175,10 +177,11 @@ anaLogger.assert((a, b)=> a === b, true, 2, 2)
175
177
 
176
178
  <br/><br/>
177
179
 
178
- ###
180
+ ### setErrorHandlerForUserTarget()
179
181
 
180
- When an error is detected and should be seen by your app consumers explicitly (for instance, display a dialogue box
181
- to them), you can hook your logic.
182
+ When an error is detected and should be seen by your app consumers explicitly (for instance, you want to display a
183
+ dialogue box
184
+ to them), you can set a handler here. All other console.error will be working as usual (logging messages).
182
185
 
183
186
  ```javascript
184
187
  anaLogger.setErrorHandlerForUserTarget(function (context/*, ...args*/)
@@ -189,5 +192,9 @@ to them), you can hook your logic.
189
192
  // When an error is detected in the Browser, the Browser will see this message
190
193
  anaLogger.alert(`Users explicitly see this message`)
191
194
  }
192
- })
195
+ });
196
+
197
+ anaLogger.setActiveTarget(LOG_TARGETS.USER);
198
+ anaLogger.error({target: LOG_TARGETS.USER}, "Salut user!"); // Display an alert box
199
+ anaLogger.error("Hi user!"); // Log the message to the inspector
193
200
  ```
package/ci.md CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "analogger",
3
- "version": "1.1.8",
3
+ "version": "1.3.2",
4
4
  "description": "Js Logger",
5
5
  "main": "dist/index-cjs.min.cjs",
6
6
  "module": "dist/index-esm.min.mjs",
@@ -17,7 +17,7 @@
17
17
  "bundle:prod:cjs": "rollup --config rollup-cjs.config.js",
18
18
  "bundle:prod:esm": "rollup --config rollup-esm.config.js",
19
19
  "bundle:prod": "npm run convert-cjs:esm && npm run convert-cjs:browser && npm run bundle:prod:cjs && npm run bundle:prod:esm",
20
- "test": "mocha",
20
+ "test": "nyc mocha",
21
21
  "demo": "npm run bundle:prod && node example/cjs/demo.cjs"
22
22
  },
23
23
  "author": "Patrice Thimothee",
@@ -41,14 +41,17 @@
41
41
  "babel-eslint": "^10.1.0",
42
42
  "capture-console": "^1.0.1",
43
43
  "chai": "^4.3.6",
44
+ "chai-arrays": "^2.2.0",
44
45
  "chai-spies": "^1.0.0",
45
46
  "eslint": "^8.8.0",
46
47
  "mocha": "^9.2.0",
48
+ "nyc": "^15.1.0",
47
49
  "rollup": "^2.67.0",
48
50
  "rollup-plugin-delete": "^2.0.0",
49
51
  "rollup-plugin-uglify": "^6.0.4",
50
52
  "semantic-release": "^19.0.2",
51
- "to-esm": "^1.6.2"
53
+ "sinon": "^13.0.1",
54
+ "to-esm": "^1.6.4"
52
55
  },
53
56
  "dependencies": {
54
57
  "chalk": "^5.0.0",
@@ -1,19 +1,14 @@
1
1
  const chai = require("chai");
2
2
  var capcon = require('capture-console');
3
3
  const {anaLogger} = require("../src/cjs/ana-logger.cjs");
4
- const {LOG_CONTEXT, LOG_TARGETS} = require("../example/cjs/contexts-def.cjs");
4
+ const {LOG_CONTEXTS, LOG_TARGETS} = require("../example/cjs/contexts-def.cjs");
5
5
  const expect = chai.expect;
6
6
 
7
7
  describe('In the Terminal', function ()
8
8
  {
9
- const {anaLogger} = require("../src/cjs/ana-logger.cjs");
10
-
11
- const LOG_CONTEXT = {STANDARD: {}, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
12
- const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", DEV3: "ME", USER: "USER"}
13
-
14
9
  before(()=>
15
10
  {
16
- anaLogger.setContexts(LOG_CONTEXT);
11
+ anaLogger.setContexts(LOG_CONTEXTS);
17
12
  anaLogger.setTargets(LOG_TARGETS);
18
13
  })
19
14
 
@@ -38,7 +33,7 @@ describe('In the Terminal', function ()
38
33
  expect(captured.stdout).to.contain(`Test Log example C1`)
39
34
  });
40
35
 
41
- it('should hide console output when the console behaviour is overridden', function ()
36
+ it('should hide console output when the console behaviour is overridden', function (done)
42
37
  {
43
38
  const captured = capcon.captureStdio(function ()
44
39
  {
@@ -46,6 +41,7 @@ describe('In the Terminal', function ()
46
41
  anaLogger.setOptions({silent: true})
47
42
  anaLogger.overrideConsole()
48
43
  console.log(`Log After override`);
44
+ done()
49
45
  })
50
46
 
51
47
  expect(captured.stdout).to.contain(`Log Before override`)
@@ -58,7 +54,7 @@ describe('In the Terminal', function ()
58
54
  {
59
55
  anaLogger.keepLogHistory();
60
56
  anaLogger.setOptions({silent: true, hideError: false})
61
- anaLogger.log(LOG_CONTEXT.C1, `Test Log example something again`);
57
+ anaLogger.log(LOG_CONTEXTS.C1, `Test Log example something again`);
62
58
  })
63
59
 
64
60
  const captured2 = capcon.captureStdio(function ()
@@ -94,8 +90,8 @@ describe('In the Terminal', function ()
94
90
  const captured = capcon.captureStdio(function ()
95
91
  {
96
92
  anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
97
- anaLogger.log({context: LOG_CONTEXT.TEST, target: LOG_TARGETS.DEV3, lid: 100001}, `Test Log example with active target`);
98
- anaLogger.log({context: LOG_CONTEXT.TEST, target: LOG_TARGETS.DEV1, lid: 100002}, `Test Log example with DEV1 target`);
93
+ anaLogger.log({context: LOG_CONTEXTS.TEST, target: LOG_TARGETS.DEV3, lid: 100001}, `Test Log example with active target`);
94
+ anaLogger.log({context: LOG_CONTEXTS.TEST, target: LOG_TARGETS.DEV1, lid: 100002}, `Test Log example with DEV1 target`);
99
95
  anaLogger.log(`Test Log example with DEFAULT target`);
100
96
  })
101
97
 
@@ -104,6 +100,17 @@ describe('In the Terminal', function ()
104
100
  expect(captured.stdout).to.not.contain(`Test Log example with DEV1 target`)
105
101
  });
106
102
 
103
+ it('should not anything when silent mode is enabled', function ()
104
+ {
105
+ const captured = capcon.captureStdio(function ()
106
+ {
107
+ anaLogger.setOptions({silent: true})
108
+ anaLogger.log(`Test Log example with DEFAULT target`);
109
+ })
110
+
111
+ expect(captured.stdout).to.not.contain(`Test Log example with DEFAULT target`)
112
+ });
113
+
107
114
 
108
115
  });
109
116
  });
package/test/unit.cjs CHANGED
@@ -1,30 +1,91 @@
1
1
  const chai = require("chai");
2
+ const assertArrays = require("chai-arrays")
2
3
  const expect = chai.expect;
4
+ const sinon = require("sinon");
5
+
6
+ let alert, sandbox;
7
+
8
+ // Arrange
9
+ const myStub = {
10
+ myMethod: () => { }
11
+ }
12
+
13
+
3
14
  const spies = require('chai-spies');
4
15
 
5
16
  chai.use(spies);
17
+ chai.use(assertArrays)
18
+
6
19
  // sut
7
20
  const {anaLogger} = require("../src/cjs/ana-logger.cjs");
8
- const {LOG_CONTEXT} = require("../example/cjs/contexts-def.cjs");
21
+ const {LOG_CONTEXTS, LOG_TARGETS} = require("../example/cjs/contexts-def.cjs");
9
22
 
10
23
  describe('AnaLogger', function ()
11
24
  {
12
- const LOG_CONTEXT = {STANDARD: {}, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
13
- const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", DEV3: "ME", USER: "USER"}
14
-
15
25
  before(() =>
16
26
  {
17
- anaLogger.setContexts(LOG_CONTEXT);
27
+ anaLogger.setContexts(LOG_CONTEXTS);
18
28
  anaLogger.setTargets(LOG_TARGETS);
19
29
  anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
30
+ anaLogger.removeOverride({error: true})
20
31
  })
21
32
 
22
33
  beforeEach(() =>
23
34
  {
35
+ alert = sinon.spy()
36
+ chai.spy.on(myStub, 'myMethod');
37
+
24
38
  anaLogger.resetLogHistory()
25
39
  anaLogger.keepLogHistory()
40
+ anaLogger.resetLogFormatter();
26
41
  })
27
42
 
43
+ afterEach(()=>
44
+ {
45
+ chai.spy.restore(myStub.myMethod)
46
+ })
47
+
48
+ describe('#isContextValid()', function ()
49
+ {
50
+ it('should be true when a valid context object is passed', function ()
51
+ {
52
+ // Arrange
53
+ const context = LOG_CONTEXTS.TEST
54
+ // Act
55
+ const result = anaLogger.isContextValid(context)
56
+ // Assert
57
+ expect(result).to.be.true
58
+ });
59
+
60
+ it('should be false when an invalid context object is passed', function ()
61
+ {
62
+ // Arrange
63
+ const context = {}
64
+ // Act
65
+ const result = anaLogger.isContextValid(context)
66
+ // Assert
67
+ expect(result).to.be.false
68
+ });
69
+
70
+ it('should be false when a null context is passed', function ()
71
+ {
72
+ // Act
73
+ const result = anaLogger.isContextValid(null)
74
+ // Assert
75
+ expect(result).to.be.false
76
+ });
77
+ });
78
+
79
+ describe('#setOptions()', function ()
80
+ {
81
+ it('should have an option to silent the log', function ()
82
+ {
83
+ anaLogger.setOptions({silent: true})
84
+ const options = anaLogger.getOptions()
85
+ expect(options.silent).to.be.true
86
+ });
87
+ });
88
+
28
89
  describe('#log()', function ()
29
90
  {
30
91
  it('should emulate console.log', function ()
@@ -39,6 +100,174 @@ describe('AnaLogger', function ()
39
100
  // Assert
40
101
  expect(output).to.contain(`Test Log example C1`)
41
102
  });
103
+
104
+ it('should understand values passed with context', function ()
105
+ {
106
+ // Act
107
+ anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C1`);
108
+
109
+ // Assert
110
+ expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
111
+ });
112
+
113
+ it('should understand values passed with context as value of object', function ()
114
+ {
115
+ // Act
116
+ anaLogger.log({context: LOG_CONTEXTS.C1}, `Test Log example C1`);
117
+
118
+ // Assert
119
+ expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
120
+ });
121
+
122
+ it('should understand values passed with context defined as null', function ()
123
+ {
124
+ // Act
125
+ anaLogger.log({context: null}, `Test Log example C1`);
126
+
127
+ // Assert
128
+ expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
129
+ });
130
+
131
+ it('should populate history even though the hidelog option is on', function ()
132
+ {
133
+ anaLogger.setOptions({hideLog: true})
134
+
135
+ // Act
136
+ anaLogger.log({context: LOG_CONTEXTS.C1, lid: 123456789233}, `The hidden log`);
137
+
138
+ // Assert
139
+ expect(anaLogger.getLogHistory()).to.contain(`The hidden log`)
140
+ });
141
+
142
+ it('should not capture or display log from another defined target', function ()
143
+ {
144
+ anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
145
+ anaLogger.log({target: LOG_TARGETS.DEV1}, `I am for DEV1`);
146
+
147
+ // Assert
148
+ expect(anaLogger.getLogHistory()).to.not.contain(`I am for DEV1`)
149
+ });
150
+
151
+ it('should capture logs when no active target is set', function ()
152
+ {
153
+ anaLogger.setActiveTarget(null)
154
+ anaLogger.log({target: LOG_TARGETS.DEV3}, `I am for DEV3`);
155
+
156
+ // Assert
157
+ expect(anaLogger.getLogHistory()).to.contain(`I am for DEV3`)
158
+ });
159
+
160
+ it('should capture logs from the same target', function ()
161
+ {
162
+ anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
163
+ anaLogger.log({target: LOG_TARGETS.DEV3}, `I am for DEV3`);
164
+
165
+ // Assert
166
+ expect(anaLogger.getLogHistory()).to.contain(`I am for DEV3`)
167
+ });
168
+
169
+ it('should capture logs from the same target', function ()
170
+ {
171
+ anaLogger.log(LOG_CONTEXTS.TEST2, `I am for DEV3`);
172
+
173
+ // Assert
174
+ expect(anaLogger.getLogHistory()).to.contain(`I am for DEV3`)
175
+ });
176
+
177
+ it('should truncate some text when too long', function ()
178
+ {
179
+ // Act
180
+ anaLogger.log({
181
+ context: LOG_CONTEXTS.C1,
182
+ lid : 123456789233
183
+ }, `The super long Log ID (lid) will be truncated`);
184
+
185
+ // Assert
186
+ expect(anaLogger.getLogHistory()).to.contain(`C1: (12... )`)
187
+ });
188
+
189
+
190
+ });
191
+
192
+ describe('#error()', function ()
193
+ {
194
+ it('should not show up when hideError mode is on', function ()
195
+ {
196
+ // Arrange
197
+ anaLogger.setOptions({hideError: true})
198
+
199
+ // Act
200
+ anaLogger.error(`Test Log example C1`);
201
+
202
+ // Assert
203
+ expect(anaLogger.getLogHistory()).to.not.contain(`Test Log example C1`)
204
+ });
205
+
206
+ it('should not show up when hideError mode is off', function ()
207
+ {
208
+ // Arrange
209
+ anaLogger.setOptions({hideError: false})
210
+
211
+ // Act
212
+ anaLogger.error(`Test Log example C1`);
213
+
214
+ // Assert
215
+ expect(anaLogger.getLogHistory()).to.contain(`Test Log example C1`)
216
+ });
217
+ });
218
+
219
+ describe('#info()', function ()
220
+ {
221
+ it('should display some log', function ()
222
+ {
223
+ anaLogger.info(`Hello from info`)
224
+ expect(anaLogger.getLogHistory()).to.contain(`Hello from info`)
225
+ });
226
+ });
227
+
228
+ describe('#warn()', function ()
229
+ {
230
+ it('should display some warn', function ()
231
+ {
232
+ anaLogger.warn(`Hello from warn`)
233
+ expect(anaLogger.getLogHistory()).to.contain(`Hello from warn`)
234
+ });
235
+ });
236
+
237
+ describe('#alert()', function ()
238
+ {
239
+ it('should not fail on alert', function ()
240
+ {
241
+ anaLogger.alert(`Hello from alert`, {aaa: 1012})
242
+ expect(anaLogger.getLogHistory()).to.contain(`Hello from alert`)
243
+ });
244
+
245
+ describe("in a non-Node environment", function ()
246
+ {
247
+ beforeEach(function ()
248
+ {
249
+ sandbox = sinon.createSandbox();
250
+ });
251
+
252
+
253
+ it('should be called', function ()
254
+ {
255
+ sandbox
256
+ .stub(anaLogger, "isNode")
257
+ .withArgs("Hello from alert")
258
+ .returns(
259
+ true
260
+ );
261
+
262
+ chai.expect(() => anaLogger.alert(`Hello from alert`)).to.throw(`alert is not defined`);
263
+ });
264
+
265
+ afterEach(function ()
266
+ {
267
+ sandbox.restore();
268
+ });
269
+ })
270
+
42
271
  });
43
272
 
44
273
  describe('#assert()', function ()
@@ -61,19 +290,32 @@ describe('AnaLogger', function ()
61
290
  expect(result).to.be.true
62
291
  });
63
292
 
293
+ it('should fail when function expressions fail', function ()
294
+ {
295
+ const result = anaLogger.assert(() => false, true)
296
+ expect(result).to.be.false
297
+ });
298
+
64
299
  it('should evaluate more complex function expressions', function ()
65
300
  {
66
301
  const result = anaLogger.assert((a, b) => a === b, true, 2, 2)
67
302
  expect(result).to.be.true
68
303
  });
304
+
305
+ it('should not break the code when an invalid function is passed', function ()
306
+ {
307
+ expect(anaLogger.assert(() => nonExistentFunctionThatIsCalledAnyway(), true, 2, 2)
308
+ ).to.be.false
309
+ });
69
310
  });
70
311
 
71
- describe('#alert()', function ()
312
+ describe('#overrideConsole()', function ()
72
313
  {
73
- it('should not fail on alert', function ()
314
+ it('should override the console behaviour', function ()
74
315
  {
75
- anaLogger.alert(`Hello from alert`, {aaa: 1012})
76
- expect(anaLogger.getLogHistory()).to.contain(`Hello from alert`)
316
+ anaLogger.overrideConsole({error: true})
317
+ console.log(`Test 1`)
318
+ expect(anaLogger.getLogHistory()).to.contain(`Test 1`)
77
319
  });
78
320
  });
79
321
 
@@ -84,21 +326,129 @@ describe('AnaLogger', function ()
84
326
  */
85
327
  it('should replace the default formatter function with the given callback when invoking console.log', function ()
86
328
  {
87
- // Arrange
88
- const methodToCall = {
89
- myMethod: () => { }
90
- }
329
+ beforeEach(function ()
330
+ {
331
+ })
332
+
333
+ afterEach(function ()
334
+ {
335
+ chai.spy.restore(myStub.myMethod)
336
+ })
337
+
338
+ anaLogger.setLogFormat(
339
+ myStub.myMethod
340
+ );
91
341
 
92
- // Spy on methodToCall.myMethod to check it has been called
93
- chai.spy.on(methodToCall, 'myMethod');
342
+ console.log(LOG_CONTEXTS.C1, `Test Log example C4 with new format`);
343
+ expect(myStub.myMethod).to.have.been.called;
344
+ });
94
345
 
346
+ it("should reset the formatter to its first value", () =>
347
+ {
95
348
  anaLogger.setLogFormat(
96
- methodToCall.myMethod
349
+ () => "If you see this the test has failed"
97
350
  );
351
+ anaLogger.resetLogFormatter();
352
+ anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C4 with new format`);
353
+ expect(anaLogger.getLogHistory()).to.contain(`C1: ( )`)
354
+ })
355
+
356
+ it("should reject invalid formatters", () =>
357
+ {
358
+ const res = anaLogger.setLogFormat(null);
359
+ expect(res).to.be.false
360
+ })
361
+ });
98
362
 
99
- console.log(LOG_CONTEXT.C1, `Test Log example C4 with new format`);
100
- expect(methodToCall.myMethod).to.have.been.called;
363
+ describe('#releaseLogHistory()', function ()
364
+ {
365
+ it('should not keep log history', function ()
366
+ {
367
+ anaLogger.releaseLogHistory()
368
+ anaLogger.log(`Hello you`)
369
+ expect(anaLogger.getLogHistory().length).to.equal(0)
370
+ });
371
+ });
372
+
373
+ describe('#getLogHistory()', function ()
374
+ {
375
+ it('should return history as a string', function ()
376
+ {
377
+ anaLogger.log(`Hello you`)
378
+ const arr = anaLogger.getLogHistory()
379
+ expect(arr).to.be.string
380
+ });
381
+
382
+ it('should return history as an array', function ()
383
+ {
384
+ anaLogger.log(`Hello you`)
385
+ const arr = anaLogger.getLogHistory(false)
386
+ expect(arr).to.be.array()
387
+ });
388
+
389
+ it('should return history as an array', function ()
390
+ {
391
+ anaLogger.log(`Hello you`)
392
+ const arr = anaLogger.getLogHistory(false)
393
+ expect(arr).to.be.array()
394
+ });
395
+ });
396
+
397
+ describe('#setErrorHandler()', function ()
398
+ {
399
+ it('should replace the error manager', function ()
400
+ {
401
+ anaLogger.setActiveTarget(LOG_TARGETS.USER)
402
+ anaLogger.error({target: LOG_TARGETS.USER}, `Test Error Log`);
101
403
  });
404
+
405
+ it('should replace the error manager', function ()
406
+ {
407
+ anaLogger.setErrorHandler(
408
+ myStub.myMethod
409
+ );
410
+
411
+ console.error(`Test Error Log`);
412
+ expect(myStub.myMethod).to.have.been.called;
413
+ });
414
+
415
+ it('should replace the error manager targeting the user', function ()
416
+ {
417
+ anaLogger.setActiveTarget(LOG_TARGETS.USER)
418
+ anaLogger.setErrorHandler(
419
+ myStub.myMethod
420
+ );
421
+
422
+ anaLogger.error(`Test Error Log`);
423
+ expect(myStub.myMethod).to.have.been.called;
424
+ });
425
+
426
+ });
427
+
428
+ describe('#setErrorHandlerForUserTarget()', function ()
429
+ {
430
+ it('should replace the error manager targetting the user', function ()
431
+ {
432
+ anaLogger.setActiveTarget(LOG_TARGETS.USER)
433
+ anaLogger.setErrorHandlerForUserTarget(
434
+ myStub.myMethod
435
+ );
436
+
437
+ anaLogger.error(`Test Error Log`);
438
+ expect(myStub.myMethod).to.have.been.called;
439
+ });
440
+
441
+ it('should replace the error manager targeting the user', function ()
442
+ {
443
+ anaLogger.setActiveTarget(LOG_TARGETS.USER)
444
+ anaLogger.setErrorHandlerForUserTarget(
445
+ myStub.myMethod
446
+ );
447
+
448
+ anaLogger.error(`Test Error Log`);
449
+ expect(myStub.myMethod).to.have.been.called;
450
+ });
451
+
102
452
  });
103
453
 
104
454
 
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var require$$0=require("chalk-cjs"),require$$1=require("color-convert-cjs"),require$$2=require("rgb-hex-cjs"),require$$3=require("os");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var require$$0__default=_interopDefaultLegacy(require$$0),require$$1__default=_interopDefaultLegacy(require$$1),require$$2__default=_interopDefaultLegacy(require$$2),require$$3__default=_interopDefaultLegacy(require$$3),anaLogger={},constants$1={};const constants={COLOR_TABLE:["#d2466e","#FFA07A","#FF7F50","#FF6347","#FFE4B5","#ADFF2F","#808000","#40E0D0","#1E90FF","#EE82EE","#708090","#DEB887","#FE642E","#210B61","#088A4B","#5E610B","#FA8258","#088A68","#B40431"],SYSTEM:{BROWSER:"BROWSER",NODE:"NODE"}},chalk=(constants$1.COLOR_TABLE=constants.COLOR_TABLE,constants$1.SYSTEM=constants.SYSTEM,require$$0__default.default),colorConvert=require$$1__default.default,rgbHex=require$$2__default.default,os=require$$3__default.default,{COLOR_TABLE,SYSTEM}=constants$1;class AnaLogger{system="";logIndex=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";keepLog=!1;logHistory=[];options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};originalFormatFunction;constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),this.originalFormatFunction=this.format,this.errorTargetHandler=this.onError.bind(this),this.errorUserTargetHandler=this.onErrorForUserTarget.bind(this),this.setOptions(this.options),this.realConsoleLog=console.log,this.realConsoleInfo=console.info,this.realConsoleWarn=console.warn,this.realConsoleError=console.error,this.ALIGN=AnaLogger.ALIGN,this.ENVIRONMENT_TYPE=AnaLogger.ENVIRONMENT_TYPE}keepLogHistory(){this.keepLog=!0}releaseLogHistory(){this.keepLog=!1}resetLogHistory(){this.logHistory=[]}getLogHistory(e=!0,t=os.EOL){const o=JSON.parse(JSON.stringify(this.logHistory.slice(0)));return e?o.join(t):o}isNode(){return this.system===SYSTEM.NODE}isBrowser(){return!this.isNode()}setOptions({contextLenMax:e=10,idLenMax:t=5,lidLenMax:o=5,symbolLenMax:r=2,messageLenMax:s=60,hideLog:n=!1,hideError:i=!1,hideHookMessage:a=!1,showPassingTests:g=!0,silent:l=!1}={}){this.options.contextLenMax=e,this.options.idLenMax=t,this.options.lidLenMax=o,this.options.messageLenMax=s,this.options.symbolLenMax=r,this.options.hideLog=!!n,this.options.hideError=!!i,this.options.hideHookMessage=!!a,this.options.showPassingTests=!!g,l&&(this.options.hideLog=!0,this.options.hideHookMessage=!0)}truncateMessage(e="",{fit:t=0,align:o=AnaLogger.ALIGN.LEFT}){try{return e=""+e,t&&e.length>=t+2&&(e=e.substring(0,t-3)+"..."),e=o===AnaLogger.ALIGN.LEFT?e.padEnd(t+1," "):e.padStart(t+1," ")}catch(e){console.error("AnaLogger:",e)}}onBuildLog({contextName:e,message:t="",lid:o="",symbol:r=""}={}){const s=new Date;var n=("0"+s.getHours()).slice(-2)+":"+("0"+s.getMinutes()).slice(-2)+":"+("0"+s.getSeconds()).slice(-2),n=this.truncateMessage(n,{fit:7});return e=this.truncateMessage(e,{fit:this.options.contextLenMax,align:AnaLogger.ALIGN.RIGHT}),o=this.truncateMessage(o,{fit:this.options.lidLenMax}),t=this.truncateMessage(t,{fit:this.options.messageLenMax}),`[${n}] ${e}: (${o}) ${r=this.truncateMessage(r,{fit:this.options.symbolLenMax})} `+t}onErrorForUserTarget(e,...t){this.errorUserTargetHandler(e,...t)}onError(e,...t){e.target===this.targets.USER&&this.onErrorForUserTarget(e,...t)}onDisplayLog(...e){this.log(...e)}onDisplayError(...e){this.error(...e)}setLogFormat(e){if("function"!=typeof e)return console.error("Invalid parameter for setFormat. It is expecting a function or method."),!1;this.format=e.bind(this)}resetLogFormatter(){this.format=this.originalFormatFunction}setErrorHandler(e){this.errorTargetHandler=e.bind(this)}setErrorHandlerForUserTarget(e){this.errorUserTargetHandler=e.bind(this)}isContext(e){return"object"==typeof e&&!Array.isArray(e)&&null!==e&&(e.hasOwnProperty("contextName")&&e.hasOwnProperty("target"))}generateDefaultContext(){const e={name:"DEFAULT",contextName:"DEFAULT",target:"ALL",symbol:"⚡"};return e.id=this.logIndex++,e.color=COLOR_TABLE[1],e}generateNewContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[this.indexColor++%(COLOR_TABLE.length-3)+2],e.symbol="",e}generateErrorContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[0],e.symbol="v",e.error=!0,e}allegeProperties(e){let t=e;e=this.generateNewContext();if(t=t||{contextName:"DEFAULT"},Array.isArray(t))throw new Error(`AnaLogger: Cannot convert Array [${JSON.stringify(t)}] to context`);if(("string"==typeof t||t instanceof String)&&(t={contextName:t}),"object"!=typeof t)throw new Error(`AnaLogger: Cannot convert Unknown [${JSON.stringify(t)}] to context`);return t=Object.assign({},e,t),-1<t.color.toLowerCase().indexOf("rgb")?t.color="#"+rgbHex(t.color):-1===t.color.indexOf("#")&&colorConvert&&(t.color="#"+colorConvert.keyword.hex(t.color)),t}setContexts(o){const e=Object.keys(o);o.DEFAULT=this.contexts.DEFAULT=this.generateDefaultContext(),o.ERROR=this.contexts.ERROR=this.generateErrorContext(),e.forEach(e=>{const t=o[e]||{};t.contextName=e,t.name=e,this.contexts[e]=this.allegeProperties(t),o[e]=this.contexts[e]})}setTargets(e={}){this.targets=Object.assign({},e,{ALL:"ALL",USER:"USER"})}setActiveTarget(e){this.activeTarget=e}enableContexts(e){this.contexts.forEach(e=>{})}getActiveLogContexts(){}isTargetAllowed(e){return!e||!this.activeTarget||(e===this.targets.ALL||this.activeTarget===e)}processOutput(o={}){try{if(!this.isTargetAllowed(o.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var r=e.join(" | ");let t="";var s=this.format({...o,message:r});if(t=this.isBrowser()?(o.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,"%c"+s):(o.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,chalk.hex(o.color)(s)),this.keepLog&&this.logHistory.push(t),this.options.hideLog)return;this.isBrowser()?this.realConsoleLog(t,"color: "+o.color):this.realConsoleLog(t),this.errorTargetHandler(o,e)}catch(e){console.error("AnaLogger:",e.message)}}isExtendedOptionsPassed(e){return"object"==typeof e&&(e.hasOwnProperty("context")||e.hasOwnProperty("target"))}log(e,...t){var o;if(!this.isExtendedOptionsPassed(e))return o=this.generateDefaultContext(),void this.processOutput.apply(this,[o,e,...t]);let r=e;if("object"==typeof e.context){const s=Object.assign({},e);delete s.context,r=Object.assign({},e.context,s)}r.hasOwnProperty("context")&&(r=Object.assign({},this.generateDefaultContext(),r),delete r.context),this.processOutput.apply(this,[r,...t])}error(e,...t){if(!this.options.hideError){var o=this.generateErrorContext();if(this.isExtendedOptionsPassed(e))return e=Object.assign({},o,e),this.log(e,...t);e=Array.prototype.slice.call(arguments);this.log(o,...e)}}overrideError(){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.error"),console.error=this.onDisplayError.bind(this)}overrideConsole({log:e=!0,info:t=!0,warn:o=!0,error:r=!1}={}){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.log"),e&&(console.log=this.onDisplayLog.bind(this)),t&&(console.info=this.onDisplayLog.bind(this)),o&&(console.warn=this.onDisplayLog.bind(this)),r&&this.overrideError()}removeOverrideError(){console.warn=this.realConsoleError}removeOverride({log:e=!0,info:t=!0,warn:o=!0,error:r=!1}={}){e&&(console.log=this.realConsoleLog),t&&(console.info=this.realConsoleInfo),o&&(console.warn=this.realConsoleWarn),r&&this.removeOverrideError()}info(...e){return this.log(...e)}warn(...e){return this.log(...e)}alert(...e){if(this.isNode())return this.log(...e);e=e.join(" | ");alert(e)}assert(e,t=!0,...o){try{return"function"==typeof e?e(...o)!==t?(this.error("Asset failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0):e!==t?(this.error("Assert failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0)}catch(e){this.error("Unexpected error in assert")}return!1}}var anaLogger_1=anaLogger.anaLogger=new AnaLogger;exports.anaLogger=anaLogger_1,exports.default=anaLogger;
@@ -1 +0,0 @@
1
- import os from"os";function rgbHex(e,t,o,r){var s=(e+(r||"")).toString().includes("%");if("string"==typeof e?[e,t,o,r]=e.match(/(0?\.?\d{1,3})%?\b/g).map(e=>Number(e)):void 0!==r&&(r=Number.parseFloat(r)),"number"!=typeof e||"number"!=typeof t||"number"!=typeof o||255<e||255<t||255<o)throw new TypeError("Expected three numbers below 256");if("number"==typeof r){if(!s&&0<=r&&r<=1)r=Math.round(255*r);else{if(!(s&&0<=r&&r<=100))throw new TypeError(`Expected alpha value (${r}) as a fraction or percentage`);r=Math.round(255*r/100)}r=(256|r).toString(16).slice(1)}else r="";return(o|t<<8|e<<16|1<<24).toString(16).slice(1)+r}const constants={COLOR_TABLE:["#d2466e","#FFA07A","#FF7F50","#FF6347","#FFE4B5","#ADFF2F","#808000","#40E0D0","#1E90FF","#EE82EE","#708090","#DEB887","#FE642E","#210B61","#088A4B","#5E610B","#FA8258","#088A68","#B40431"],SYSTEM:{BROWSER:"BROWSER",NODE:"NODE"}},COLOR_TABLE=constants.COLOR_TABLE,SYSTEM=constants.SYSTEM,chalk=null;class AnaLogger{system="";logIndex=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";keepLog=!1;logHistory=[];options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};originalFormatFunction;constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),this.originalFormatFunction=this.format,this.errorTargetHandler=this.onError.bind(this),this.errorUserTargetHandler=this.onErrorForUserTarget.bind(this),this.setOptions(this.options),this.realConsoleLog=console.log,this.realConsoleInfo=console.info,this.realConsoleWarn=console.warn,this.realConsoleError=console.error,this.ALIGN=AnaLogger.ALIGN,this.ENVIRONMENT_TYPE=AnaLogger.ENVIRONMENT_TYPE}keepLogHistory(){this.keepLog=!0}releaseLogHistory(){this.keepLog=!1}resetLogHistory(){this.logHistory=[]}getLogHistory(e=!0,t=os.EOL){const o=JSON.parse(JSON.stringify(this.logHistory.slice(0)));return e?o.join(t):o}isNode(){return this.system===SYSTEM.NODE}isBrowser(){return!this.isNode()}setOptions({contextLenMax:e=10,idLenMax:t=5,lidLenMax:o=5,symbolLenMax:r=2,messageLenMax:s=60,hideLog:n=!1,hideError:i=!1,hideHookMessage:a=!1,showPassingTests:g=!0,silent:h=!1}={}){this.options.contextLenMax=e,this.options.idLenMax=t,this.options.lidLenMax=o,this.options.messageLenMax=s,this.options.symbolLenMax=r,this.options.hideLog=!!n,this.options.hideError=!!i,this.options.hideHookMessage=!!a,this.options.showPassingTests=!!g,h&&(this.options.hideLog=!0,this.options.hideHookMessage=!0)}truncateMessage(e="",{fit:t=0,align:o=AnaLogger.ALIGN.LEFT}){try{return e=""+e,t&&e.length>=t+2&&(e=e.substring(0,t-3)+"..."),e=o===AnaLogger.ALIGN.LEFT?e.padEnd(t+1," "):e.padStart(t+1," ")}catch(e){console.error("AnaLogger:",e)}}onBuildLog({contextName:e,message:t="",lid:o="",symbol:r=""}={}){const s=new Date;var n=("0"+s.getHours()).slice(-2)+":"+("0"+s.getMinutes()).slice(-2)+":"+("0"+s.getSeconds()).slice(-2),n=this.truncateMessage(n,{fit:7});return e=this.truncateMessage(e,{fit:this.options.contextLenMax,align:AnaLogger.ALIGN.RIGHT}),o=this.truncateMessage(o,{fit:this.options.lidLenMax}),t=this.truncateMessage(t,{fit:this.options.messageLenMax}),`[${n}] ${e}: (${o}) ${r=this.truncateMessage(r,{fit:this.options.symbolLenMax})} `+t}onErrorForUserTarget(e,...t){this.errorUserTargetHandler(e,...t)}onError(e,...t){e.target===this.targets.USER&&this.onErrorForUserTarget(e,...t)}onDisplayLog(...e){this.log(...e)}onDisplayError(...e){this.error(...e)}setLogFormat(e){if("function"!=typeof e)return console.error("Invalid parameter for setFormat. It is expecting a function or method."),!1;this.format=e.bind(this)}resetLogFormatter(){this.format=this.originalFormatFunction}setErrorHandler(e){this.errorTargetHandler=e.bind(this)}setErrorHandlerForUserTarget(e){this.errorUserTargetHandler=e.bind(this)}isContext(e){return"object"==typeof e&&!Array.isArray(e)&&null!==e&&(e.hasOwnProperty("contextName")&&e.hasOwnProperty("target"))}generateDefaultContext(){const e={name:"DEFAULT",contextName:"DEFAULT",target:"ALL",symbol:"⚡"};return e.id=this.logIndex++,e.color=COLOR_TABLE[1],e}generateNewContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[this.indexColor++%(COLOR_TABLE.length-3)+2],e.symbol="",e}generateErrorContext(){const e=this.generateDefaultContext();return e.color=COLOR_TABLE[0],e.symbol="v",e.error=!0,e}allegeProperties(e){let t=e;e=this.generateNewContext();if(t=t||{contextName:"DEFAULT"},Array.isArray(t))throw new Error(`AnaLogger: Cannot convert Array [${JSON.stringify(t)}] to context`);if(("string"==typeof t||t instanceof String)&&(t={contextName:t}),"object"!=typeof t)throw new Error(`AnaLogger: Cannot convert Unknown [${JSON.stringify(t)}] to context`);return t=Object.assign({},e,t),-1<t.color.toLowerCase().indexOf("rgb")?t.color="#"+rgbHex(t.color):t.color.indexOf("#"),t}setContexts(o){const e=Object.keys(o);o.DEFAULT=this.contexts.DEFAULT=this.generateDefaultContext(),o.ERROR=this.contexts.ERROR=this.generateErrorContext(),e.forEach(e=>{const t=o[e]||{};t.contextName=e,t.name=e,this.contexts[e]=this.allegeProperties(t),o[e]=this.contexts[e]})}setTargets(e={}){this.targets=Object.assign({},e,{ALL:"ALL",USER:"USER"})}setActiveTarget(e){this.activeTarget=e}enableContexts(e){this.contexts.forEach(e=>{})}getActiveLogContexts(){}isTargetAllowed(e){return!e||!this.activeTarget||(e===this.targets.ALL||this.activeTarget===e)}processOutput(o={}){try{if(!this.isTargetAllowed(o.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var r=e.join(" | ");let t="";var s=this.format({...o,message:r});if(t=this.isBrowser()?(o.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,"%c"+s):(o.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,chalk.hex(o.color)(s)),this.keepLog&&this.logHistory.push(t),this.options.hideLog)return;this.isBrowser()?this.realConsoleLog(t,"color: "+o.color):this.realConsoleLog(t),this.errorTargetHandler(o,e)}catch(e){console.error("AnaLogger:",e.message)}}isExtendedOptionsPassed(e){return"object"==typeof e&&(e.hasOwnProperty("context")||e.hasOwnProperty("target"))}log(e,...t){var o;if(!this.isExtendedOptionsPassed(e))return o=this.generateDefaultContext(),void this.processOutput.apply(this,[o,e,...t]);let r=e;if("object"==typeof e.context){const s=Object.assign({},e);delete s.context,r=Object.assign({},e.context,s)}r.hasOwnProperty("context")&&(r=Object.assign({},this.generateDefaultContext(),r),delete r.context),this.processOutput.apply(this,[r,...t])}error(e,...t){if(!this.options.hideError){var o=this.generateErrorContext();if(this.isExtendedOptionsPassed(e))return e=Object.assign({},o,e),this.log(e,...t);var r=Array.prototype.slice.call(arguments);this.log(o,...r)}}overrideError(){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.error"),console.error=this.onDisplayError.bind(this)}overrideConsole({log:e=!0,info:t=!0,warn:o=!0,error:r=!1}={}){this.options.hideHookMessage||this.realConsoleLog("AnaLogger: Hook placed on console.log"),e&&(console.log=this.onDisplayLog.bind(this)),t&&(console.info=this.onDisplayLog.bind(this)),o&&(console.warn=this.onDisplayLog.bind(this)),r&&this.overrideError()}removeOverrideError(){console.warn=this.realConsoleError}removeOverride({log:e=!0,info:t=!0,warn:o=!0,error:r=!1}={}){e&&(console.log=this.realConsoleLog),t&&(console.info=this.realConsoleInfo),o&&(console.warn=this.realConsoleWarn),r&&this.removeOverrideError()}info(...e){return this.log(...e)}warn(...e){return this.log(...e)}alert(...e){if(this.isNode())return this.log(...e);e=e.join(" | ");alert(e)}assert(e,t=!0,...o){try{return"function"==typeof e?e(...o)!==t?(this.error("Asset failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0):e!==t?(this.error("Assert failed"),!1):(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"),!0)}catch(e){this.error("Unexpected error in assert")}return!1}}const anaLogger=new AnaLogger;export{anaLogger};