analogger 1.1.7 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +25 -0
- package/.github/actions/checkout/action.yml +10 -0
- package/.github/workflows/test.yml +24 -0
- package/.github/workflows/versioning.yml +25 -0
- package/.releaserc +27 -0
- package/CHANGELOG.md +3 -0
- package/README.md +47 -14
- package/ci.md +0 -0
- package/package.json +64 -50
- package/test/integration.cjs +116 -0
- package/test/unit.cjs +455 -0
- package/dist/index-cjs.min.cjs +0 -1
- package/dist/index-esm.min.mjs +0 -1
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
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
env:
|
|
3
|
+
ANALOGGER_APP_NAME: "release/analogger"
|
|
4
|
+
on:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
- "**"
|
|
8
|
+
paths-ignore:
|
|
9
|
+
- "**.md"
|
|
10
|
+
- ".vscode/**"
|
|
11
|
+
jobs:
|
|
12
|
+
set-version:
|
|
13
|
+
runs-on: [self-hosted]
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v2
|
|
16
|
+
- name: Check out Git repository
|
|
17
|
+
uses: thimpat/analogger/.github/actions/checkout@ci
|
|
18
|
+
- name: Install dependencies
|
|
19
|
+
shell: powershell
|
|
20
|
+
run: npm install
|
|
21
|
+
- name: Test
|
|
22
|
+
shell: powershell
|
|
23
|
+
run: |
|
|
24
|
+
npm test
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: Build
|
|
2
|
+
env:
|
|
3
|
+
ANALOGGER_APP_NAME: "release/analogger"
|
|
4
|
+
on:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
- main
|
|
8
|
+
- ci
|
|
9
|
+
paths-ignore:
|
|
10
|
+
- "**.md"
|
|
11
|
+
- ".vscode/**"
|
|
12
|
+
jobs:
|
|
13
|
+
set-version:
|
|
14
|
+
runs-on: [self-hosted]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v2
|
|
17
|
+
- name: Check out Git repository
|
|
18
|
+
uses: thimpat/analogger/.github/actions/checkout@ci
|
|
19
|
+
- name: Install dependencies
|
|
20
|
+
shell: powershell
|
|
21
|
+
run: npm install
|
|
22
|
+
- name: Upgrade version
|
|
23
|
+
shell: powershell
|
|
24
|
+
run: |
|
|
25
|
+
npx semantic-release
|
package/.releaserc
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"branches": ["test", "main", "next"],
|
|
3
|
+
"dryRun": false,
|
|
4
|
+
"ci": true,
|
|
5
|
+
"debug": true,
|
|
6
|
+
"plugins": [
|
|
7
|
+
["@semantic-release/commit-analyzer",
|
|
8
|
+
{
|
|
9
|
+
"preset": "angular",
|
|
10
|
+
"releaseRules": [
|
|
11
|
+
{"type": "docs", "scope":"README", "release": "patch"},
|
|
12
|
+
{"type": "refactor", "release": "patch"},
|
|
13
|
+
{"type": "style", "release": "patch"}
|
|
14
|
+
],
|
|
15
|
+
"parserOpts": {
|
|
16
|
+
"noteKeywords": ["BREAKING CHANGE", "BREAKING CHANGES"]
|
|
17
|
+
}
|
|
18
|
+
}],
|
|
19
|
+
"@semantic-release/release-notes-generator",
|
|
20
|
+
"@semantic-release/changelog",
|
|
21
|
+
"@semantic-release/npm",
|
|
22
|
+
["@semantic-release/git", {
|
|
23
|
+
"assets": ["package.json", "CHANGELOG.md"],
|
|
24
|
+
"message": "AnaLogger Release: ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
|
25
|
+
}]
|
|
26
|
+
]
|
|
27
|
+
}
|
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
|
|
2
|
+

|
|
3
|
+

