analogger 1.1.3 → 1.1.4
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 +74 -14
- package/dist/index-cjs.min.cjs +1 -1
- package/dist/index-esm.min.mjs +1 -1
- package/package.json +1 -1
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 ran from it, otherwise 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
|
|
|
@@ -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()
|
|
78
128
|
|
|
79
|
-
|
|
129
|
+
#### Targets
|
|
130
|
+
|
|
131
|
+
Targets allow to define some log categories. They can be developpers, 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,12 @@ anaLogger.log(LOG_CONTEXT.C3, `Test Log example C3`);
|
|
|
101
155
|
|
|
102
156
|
<br/><br/>
|
|
103
157
|
|
|
104
|
-
###
|
|
158
|
+
### assert()
|
|
159
|
+
|
|
160
|
+
You can set some tests directly in the code. It serves as early feedback.
|
|
161
|
+
It is useful to guarantee that the code is running straight away, rather than waiting on the CI to send its feedback.
|
|
105
162
|
|
|
106
|
-
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
anaLogger.asset((a, b)=> a === b, true)
|
|
166
|
+
```
|
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){try{if("function"==typeof e)return e()!==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 +1 @@
|
|
|
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={};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
|
+
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){try{if("function"==typeof e)return e()!==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};
|