analogger 1.1.2 → 1.1.6
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 +78 -15
- package/dist/index-cjs.min.cjs +1 -1
- package/dist/index-esm.min.mjs +1 -591
- package/package.json +1 -1
- package/docs/images/img.png +0 -0
- package/docs/images/img_1.png +0 -0
- package/docs/images/img_2.png +0 -0
- package/docs/images/img_3.png +0 -0
- package/example/cjs/contexts-def.cjs +0 -6
- package/example/cjs/demo.cjs +0 -62
- package/example/esm/contexts-def.mjs +0 -12
- package/example/esm/demo.mjs +0 -68
- package/example/esm-browser/contexts-def.mjs +0 -12
- package/example/esm-browser/demo.mjs +0 -68
- package/example/index.html +0 -23
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
Analogger is a very simple logger for both Node and the Browser.
|
|
3
|
-
It is a library using both CJS and ESM.
|
|
3
|
+
It is a library using both CJS and ESM.
|
|
4
|
+
It serves as a packaging example of **hybrid (CJS/ESM) module**.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -22,16 +23,12 @@ const {anaLogger} = require("analogger");
|
|
|
22
23
|
import {anaLogger} from "./node_modules/analogger/dist/index-esm.min.mjs";
|
|
23
24
|
```
|
|
24
25
|
|
|
25
|
-
or
|
|
26
|
+
### With a bundler or a transpiler
|
|
26
27
|
|
|
27
28
|
```javascript
|
|
28
29
|
import {anaLogger} from "analogger"
|
|
29
30
|
```
|
|
30
31
|
|
|
31
|
-
Depending on whether you use a bundler.
|
|
32
|
-
|
|
33
|
-
### Usages
|
|
34
|
-
|
|
35
32
|
<br/>
|
|
36
33
|
|
|
37
34
|
### Preview
|
|
@@ -40,9 +37,62 @@ Depending on whether you use a bundler.
|
|
|
40
37
|
|
|
41
38
|

|
|
42
39
|
|
|
43
|
-
|
|
40
|
+
<br/>
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
<br/>
|
|
45
|
+
|
|
46
|
+
### log() / info() / warn() / error()
|
|
47
|
+
|
|
48
|
+
Display a message in the terminal or the inspector, depending on where the process is running.
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
anaLogger.log(`I'am some log`);
|
|
52
|
+
anaLogger.info(`I'am some log`);
|
|
53
|
+
anaLogger.warn(`I'am some log`);
|
|
54
|
+
anaLogger.error(`I'am some log`);
|
|
55
|
+
```
|
|
56
|
+
<br/>
|
|
57
|
+
|
|
58
|
+
### alert()
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
anaLogger.alert(`I'am some log`);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Display the browser native message box if run from it; otherwise, it displays the message in the terminal.
|
|
65
|
+
|
|
66
|
+
<br/>
|
|
67
|
+
|
|
68
|
+
### overrideConsole() | setOptions()
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
anaLogger.setOptions({silent: true, hideError: false})
|
|
72
|
+
console.log(`Log Before override`);
|
|
73
|
+
anaLogger.overrideConsole()
|
|
74
|
+
console.log(`Log After override`);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Override console.log, console.info and console.warn. If you already have many console.log running in your system,
|
|
78
|
+
it allows hiding them all in one go.
|
|
79
|
+
In this example, the terminal (or inspector) will not show the message "Log After override". All following messages
|
|
80
|
+
either.
|
|
81
|
+
|
|
82
|
+
<br/>
|
|
83
|
+
|
|
84
|
+
### overrideError()
|
|
85
|
+
|
|
86
|
+
Same as above, but for errors (console.error)
|
|
87
|
+
|
|
88
|
+
<br/>
|
|
89
|
+
|
|
90
|
+
### setContexts()
|
|
91
|
+
|
|
92
|
+
#### Contexts
|
|
93
|
+
|
|
94
|
+
A context allows grouping the logs by functionality by assigning them some colour.
|
|
44
95
|
|
|
45
|
-
A context allows grouping the logs by functionality (role) by assigning them some colour.
|
|
46
96
|
|
|
47
97
|
##### Examples
|
|
48
98
|
|
|
@@ -58,7 +108,7 @@ anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
|
58
108
|
```
|
|
59
109
|
|
|
60
110
|
See LOG_CONTEXT.C1 in this example to categorise the functionality we want to monitor.
|
|
61
|
-
For instance, LOG_CONTEXT.INVESTIGATING_TIMER_EFFECT could
|
|
111
|
+
For instance, LOG_CONTEXT.INVESTIGATING_TIMER_EFFECT could display output related to something that has to
|
|
62
112
|
do with a timer.
|
|
63
113
|
|
|
64
114
|
The "Testing log 2" log will not show up in the console or the terminal.
|
|
@@ -74,17 +124,21 @@ The "Testing log 2" log will not show up in the console or the terminal.
|
|
|
74
124
|
|
|
75
125
|
<br/>
|
|
76
126
|
|
|
77
|
-
|
|
127
|
+
### setTargets() / setActiveTarget()
|
|
128
|
+
|
|
129
|
+
#### Targets
|
|
78
130
|
|
|
79
|
-
|
|
131
|
+
Targets allow defining some log categories. For example, they can be developers, roles, etc.
|
|
132
|
+
setActiveTarget() allows hiding logs from other devs or roles.
|
|
80
133
|
|
|
81
134
|
##### Examples
|
|
82
135
|
|
|
83
136
|
```javascript
|
|
84
|
-
anaLogger.
|
|
137
|
+
anaLogger.setActiveTarget(LOG_TARGETS.DEV1);
|
|
85
138
|
console.log({target: LOG_CONTEXT.DEV1}, `Testing log 1`)
|
|
86
139
|
console.log({target: LOG_CONTEXT.DEV2}, `Testing log 2`)
|
|
87
|
-
console.log({context: LOG_CONTEXT.
|
|
140
|
+
console.log({context: LOG_CONTEXT.DEV3}, `Testing log 3`)
|
|
141
|
+
console.log(`Testing log 4`)
|
|
88
142
|
```
|
|
89
143
|
|
|
90
144
|
|
|
@@ -101,6 +155,15 @@ anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
|
101
155
|
|
|
102
156
|
<br/><br/>
|
|
103
157
|
|
|
104
|
-
###
|
|
158
|
+
### assert()
|
|
105
159
|
|
|
106
|
-
|
|
160
|
+
You can set some tests directly in the code. It serves as early feedback.
|
|
161
|
+
It is helpful to guarantee that the code is running straight away rather than waiting on the CI to send its feedback.
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
anaLogger.assert(1 === 1)
|
|
166
|
+
anaLogger.assert(1 === 2)
|
|
167
|
+
anaLogger.assert(()=>true, true)
|
|
168
|
+
anaLogger.assert((a, b)=> a === b, true, 2, 2)
|
|
169
|
+
```
|
package/dist/index-cjs.min.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
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={};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,
|
|
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
CHANGED
|
@@ -1,591 +1 @@
|
|
|
1
|
-
function rgbHex(
|
|
2
|
-
const isPercent = (red + (alpha || '')).toString().includes('%');
|
|
3
|
-
|
|
4
|
-
if (typeof red === 'string') {
|
|
5
|
-
[red, green, blue, alpha] = red.match(/(0?\.?\d{1,3})%?\b/g).map(component => Number(component));
|
|
6
|
-
} else if (alpha !== undefined) {
|
|
7
|
-
alpha = Number.parseFloat(alpha);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (typeof red !== 'number' ||
|
|
11
|
-
typeof green !== 'number' ||
|
|
12
|
-
typeof blue !== 'number' ||
|
|
13
|
-
red > 255 ||
|
|
14
|
-
green > 255 ||
|
|
15
|
-
blue > 255
|
|
16
|
-
) {
|
|
17
|
-
throw new TypeError('Expected three numbers below 256');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (typeof alpha === 'number') {
|
|
21
|
-
if (!isPercent && alpha >= 0 && alpha <= 1) {
|
|
22
|
-
alpha = Math.round(255 * alpha);
|
|
23
|
-
} else if (isPercent && alpha >= 0 && alpha <= 100) {
|
|
24
|
-
alpha = Math.round(255 * alpha / 100);
|
|
25
|
-
} else {
|
|
26
|
-
throw new TypeError(`Expected alpha value (${alpha}) as a fraction or percentage`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
alpha = (alpha | 1 << 8).toString(16).slice(1); // eslint-disable-line no-mixed-operators
|
|
30
|
-
} else {
|
|
31
|
-
alpha = '';
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// TODO: Remove this ignore comment.
|
|
35
|
-
// eslint-disable-next-line no-mixed-operators
|
|
36
|
-
return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1) + alpha;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
41
|
-
* This file is generated following the conversion of
|
|
42
|
-
* [src/cjs/constants.cjs]
|
|
43
|
-
*
|
|
44
|
-
**/
|
|
45
|
-
const constants = {
|
|
46
|
-
COLOR_TABLE: [
|
|
47
|
-
"#d2466e", // Error context color
|
|
48
|
-
"#FFA07A", // Default context color
|
|
49
|
-
"#FF7F50",
|
|
50
|
-
"#FF6347",
|
|
51
|
-
"#FFE4B5",
|
|
52
|
-
"#ADFF2F",
|
|
53
|
-
"#808000",
|
|
54
|
-
"#40E0D0",
|
|
55
|
-
"#1E90FF",
|
|
56
|
-
"#EE82EE",
|
|
57
|
-
"#708090",
|
|
58
|
-
"#DEB887",
|
|
59
|
-
"#FE642E",
|
|
60
|
-
"#210B61",
|
|
61
|
-
"#088A4B",
|
|
62
|
-
"#5E610B",
|
|
63
|
-
"#FA8258",
|
|
64
|
-
"#088A68",
|
|
65
|
-
"#B40431",
|
|
66
|
-
],
|
|
67
|
-
SYSTEM: {
|
|
68
|
-
BROWSER: "BROWSER",
|
|
69
|
-
NODE: "NODE"
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const COLOR_TABLE = constants.COLOR_TABLE;
|
|
75
|
-
const SYSTEM = constants.SYSTEM;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
79
|
-
* This file is generated following the conversion of
|
|
80
|
-
* [src/cjs/ana-logger.cjs]
|
|
81
|
-
*
|
|
82
|
-
**/
|
|
83
|
-
const chalk = null;
|
|
84
|
-
|
|
85
|
-
class AnaLogger
|
|
86
|
-
{
|
|
87
|
-
system = ""
|
|
88
|
-
|
|
89
|
-
logIndex = 0;
|
|
90
|
-
contexts = [];
|
|
91
|
-
targets = {};
|
|
92
|
-
|
|
93
|
-
indexColor = 0;
|
|
94
|
-
|
|
95
|
-
format = ""
|
|
96
|
-
|
|
97
|
-
options = {
|
|
98
|
-
hideHookMessage: false
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
static ALIGN = {
|
|
102
|
-
LEFT : "LEFT",
|
|
103
|
-
RIGHT: "RIGHT"
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
static ENVIRONMENT_TYPE = {
|
|
107
|
-
BROWSER : "BROWSER",
|
|
108
|
-
NODE: "NODE",
|
|
109
|
-
OTHER: "OTHER"
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
constructor()
|
|
113
|
-
{
|
|
114
|
-
this.system = (typeof process === "object") ? SYSTEM.NODE : SYSTEM.BROWSER;
|
|
115
|
-
this.format = this.onBuildLog.bind(this);
|
|
116
|
-
this.errorTargetHandler = this.onError.bind(this);
|
|
117
|
-
this.errorUserTargetHandler = this.onErrorForUserTarget.bind(this);
|
|
118
|
-
|
|
119
|
-
this.setOptions(this.options);
|
|
120
|
-
|
|
121
|
-
this.realConsoleLog = console.log;
|
|
122
|
-
this.realConsoleInfo = console.info;
|
|
123
|
-
this.realConsoleWarn = console.warn;
|
|
124
|
-
this.realConsoleError = console.error;
|
|
125
|
-
|
|
126
|
-
this.ALIGN = AnaLogger.ALIGN;
|
|
127
|
-
this.ENVIRONMENT_TYPE = AnaLogger.ENVIRONMENT_TYPE;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Tell whether we are in a Node environment
|
|
132
|
-
* @returns {boolean}
|
|
133
|
-
*/
|
|
134
|
-
isNode()
|
|
135
|
-
{
|
|
136
|
-
return this.system === SYSTEM.NODE
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Tell whether the logger runs from a browser
|
|
141
|
-
* @returns {boolean}
|
|
142
|
-
*/
|
|
143
|
-
isBrowser()
|
|
144
|
-
{
|
|
145
|
-
return !this.isNode()
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
setOptions({
|
|
149
|
-
contextLenMax = 10,
|
|
150
|
-
idLenMax = 5,
|
|
151
|
-
lidLenMax = 5,
|
|
152
|
-
symbolLenMax = 2,
|
|
153
|
-
messageLenMax = 60,
|
|
154
|
-
hideLog = false,
|
|
155
|
-
hideError = false,
|
|
156
|
-
hideHookMessage = false,
|
|
157
|
-
silent = false
|
|
158
|
-
} = {})
|
|
159
|
-
{
|
|
160
|
-
this.options.contextLenMax = contextLenMax;
|
|
161
|
-
this.options.idLenMax = idLenMax;
|
|
162
|
-
this.options.lidLenMax = lidLenMax;
|
|
163
|
-
this.options.messageLenMax = messageLenMax;
|
|
164
|
-
this.options.symbolLenMax = symbolLenMax;
|
|
165
|
-
this.options.hideLog = !!hideLog;
|
|
166
|
-
this.options.hideError = !!hideError;
|
|
167
|
-
this.options.hideHookMessage = !!hideHookMessage;
|
|
168
|
-
|
|
169
|
-
if (silent)
|
|
170
|
-
{
|
|
171
|
-
this.options.hideLog = true;
|
|
172
|
-
this.options.hideHookMessage = true;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
truncateMessage(input = "", {fit = 0, align = AnaLogger.ALIGN.LEFT})
|
|
177
|
-
{
|
|
178
|
-
try
|
|
179
|
-
{
|
|
180
|
-
input = "" + input;
|
|
181
|
-
if (fit && input.length >= fit + 2)
|
|
182
|
-
{
|
|
183
|
-
input = input.substring(0, fit - 3) + "...";
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
input = align === AnaLogger.ALIGN.LEFT ? input.padEnd(fit + 1, " ") : input.padStart(fit + 1, " ");
|
|
187
|
-
return input
|
|
188
|
-
}
|
|
189
|
-
catch (e)
|
|
190
|
-
{
|
|
191
|
-
console.error(`AnaLogger:`, e);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Format inputs
|
|
197
|
-
* @see Override {@link setLogFormat}
|
|
198
|
-
* @param contextName
|
|
199
|
-
* @param id
|
|
200
|
-
* @param message
|
|
201
|
-
* @param lid
|
|
202
|
-
* @param symbol
|
|
203
|
-
* @returns {string}
|
|
204
|
-
*/
|
|
205
|
-
onBuildLog({contextName, id, message = "", lid = "", symbol = ""} = {})
|
|
206
|
-
{
|
|
207
|
-
// Time
|
|
208
|
-
const date = new Date();
|
|
209
|
-
let time = ('0' + date.getHours()).slice(-2) + ":" + ('0' + date.getMinutes()).slice(-2) + ":" + ('0' + date.getSeconds()).slice(-2);
|
|
210
|
-
|
|
211
|
-
// Display content in columns
|
|
212
|
-
time = this.truncateMessage(time, {fit: 7});
|
|
213
|
-
contextName = this.truncateMessage(contextName, {fit: this.options.contextLenMax, align: AnaLogger.ALIGN.RIGHT});
|
|
214
|
-
// id = this.truncateMessage(id, {fit: this.options.idLenMax})
|
|
215
|
-
lid = this.truncateMessage(lid, {fit: this.options.lidLenMax});
|
|
216
|
-
message = this.truncateMessage(message, {fit: this.options.messageLenMax});
|
|
217
|
-
symbol = this.truncateMessage(symbol, {fit: this.options.symbolLenMax});
|
|
218
|
-
|
|
219
|
-
return `[${time}] ${contextName}: (${lid}) ${symbol} ${message}`
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
onErrorForUserTarget(context, ...args)
|
|
223
|
-
{
|
|
224
|
-
this.errorUserTargetHandler(context, ...args);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
onError(context, ...args)
|
|
228
|
-
{
|
|
229
|
-
if (context.target === this.targets.USER)
|
|
230
|
-
{
|
|
231
|
-
this.onErrorForUserTarget(context, ...args);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Forward input to real console log
|
|
237
|
-
* @param args
|
|
238
|
-
*/
|
|
239
|
-
onDisplayLog(...args)
|
|
240
|
-
{
|
|
241
|
-
this.log(...args);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Forward input to real console log
|
|
246
|
-
* @param args
|
|
247
|
-
*/
|
|
248
|
-
onDisplayError(...args)
|
|
249
|
-
{
|
|
250
|
-
this.error(...args);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Set log template
|
|
255
|
-
* @param format
|
|
256
|
-
*/
|
|
257
|
-
setLogFormat(format)
|
|
258
|
-
{
|
|
259
|
-
if (typeof format !== 'function')
|
|
260
|
-
{
|
|
261
|
-
console.error(`Invalid parameter for setFormat. It is expecting a function or method.`);
|
|
262
|
-
return false
|
|
263
|
-
}
|
|
264
|
-
this.format = format.bind(this);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
setErrorHandler(handler)
|
|
268
|
-
{
|
|
269
|
-
this.errorTargetHandler = handler.bind(this);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
setErrorHandlerForUserTarget(handler)
|
|
273
|
-
{
|
|
274
|
-
this.errorUserTargetHandler = handler.bind(this);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// ------------------------------------------------
|
|
278
|
-
// Color
|
|
279
|
-
// ------------------------------------------------
|
|
280
|
-
|
|
281
|
-
// ------------------------------------------------
|
|
282
|
-
// Log Contexts
|
|
283
|
-
// ------------------------------------------------
|
|
284
|
-
isContext(context)
|
|
285
|
-
{
|
|
286
|
-
if (
|
|
287
|
-
!(typeof context === 'object' &&
|
|
288
|
-
!Array.isArray(context) &&
|
|
289
|
-
context !== null)
|
|
290
|
-
)
|
|
291
|
-
{
|
|
292
|
-
return false
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return (context.hasOwnProperty("contextName") && context.hasOwnProperty("target"))
|
|
296
|
-
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
generateDefaultContext()
|
|
300
|
-
{
|
|
301
|
-
const defaultContext = {
|
|
302
|
-
name : "DEFAULT",
|
|
303
|
-
contextName: "DEFAULT",
|
|
304
|
-
target : "DEV",
|
|
305
|
-
symbol : "⚡"
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
defaultContext.id = this.logIndex++;
|
|
309
|
-
defaultContext.color = COLOR_TABLE[1];
|
|
310
|
-
return defaultContext
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
generateNewContext()
|
|
314
|
-
{
|
|
315
|
-
const newContext = this.generateDefaultContext();
|
|
316
|
-
newContext.color = COLOR_TABLE[(this.indexColor++) % (COLOR_TABLE.length - 3) + 2];
|
|
317
|
-
newContext.symbol = "";
|
|
318
|
-
return newContext
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
generateErrorContext()
|
|
322
|
-
{
|
|
323
|
-
const errorContext = this.generateDefaultContext();
|
|
324
|
-
errorContext.color = COLOR_TABLE[0];
|
|
325
|
-
errorContext.symbol = "v";
|
|
326
|
-
errorContext.error = true;
|
|
327
|
-
return errorContext
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
allegeProperties(entry)
|
|
331
|
-
{
|
|
332
|
-
let converted = entry;
|
|
333
|
-
|
|
334
|
-
const defaultContext = this.generateNewContext();
|
|
335
|
-
|
|
336
|
-
if (!converted)
|
|
337
|
-
{
|
|
338
|
-
converted = {
|
|
339
|
-
contextName: "DEFAULT",
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
if (Array.isArray(converted))
|
|
344
|
-
{
|
|
345
|
-
throw new Error(`AnaLogger: Cannot convert Array [${JSON.stringify(converted)}] to context`);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
if (typeof converted === "string" || converted instanceof String)
|
|
349
|
-
{
|
|
350
|
-
converted = {
|
|
351
|
-
contextName: converted
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (
|
|
356
|
-
typeof converted !== "object"
|
|
357
|
-
)
|
|
358
|
-
{
|
|
359
|
-
throw new Error(`AnaLogger: Cannot convert Unknown [${JSON.stringify(converted)}] to context`);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
converted = Object.assign({}, defaultContext, converted);
|
|
363
|
-
|
|
364
|
-
if (converted.color.toLowerCase().indexOf("rgb") > -1)
|
|
365
|
-
{
|
|
366
|
-
converted.color = "#" + rgbHex(converted.color);
|
|
367
|
-
}
|
|
368
|
-
else if (converted.color.indexOf("#") === -1)
|
|
369
|
-
;
|
|
370
|
-
|
|
371
|
-
return converted;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Load the context names that should be available to the environment.
|
|
376
|
-
* They are defined by the user.
|
|
377
|
-
* @see Context definitions {@link ./example/cjs/contexts-def.cjs}
|
|
378
|
-
* @param contextTable
|
|
379
|
-
*/
|
|
380
|
-
setContexts(contextTable)
|
|
381
|
-
{
|
|
382
|
-
const arr = Object.keys(contextTable);
|
|
383
|
-
contextTable["DEFAULT"] = this.contexts["DEFAULT"] = this.generateDefaultContext();
|
|
384
|
-
contextTable["ERROR"] = this.contexts["ERROR"] = this.generateErrorContext();
|
|
385
|
-
arr.forEach((key) =>
|
|
386
|
-
{
|
|
387
|
-
const contextPassed = contextTable[key] || {};
|
|
388
|
-
contextPassed.contextName = key;
|
|
389
|
-
contextPassed.name = key;
|
|
390
|
-
this.contexts[key] = this.allegeProperties(contextPassed);
|
|
391
|
-
contextTable[key] = this.contexts[key];
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
setTargets(targetTable = {})
|
|
396
|
-
{
|
|
397
|
-
this.targets = Object.assign({}, targetTable, {ALL: "ALL", USER: "USER"});
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
enableContexts(contextNames)
|
|
401
|
-
{
|
|
402
|
-
this.contexts.forEach((context) =>
|
|
403
|
-
{
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
*
|
|
409
|
-
* @returns {{}}
|
|
410
|
-
*/
|
|
411
|
-
getActiveLogContexts()
|
|
412
|
-
{
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
// ------------------------------------------------
|
|
417
|
-
// Logging methods
|
|
418
|
-
// ------------------------------------------------
|
|
419
|
-
/**
|
|
420
|
-
* Display log following template
|
|
421
|
-
* @param context
|
|
422
|
-
*/
|
|
423
|
-
processLog(context = {})
|
|
424
|
-
{
|
|
425
|
-
try
|
|
426
|
-
{
|
|
427
|
-
if (this.options.hideLog)
|
|
428
|
-
{
|
|
429
|
-
return
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
let args = Array.prototype.slice.call(arguments);
|
|
433
|
-
args.shift();
|
|
434
|
-
|
|
435
|
-
const message = args.join(" | ");
|
|
436
|
-
|
|
437
|
-
const text = this.format({...context, message});
|
|
438
|
-
if (this.isBrowser())
|
|
439
|
-
{
|
|
440
|
-
context.environnment = AnaLogger.ENVIRONMENT_TYPE.BROWSER;
|
|
441
|
-
this.realConsoleLog(`%c${text}`, `color: ${context.color}`);
|
|
442
|
-
}
|
|
443
|
-
else
|
|
444
|
-
{
|
|
445
|
-
context.environnment = AnaLogger.ENVIRONMENT_TYPE.NODE;
|
|
446
|
-
this.realConsoleLog(chalk.hex(context.color)(text));
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
this.errorTargetHandler(context, args);
|
|
450
|
-
}
|
|
451
|
-
catch (e)
|
|
452
|
-
{
|
|
453
|
-
console.error(`AnaLogger:`, e.message);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* Check that a parameter (should be the first) uses the expected format.
|
|
459
|
-
* @param options
|
|
460
|
-
* @returns {boolean}
|
|
461
|
-
*/
|
|
462
|
-
isExtendedOptionsPassed(options)
|
|
463
|
-
{
|
|
464
|
-
if (typeof options !== "object")
|
|
465
|
-
{
|
|
466
|
-
return false;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
return options.hasOwnProperty("context") || options.hasOwnProperty("target");
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
/**
|
|
473
|
-
* console.log with options set on the first parameter to dictate console log behaviours
|
|
474
|
-
* @param options
|
|
475
|
-
* @param args
|
|
476
|
-
*/
|
|
477
|
-
log(options, ...args)
|
|
478
|
-
{
|
|
479
|
-
if (!this.isExtendedOptionsPassed(options))
|
|
480
|
-
{
|
|
481
|
-
const defaultContext = this.generateDefaultContext();
|
|
482
|
-
this.processLog.apply(this, [defaultContext, options, ...args]);
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
let context = options;
|
|
487
|
-
if (typeof options.context === "object")
|
|
488
|
-
{
|
|
489
|
-
const moreOptions = Object.assign({}, options);
|
|
490
|
-
delete moreOptions.context;
|
|
491
|
-
context = Object.assign({}, options.context, moreOptions);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if (context.hasOwnProperty("context"))
|
|
495
|
-
{
|
|
496
|
-
context = Object.assign({}, this.generateDefaultContext(), context);
|
|
497
|
-
delete context.context;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
// let args0 = Array.prototype.slice.call(arguments);
|
|
501
|
-
// args0.unshift(options)
|
|
502
|
-
this.processLog.apply(this, [context, ...args]);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
error(options, ...args)
|
|
506
|
-
{
|
|
507
|
-
if (this.options.hideError)
|
|
508
|
-
{
|
|
509
|
-
return
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const errorContext = this.generateErrorContext();
|
|
513
|
-
|
|
514
|
-
if (this.isExtendedOptionsPassed(options))
|
|
515
|
-
{
|
|
516
|
-
options = Object.assign({}, errorContext, options);
|
|
517
|
-
return this.log(options, ...args)
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
let args0 = Array.prototype.slice.call(arguments);
|
|
521
|
-
this.log(errorContext, ...args0);
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
overrideError()
|
|
525
|
-
{
|
|
526
|
-
if (!this.options.hideHookMessage)
|
|
527
|
-
{
|
|
528
|
-
this.realConsoleLog(`AnaLogger: Hook placed on console.error`);
|
|
529
|
-
}
|
|
530
|
-
console.error = this.onDisplayError.bind(this);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
overrideConsole({log = true, info = true, warn = true, error = false} = {})
|
|
534
|
-
{
|
|
535
|
-
if (!this.options.hideHookMessage)
|
|
536
|
-
{
|
|
537
|
-
this.realConsoleLog(`AnaLogger: Hook placed on console.log`);
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
if (log)
|
|
541
|
-
{
|
|
542
|
-
console.log = this.onDisplayLog.bind(this);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
if (info)
|
|
546
|
-
{
|
|
547
|
-
console.info = this.onDisplayLog.bind(this);
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if (warn)
|
|
551
|
-
{
|
|
552
|
-
console.warn = this.onDisplayLog.bind(this);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
if (error)
|
|
556
|
-
{
|
|
557
|
-
this.overrideError();
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
info(...args)
|
|
562
|
-
{
|
|
563
|
-
return this.log(...args)
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
warn(...args)
|
|
567
|
-
{
|
|
568
|
-
return this.log(...args)
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
alert(...args)
|
|
572
|
-
{
|
|
573
|
-
if (this.isNode())
|
|
574
|
-
{
|
|
575
|
-
return this.log(...args)
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
const message = args.join(" | ");
|
|
579
|
-
alert(message);
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
assert()
|
|
583
|
-
{
|
|
584
|
-
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
const anaLogger = new AnaLogger();
|
|
590
|
-
|
|
591
|
-
export { anaLogger };
|
|
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};
|
package/package.json
CHANGED
package/docs/images/img.png
DELETED
|
Binary file
|
package/docs/images/img_1.png
DELETED
|
Binary file
|
package/docs/images/img_2.png
DELETED
|
Binary file
|
package/docs/images/img_3.png
DELETED
|
Binary file
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* @type {{C3: null, TEST: {symbol: string, color: string}, STANDARD: {}, C1: null, DEFAULT: {}, C2: null}}
|
|
4
|
-
*/
|
|
5
|
-
module.exports.LOG_CONTEXT = {STANDARD: {}, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
|
|
6
|
-
module.exports.LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"}
|
package/example/cjs/demo.cjs
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
const {LOG_CONTEXT, LOG_TARGETS} = require("./contexts-def.cjs")
|
|
2
|
-
const {anaLogger} = require("../../dist/index-cjs.min.cjs");
|
|
3
|
-
|
|
4
|
-
anaLogger.setContexts(LOG_CONTEXT);
|
|
5
|
-
anaLogger.setTargets(LOG_TARGETS);
|
|
6
|
-
anaLogger.setOptions({silent: false, hideError: false})
|
|
7
|
-
|
|
8
|
-
console.log("==========================");
|
|
9
|
-
anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
|
|
10
|
-
anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
|
|
11
|
-
anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
12
|
-
|
|
13
|
-
console.log(`Log Before override`);
|
|
14
|
-
anaLogger.overrideConsole()
|
|
15
|
-
console.log(`Log After override`);
|
|
16
|
-
console.error(`Error Before override`);
|
|
17
|
-
anaLogger.overrideError()
|
|
18
|
-
console.error(`Error After override`);
|
|
19
|
-
console.log("==========================");
|
|
20
|
-
|
|
21
|
-
anaLogger.log(LOG_CONTEXT.STANDARD, `Basic Log example 2`, "+Something 0", "+Something 1");
|
|
22
|
-
anaLogger.log({context: LOG_CONTEXT.TEST, lid: 100000}, `Test Log example`);
|
|
23
|
-
anaLogger.log(LOG_CONTEXT.TEST, `Test Log example`, "+Something 3");
|
|
24
|
-
anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
|
|
25
|
-
anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
|
|
26
|
-
anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
27
|
-
|
|
28
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C4`);
|
|
29
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C5`);
|
|
30
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C6`);
|
|
31
|
-
|
|
32
|
-
console.error({context: LOG_CONTEXT.ERROR, lid: 200000}, `Testing Error 1`)
|
|
33
|
-
console.error(LOG_CONTEXT.ERROR, `Testing Error 2`)
|
|
34
|
-
console.error(`Testing Error 3`)
|
|
35
|
-
console.error(undefined, `Testing Error 4`)
|
|
36
|
-
|
|
37
|
-
console.error({context: LOG_CONTEXT.ERROR, target: LOG_TARGETS.USER, lid: 200010}, `Testing Error 4`)
|
|
38
|
-
|
|
39
|
-
anaLogger.alert(`Hello from alert`, {aaa: 1012})
|
|
40
|
-
|
|
41
|
-
anaLogger.setErrorHandlerForUserTarget(function (context/*, ...args*/)
|
|
42
|
-
{
|
|
43
|
-
if (context.environnment === anaLogger.ENVIRONMENT_TYPE.BROWSER)
|
|
44
|
-
{
|
|
45
|
-
alert(`Users explicitly see this message`)
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
console.error({context: LOG_CONTEXT.ERROR, target: LOG_TARGETS.USER, lid: 200020}, `Testing Error that triggers a special handler`)
|
|
50
|
-
|
|
51
|
-
anaLogger.setLogFormat(
|
|
52
|
-
function({contextName, message})
|
|
53
|
-
{
|
|
54
|
-
return `${contextName}: ${message}`
|
|
55
|
-
}
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C4 with new format`);
|
|
59
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C5 with new format`);
|
|
60
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C6 with new format`);
|
|
61
|
-
|
|
62
|
-
anaLogger.log(`Basic Log example 1`);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
3
|
-
* This file is generated following the conversion of
|
|
4
|
-
* [example/cjs/contexts-def.cjs]
|
|
5
|
-
*
|
|
6
|
-
**/
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @type {{C3: null, TEST: {symbol: string, color: string}, STANDARD: {}, C1: null, DEFAULT: {}, C2: null}}
|
|
10
|
-
*/
|
|
11
|
-
export const LOG_CONTEXT = {STANDARD: {}, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
|
|
12
|
-
export const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"}
|
package/example/esm/demo.mjs
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
3
|
-
* This file is generated following the conversion of
|
|
4
|
-
* [example/cjs/demo.cjs]
|
|
5
|
-
*
|
|
6
|
-
**/
|
|
7
|
-
import {LOG_CONTEXT, LOG_TARGETS} from "./contexts-def.mjs"
|
|
8
|
-
import {anaLogger} from "../../dist/index-esm.min.mjs";
|
|
9
|
-
|
|
10
|
-
anaLogger.setContexts(LOG_CONTEXT);
|
|
11
|
-
anaLogger.setTargets(LOG_TARGETS);
|
|
12
|
-
anaLogger.setOptions({silent: false, hideError: false})
|
|
13
|
-
|
|
14
|
-
console.log("==========================");
|
|
15
|
-
anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
|
|
16
|
-
anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
|
|
17
|
-
anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
18
|
-
|
|
19
|
-
console.log(`Log Before override`);
|
|
20
|
-
anaLogger.overrideConsole()
|
|
21
|
-
console.log(`Log After override`);
|
|
22
|
-
console.error(`Error Before override`);
|
|
23
|
-
anaLogger.overrideError()
|
|
24
|
-
console.error(`Error After override`);
|
|
25
|
-
console.log("==========================");
|
|
26
|
-
|
|
27
|
-
anaLogger.log(LOG_CONTEXT.STANDARD, `Basic Log example 2`, "+Something 0", "+Something 1");
|
|
28
|
-
anaLogger.log({context: LOG_CONTEXT.TEST, lid: 100000}, `Test Log example`);
|
|
29
|
-
anaLogger.log(LOG_CONTEXT.TEST, `Test Log example`, "+Something 3");
|
|
30
|
-
anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
|
|
31
|
-
anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
|
|
32
|
-
anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
33
|
-
|
|
34
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C4`);
|
|
35
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C5`);
|
|
36
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C6`);
|
|
37
|
-
|
|
38
|
-
console.error({context: LOG_CONTEXT.ERROR, lid: 200000}, `Testing Error 1`)
|
|
39
|
-
console.error(LOG_CONTEXT.ERROR, `Testing Error 2`)
|
|
40
|
-
console.error(`Testing Error 3`)
|
|
41
|
-
console.error(undefined, `Testing Error 4`)
|
|
42
|
-
|
|
43
|
-
console.error({context: LOG_CONTEXT.ERROR, target: LOG_TARGETS.USER, lid: 200010}, `Testing Error 4`)
|
|
44
|
-
|
|
45
|
-
anaLogger.alert(`Hello from alert`, {aaa: 1012})
|
|
46
|
-
|
|
47
|
-
anaLogger.setErrorHandlerForUserTarget(function (context/*, ...args*/)
|
|
48
|
-
{
|
|
49
|
-
if (context.environnment === anaLogger.ENVIRONMENT_TYPE.BROWSER)
|
|
50
|
-
{
|
|
51
|
-
alert(`Users explicitly see this message`)
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
console.error({context: LOG_CONTEXT.ERROR, target: LOG_TARGETS.USER, lid: 200020}, `Testing Error that triggers a special handler`)
|
|
56
|
-
|
|
57
|
-
anaLogger.setLogFormat(
|
|
58
|
-
function({contextName, message})
|
|
59
|
-
{
|
|
60
|
-
return `${contextName}: ${message}`
|
|
61
|
-
}
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C4 with new format`);
|
|
65
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C5 with new format`);
|
|
66
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C6 with new format`);
|
|
67
|
-
|
|
68
|
-
anaLogger.log(`Basic Log example 1`);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
3
|
-
* This file is generated following the conversion of
|
|
4
|
-
* [example/cjs/contexts-def.cjs]
|
|
5
|
-
*
|
|
6
|
-
**/
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @type {{C3: null, TEST: {symbol: string, color: string}, STANDARD: {}, C1: null, DEFAULT: {}, C2: null}}
|
|
10
|
-
*/
|
|
11
|
-
export const LOG_CONTEXT = {STANDARD: {}, TEST: {color: "#B18904", symbol: "⏰"}, C1: null, C2: null, C3: null, DEFAULT: {}}
|
|
12
|
-
export const LOG_TARGETS = {ALL: "ALL", DEV1: "TOM", DEV2: "TIM", USER: "USER"}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
3
|
-
* This file is generated following the conversion of
|
|
4
|
-
* [example/cjs/demo.cjs]
|
|
5
|
-
*
|
|
6
|
-
**/
|
|
7
|
-
import {LOG_CONTEXT, LOG_TARGETS} from "./contexts-def.mjs"
|
|
8
|
-
import {anaLogger} from "../../dist/index-esm.min.mjs";
|
|
9
|
-
|
|
10
|
-
anaLogger.setContexts(LOG_CONTEXT);
|
|
11
|
-
anaLogger.setTargets(LOG_TARGETS);
|
|
12
|
-
anaLogger.setOptions({silent: false, hideError: false})
|
|
13
|
-
|
|
14
|
-
console.log("==========================");
|
|
15
|
-
anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
|
|
16
|
-
anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
|
|
17
|
-
anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
18
|
-
|
|
19
|
-
console.log(`Log Before override`);
|
|
20
|
-
anaLogger.overrideConsole()
|
|
21
|
-
console.log(`Log After override`);
|
|
22
|
-
console.error(`Error Before override`);
|
|
23
|
-
anaLogger.overrideError()
|
|
24
|
-
console.error(`Error After override`);
|
|
25
|
-
console.log("==========================");
|
|
26
|
-
|
|
27
|
-
anaLogger.log(LOG_CONTEXT.STANDARD, `Basic Log example 2`, "+Something 0", "+Something 1");
|
|
28
|
-
anaLogger.log({context: LOG_CONTEXT.TEST, lid: 100000}, `Test Log example`);
|
|
29
|
-
anaLogger.log(LOG_CONTEXT.TEST, `Test Log example`, "+Something 3");
|
|
30
|
-
anaLogger.log(LOG_CONTEXT.C1, `Test Log example C1`);
|
|
31
|
-
anaLogger.log(LOG_CONTEXT.C2, `Test Log example C2`);
|
|
32
|
-
anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
33
|
-
|
|
34
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C4`);
|
|
35
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C5`);
|
|
36
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C6`);
|
|
37
|
-
|
|
38
|
-
console.error({context: LOG_CONTEXT.ERROR, lid: 200000}, `Testing Error 1`)
|
|
39
|
-
console.error(LOG_CONTEXT.ERROR, `Testing Error 2`)
|
|
40
|
-
console.error(`Testing Error 3`)
|
|
41
|
-
console.error(undefined, `Testing Error 4`)
|
|
42
|
-
|
|
43
|
-
console.error({context: LOG_CONTEXT.ERROR, target: LOG_TARGETS.USER, lid: 200010}, `Testing Error 4`)
|
|
44
|
-
|
|
45
|
-
anaLogger.alert(`Hello from alert`, {aaa: 1012})
|
|
46
|
-
|
|
47
|
-
anaLogger.setErrorHandlerForUserTarget(function (context/*, ...args*/)
|
|
48
|
-
{
|
|
49
|
-
if (context.environnment === anaLogger.ENVIRONMENT_TYPE.BROWSER)
|
|
50
|
-
{
|
|
51
|
-
alert(`Users explicitly see this message`)
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
console.error({context: LOG_CONTEXT.ERROR, target: LOG_TARGETS.USER, lid: 200020}, `Testing Error that triggers a special handler`)
|
|
56
|
-
|
|
57
|
-
anaLogger.setLogFormat(
|
|
58
|
-
function({contextName, message})
|
|
59
|
-
{
|
|
60
|
-
return `${contextName}: ${message}`
|
|
61
|
-
}
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C4 with new format`);
|
|
65
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C5 with new format`);
|
|
66
|
-
console.log(LOG_CONTEXT.C1, `Test Log example C6 with new format`);
|
|
67
|
-
|
|
68
|
-
anaLogger.log(`Basic Log example 1`);
|
package/example/index.html
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<title>Demo</title>
|
|
6
|
-
<script>
|
|
7
|
-
// "rgb-hex": "./../node_modules/rgb-hex/index.js"
|
|
8
|
-
</script>
|
|
9
|
-
<script type="importmap">
|
|
10
|
-
{
|
|
11
|
-
"imports": {
|
|
12
|
-
"chalk": "./../node_modules/chalk/source/index.js",
|
|
13
|
-
"color-convert": "./../node_modules/color-convert/index.js"
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
</script>
|
|
17
|
-
|
|
18
|
-
</head>
|
|
19
|
-
<body>
|
|
20
|
-
|
|
21
|
-
<script type="module" src="esm/demo.mjs"></script>
|
|
22
|
-
</body>
|
|
23
|
-
</html>
|