|
|
4
|
+
|
|
2
5
|
Analogger is a very simple logger for both Node and the Browser.
|
|
3
6
|
It is a library using both CJS and ESM.
|
|
4
7
|
It serves as a packaging example of **hybrid (CJS/ESM) module**.
|
|
@@ -87,6 +90,12 @@ Same as above, but for errors (console.error)
|
|
|
87
90
|
|
|
88
91
|
<br/>
|
|
89
92
|
|
|
93
|
+
### removeOverride() | removeOverrideError()
|
|
94
|
+
|
|
95
|
+
Remove overridden console methods
|
|
96
|
+
|
|
97
|
+
<br/>
|
|
98
|
+
|
|
90
99
|
### setContexts()
|
|
91
100
|
|
|
92
101
|
#### Contexts
|
|
@@ -97,18 +106,18 @@ A context allows grouping the logs by functionality by assigning them some colou
|
|
|
97
106
|
##### Examples
|
|
98
107
|
|
|
99
108
|
```javascript
|
|
100
|
-
const
|
|
109
|
+
const LOG_CONTEXTS = {STANDARD: null, TEST: {color: "#B18904"}, C1: null, C2: null, C3: null, DEFAULT: {}}
|
|
101
110
|
const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"};
|
|
102
111
|
|
|
103
|
-
anaLogger.setContexts(
|
|
112
|
+
anaLogger.setContexts(LOG_CONTEXTS);
|
|
104
113
|
|
|
105
|
-
anaLogger.log(
|
|
106
|
-
anaLogger.log(
|
|
107
|
-
anaLogger.log(
|
|
114
|
+
anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C1`);
|
|
115
|
+
anaLogger.log(LOG_CONTEXTS.C2, `Test Log example C2`);
|
|
116
|
+
anaLogger.log(LOG_CONTEXTS.C3, `Test Log example C3`);
|
|
108
117
|
```
|
|
109
118
|
|
|
110
|
-
See
|
|
111
|
-
For instance,
|
|
119
|
+
See LOG_CONTEXTS.C1 in this example to categorise the functionality we want to monitor.
|
|
120
|
+
For instance, LOG_CONTEXTS.INVESTIGATING_TIMER_EFFECT could display output related to something that has to
|
|
112
121
|
do with a timer.
|
|
113
122
|
|
|
114
123
|
The "Testing log 2" log will not show up in the console or the terminal.
|
|
@@ -134,19 +143,19 @@ setActiveTarget() allows hiding logs from other devs or roles.
|
|
|
134
143
|
##### Examples
|
|
135
144
|
|
|
136
145
|
```javascript
|
|
137
|
-
const
|
|
146
|
+
const LOG_CONTEXTS = {STANDARD: null, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
|
|
138
147
|
const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"};
|
|
139
148
|
|
|
140
|
-
anaLogger.setContexts(
|
|
149
|
+
anaLogger.setContexts(LOG_CONTEXTS);
|
|
141
150
|
anaLogger.setActiveTarget(LOG_TARGETS.DEV1); // <- You are DEV1
|
|
142
151
|
|
|
143
|
-
console.log({target:
|
|
144
|
-
console.log({target:
|
|
145
|
-
console.log({context:
|
|
146
|
-
console.log(`Testing log 4`); // You will see this. No context =
|
|
152
|
+
console.log({target: LOG_TARGETS.DEV1}, `Testing log 1`); // You will see this
|
|
153
|
+
console.log({target: LOG_TARGETS.DEV2}, `Testing log 2`); // You will not see this
|
|
154
|
+
console.log({context: LOG_CONTEXTS.STANDARD}, `Testing log 3`); // You will see this
|
|
155
|
+
console.log(`Testing log 4`); // You will see this. No context = LOG_CONTEXTS.ALL
|
|
147
156
|
|
|
148
157
|
|
|
149
|
-
anaLogger.log(
|
|
158
|
+
anaLogger.log(LOG_CONTEXTS.C1, `Test Log example C1`); // You will see this
|
|
150
159
|
```
|
|
151
160
|
|
|
152
161
|
<br/><br/>
|
|
@@ -163,3 +172,27 @@ anaLogger.assert(1 === 2)
|
|
|
163
172
|
anaLogger.assert(()=>true, true)
|
|
164
173
|
anaLogger.assert((a, b)=> a === b, true, 2, 2)
|
|
165
174
|
```
|
|
175
|
+
|
|
176
|
+
<br/><br/>
|
|
177
|
+
|
|
178
|
+
### setErrorHandlerForUserTarget()
|
|
179
|
+
|
|
180
|
+
When an error is detected and should be seen by your app consumers explicitly (for instance, you want to display a
|
|
181
|
+
dialogue box
|
|
182
|
+
to them), you can set a handler here. All other console.error will be working as usual (logging messages).
|
|
183
|
+
|
|
184
|
+
```javascript
|
|
185
|
+
anaLogger.setErrorHandlerForUserTarget(function (context/*, ...args*/)
|
|
186
|
+
{
|
|
187
|
+
// Detect whether we are in a browser
|
|
188
|
+
if (context.environnment === anaLogger.ENVIRONMENT_TYPE.BROWSER)
|
|
189
|
+
{
|
|
190
|
+
// When an error is detected in the Browser, the Browser will see this message
|
|
191
|
+
anaLogger.alert(`Users explicitly see this message`)
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
anaLogger.setActiveTarget(LOG_TARGETS.USER);
|
|
196
|
+
anaLogger.error({target: LOG_TARGETS.USER}, "Salut user!"); // Display an alert box
|
|
197
|
+
anaLogger.error("Hi user!"); // Log the message to the inspector
|
|
198
|
+
```
|
package/ci.md
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,50 +1,64 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "analogger",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"description": "Js Logger",
|
|
5
|
-
"main": "dist/index-cjs.min.cjs",
|
|
6
|
-
"module": "dist/index-esm.min.mjs",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"require": "./dist/index-cjs.min.cjs",
|
|
11
|
-
"import": "./dist/index-esm.min.mjs"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"convert-cjs:esm": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --output=src/esm/ --input=\"example/**/*.cjs\" --output=example/esm/ --config=\".toesm.cjs\"",
|
|
16
|
-
"convert-cjs:browser": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --input=\"example/**/*.cjs\" --output=src/esm-browser/ --output=example/esm-browser/ --config=\".toesm.cjs\" --solvedep",
|
|
17
|
-
"bundle:prod:cjs": "rollup --config rollup-cjs.config.js",
|
|
18
|
-
"bundle:prod:esm": "rollup --config rollup-esm.config.js",
|
|
19
|
-
"bundle:prod": "npm run convert-cjs:esm && npm run convert-cjs:browser && npm run bundle:prod:cjs && npm run bundle:prod:esm",
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"@babel/
|
|
32
|
-
"@babel/
|
|
33
|
-
"@
|
|
34
|
-
"@rollup/plugin-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "analogger",
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "Js Logger",
|
|
5
|
+
"main": "dist/index-cjs.min.cjs",
|
|
6
|
+
"module": "dist/index-esm.min.mjs",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/index-cjs.min.cjs",
|
|
11
|
+
"import": "./dist/index-esm.min.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"convert-cjs:esm": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --output=src/esm/ --input=\"example/**/*.cjs\" --output=example/esm/ --config=\".toesm.cjs\"",
|
|
16
|
+
"convert-cjs:browser": "toesm.cmd --input=\"src/cjs/**/*.cjs\" --input=\"example/**/*.cjs\" --output=src/esm-browser/ --output=example/esm-browser/ --config=\".toesm.cjs\" --solvedep",
|
|
17
|
+
"bundle:prod:cjs": "rollup --config rollup-cjs.config.js",
|
|
18
|
+
"bundle:prod:esm": "rollup --config rollup-esm.config.js",
|
|
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": "nyc mocha",
|
|
21
|
+
"demo": "npm run bundle:prod && node example/cjs/demo.cjs"
|
|
22
|
+
},
|
|
23
|
+
"author": "Patrice Thimothee",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"homepage": "https://github.com/thimpat/analogger/blob/main/README.md",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/thimpat/analogger.git"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@babel/core": "^7.17.0",
|
|
32
|
+
"@babel/eslint-parser": "^7.17.0",
|
|
33
|
+
"@babel/preset-env": "^7.16.11",
|
|
34
|
+
"@rollup/plugin-commonjs": "^21.0.1",
|
|
35
|
+
"@rollup/plugin-node-resolve": "^13.1.3",
|
|
36
|
+
"@semantic-release/changelog": "^6.0.1",
|
|
37
|
+
"@semantic-release/commit-analyzer": "^9.0.2",
|
|
38
|
+
"@semantic-release/git": "^10.0.1",
|
|
39
|
+
"@semantic-release/npm": "^9.0.0",
|
|
40
|
+
"@semantic-release/release-notes-generator": "^10.0.3",
|
|
41
|
+
"babel-eslint": "^10.1.0",
|
|
42
|
+
"capture-console": "^1.0.1",
|
|
43
|
+
"chai": "^4.3.6",
|
|
44
|
+
"chai-arrays": "^2.2.0",
|
|
45
|
+
"chai-spies": "^1.0.0",
|
|
46
|
+
"eslint": "^8.8.0",
|
|
47
|
+
"mocha": "^9.2.0",
|
|
48
|
+
"nyc": "^15.1.0",
|
|
49
|
+
"rollup": "^2.67.0",
|
|
50
|
+
"rollup-plugin-delete": "^2.0.0",
|
|
51
|
+
"rollup-plugin-uglify": "^6.0.4",
|
|
52
|
+
"semantic-release": "^19.0.2",
|
|
53
|
+
"sinon": "^13.0.1",
|
|
54
|
+
"to-esm": "^1.6.4"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"chalk": "^5.0.0",
|
|
58
|
+
"chalk-cjs": "npm:chalk@^4.1.2",
|
|
59
|
+
"color-convert": "^2.0.1",
|
|
60
|
+
"color-convert-cjs": "npm:color-convert@^2.0.1",
|
|
61
|
+
"rgb-hex": "^4.0.0",
|
|
62
|
+
"rgb-hex-cjs": "npm:rgb-hex@^3.0.0"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const chai = require("chai");
|
|
2
|
+
var capcon = require('capture-console');
|
|
3
|
+
const {anaLogger} = require("../src/cjs/ana-logger.cjs");
|
|
4
|
+
const {LOG_CONTEXTS, LOG_TARGETS} = require("../example/cjs/contexts-def.cjs");
|
|
5
|
+
const expect = chai.expect;
|
|
6
|
+
|
|
7
|
+
describe('In the Terminal', function ()
|
|
8
|
+
{
|
|
9
|
+
before(()=>
|
|
10
|
+
{
|
|
11
|
+
anaLogger.setContexts(LOG_CONTEXTS);
|
|
12
|
+
anaLogger.setTargets(LOG_TARGETS);
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
beforeEach(()=>
|
|
16
|
+
{
|
|
17
|
+
anaLogger.resetLogHistory()
|
|
18
|
+
anaLogger.keepLogHistory()
|
|
19
|
+
anaLogger.setOptions({silent: false, hideError: false})
|
|
20
|
+
anaLogger.removeOverride()
|
|
21
|
+
anaLogger.removeOverrideError()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
describe('AnaLogger', function ()
|
|
25
|
+
{
|
|
26
|
+
it('should emulate console.log', function ()
|
|
27
|
+
{
|
|
28
|
+
const captured = capcon.captureStdio(function ()
|
|
29
|
+
{
|
|
30
|
+
anaLogger.log(`Test Log example C1`);
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
expect(captured.stdout).to.contain(`Test Log example C1`)
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should hide console output when the console behaviour is overridden', function (done)
|
|
37
|
+
{
|
|
38
|
+
const captured = capcon.captureStdio(function ()
|
|
39
|
+
{
|
|
40
|
+
console.log(`Log Before override`);
|
|
41
|
+
anaLogger.setOptions({silent: true})
|
|
42
|
+
anaLogger.overrideConsole()
|
|
43
|
+
console.log(`Log After override`);
|
|
44
|
+
done()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
expect(captured.stdout).to.contain(`Log Before override`)
|
|
48
|
+
expect(captured.stdout).to.not.contain(`Log After override`)
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should hide console output but keep console input in the log history', function ()
|
|
52
|
+
{
|
|
53
|
+
const captured1 = capcon.captureStdio(function ()
|
|
54
|
+
{
|
|
55
|
+
anaLogger.keepLogHistory();
|
|
56
|
+
anaLogger.setOptions({silent: true, hideError: false})
|
|
57
|
+
anaLogger.log(LOG_CONTEXTS.C1, `Test Log example something again`);
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
const captured2 = capcon.captureStdio(function ()
|
|
61
|
+
{
|
|
62
|
+
console.log(anaLogger.getLogHistory())
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
expect(captured1.stdout).to.not.contain(`Test Log example something again`)
|
|
66
|
+
expect(captured2.stdout).to.contain(`Test Log example something again`)
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should hide console error when the console behaviour is overridden', function ()
|
|
70
|
+
{
|
|
71
|
+
const captured = capcon.captureStdio(function ()
|
|
72
|
+
{
|
|
73
|
+
console.log(`Log Before override`);
|
|
74
|
+
anaLogger.setOptions({hideError: true})
|
|
75
|
+
anaLogger.overrideConsole()
|
|
76
|
+
|
|
77
|
+
console.error(`Error Before override`);
|
|
78
|
+
anaLogger.overrideError()
|
|
79
|
+
console.error(`Error After override`);
|
|
80
|
+
|
|
81
|
+
console.log(`Log After override`);
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
expect(captured.stdout).to.contain(`Log Before override`)
|
|
85
|
+
expect(captured.stdout).to.not.contain(`Error After override`)
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should not show unrelated target logs', function ()
|
|
89
|
+
{
|
|
90
|
+
const captured = capcon.captureStdio(function ()
|
|
91
|
+
{
|
|
92
|
+
anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
|
|
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`);
|
|
95
|
+
anaLogger.log(`Test Log example with DEFAULT target`);
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
expect(captured.stdout).to.contain(`Test Log example with active target`)
|
|
99
|
+
expect(captured.stdout).to.contain(`Test Log example with DEFAULT target`)
|
|
100
|
+
expect(captured.stdout).to.not.contain(`Test Log example with DEV1 target`)
|
|
101
|
+
});
|
|
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
|
+
|
|
114
|
+
|
|
115
|
+
});
|
|
116
|
+
});
|
package/test/unit.cjs
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
const chai = require("chai");
|
|
2
|
+
const assertArrays = require("chai-arrays")
|
|
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
|
+
|
|
14
|
+
const spies = require('chai-spies');
|
|
15
|
+
|
|
16
|
+
chai.use(spies);
|
|
17
|
+
chai.use(assertArrays)
|
|
18
|
+
|
|
19
|
+
// sut
|
|
20
|
+
const {anaLogger} = require("../src/cjs/ana-logger.cjs");
|
|
21
|
+
const {LOG_CONTEXTS, LOG_TARGETS} = require("../example/cjs/contexts-def.cjs");
|
|
22
|
+
|
|
23
|
+
describe('AnaLogger', function ()
|
|
24
|
+
{
|
|
25
|
+
before(() =>
|
|
26
|
+
{
|
|
27
|
+
anaLogger.setContexts(LOG_CONTEXTS);
|
|
28
|
+
anaLogger.setTargets(LOG_TARGETS);
|
|
29
|
+
anaLogger.setActiveTarget(LOG_TARGETS.DEV3)
|
|
30
|
+
anaLogger.removeOverride({error: true})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
beforeEach(() =>
|
|
34
|
+
{
|
|
35
|
+
alert = sinon.spy()
|
|
36
|
+
chai.spy.on(myStub, 'myMethod');
|
|
37
|
+
|
|
38
|
+
anaLogger.resetLogHistory()
|
|
39
|
+
anaLogger.keepLogHistory()
|
|
40
|
+
anaLogger.resetLogFormatter();
|
|
41
|
+
})
|
|
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
|
+
|
|
89
|
+
describe('#log()', function ()
|
|
90
|
+
{
|
|
91
|
+
it('should emulate console.log', function ()
|
|
92
|
+
{
|
|
93
|
+
// Arrange
|
|
94
|
+
anaLogger.setOptions({silent: false, hideError: false})
|
|
95
|
+
|
|
96
|
+
// Act
|
|
97
|
+
anaLogger.log(`Test Log example C1`);
|
|
98
|
+
const output = anaLogger.getLogHistory()
|
|
99
|
+
|
|
100
|
+
// Assert
|
|
101
|
+
expect(output).to.contain(`Test Log example C1`)
|
|
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
|
+
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('#assert()', function ()
|
|
274
|
+
{
|
|
275
|
+
it('should evaluate condition expressions', function ()
|
|
276
|
+
{
|
|
277
|
+
const result = anaLogger.assert(1 === 1)
|
|
278
|
+
expect(result).to.be.true
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('should detect failing condition expressions', function ()
|
|
282
|
+
{
|
|
283
|
+
const result = anaLogger.assert(1 === 2)
|
|
284
|
+
expect(result).to.be.false
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('should evaluate function expressions', function ()
|
|
288
|
+
{
|
|
289
|
+
const result = anaLogger.assert(() => true, true)
|
|
290
|
+
expect(result).to.be.true
|
|
291
|
+
});
|
|
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
|
+
|
|
299
|
+
it('should evaluate more complex function expressions', function ()
|
|
300
|
+
{
|
|
301
|
+
const result = anaLogger.assert((a, b) => a === b, true, 2, 2)
|
|
302
|
+
expect(result).to.be.true
|
|
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
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe('#overrideConsole()', function ()
|
|
313
|
+
{
|
|
314
|
+
it('should override the console behaviour', function ()
|
|
315
|
+
{
|
|
316
|
+
anaLogger.overrideConsole({error: true})
|
|
317
|
+
console.log(`Test 1`)
|
|
318
|
+
expect(anaLogger.getLogHistory()).to.contain(`Test 1`)
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('#setLogFormat()', function ()
|
|
323
|
+
{
|
|
324
|
+
/**
|
|
325
|
+
* We use a spy here, but things would have been straightforward with a simple "done" + async on the "it"
|
|
326
|
+
*/
|
|
327
|
+
it('should replace the default formatter function with the given callback when invoking console.log', function ()
|
|
328
|
+
{
|
|
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
|
+
);
|
|
341
|
+
|
|
342
|
+
console.log(LOG_CONTEXTS.C1, `Test Log example C4 with new format`);
|
|
343
|
+
expect(myStub.myMethod).to.have.been.called;
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("should reset the formatter to its first value", () =>
|
|
347
|
+
{
|
|
348
|
+
anaLogger.setLogFormat(
|
|
349
|
+
() => "If you see this the test has failed"
|
|
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
|
+
});
|
|
362
|
+
|
|
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`);
|
|
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
|
+
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
});
|
package/dist/index-cjs.min.cjs
DELETED
|
@@ -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");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),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,{COLOR_TABLE,SYSTEM}=constants$1;class AnaLogger{system="";logIndex=0;contexts=[];targets={};activeTarget=null;indexColor=0;format="";options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),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}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)}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)}processLog(t={}){try{if(this.options.hideLog)return;if(!this.isTargetAllowed(t.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var o=e.join(" | "),r=this.format({...t,message:o});this.isBrowser()?(t.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,this.realConsoleLog("%c"+r,"color: "+t.color)):(t.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,this.realConsoleLog(chalk.hex(t.color)(r))),this.errorTargetHandler(t,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.processLog.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.processLog.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()}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{if("function"==typeof e)return e(...o)!==t?void this.error("Asset failed"):void(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"));if(e!==t)return void this.error("Asset failed");this.options.showPassingTests&&this.log("SUCCESS: Assert passed")}catch(e){this.error("Unexpected error in assert")}}}var anaLogger_1=anaLogger.anaLogger=new AnaLogger;exports.anaLogger=anaLogger_1,exports.default=anaLogger;
|
package/dist/index-esm.min.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
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="";options={hideHookMessage:!1};static ALIGN={LEFT:"LEFT",RIGHT:"RIGHT"};static ENVIRONMENT_TYPE={BROWSER:"BROWSER",NODE:"NODE",OTHER:"OTHER"};constructor(){this.system="object"==typeof process?SYSTEM.NODE:SYSTEM.BROWSER,this.format=this.onBuildLog.bind(this),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}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)}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)}processLog(t={}){try{if(this.options.hideLog)return;if(!this.isTargetAllowed(t.target))return;let e=Array.prototype.slice.call(arguments);e.shift();var o=e.join(" | "),r=this.format({...t,message:o});this.isBrowser()?(t.environnment=AnaLogger.ENVIRONMENT_TYPE.BROWSER,this.realConsoleLog("%c"+r,"color: "+t.color)):(t.environnment=AnaLogger.ENVIRONMENT_TYPE.NODE,this.realConsoleLog(chalk.hex(t.color)(r))),this.errorTargetHandler(t,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.processLog.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.processLog.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()}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{if("function"==typeof e)return e(...o)!==t?void this.error("Asset failed"):void(this.options.showPassingTests&&this.log("SUCCESS: Assert passed"));if(e!==t)return void this.error("Asset failed");this.options.showPassingTests&&this.log("SUCCESS: Assert passed")}catch(e){this.error("Unexpected error in assert")}}}const anaLogger=new AnaLogger;export{anaLogger};
|