@reldens/utils 0.37.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/logger.js +85 -18
- package/lib/shortcuts.js +3 -8
- package/package.json +1 -1
package/lib/logger.js
CHANGED
|
@@ -25,6 +25,10 @@ class Logger
|
|
|
25
25
|
// - Implement different log systems (console.log, files logs, db log?).
|
|
26
26
|
// - Implement notifications system (email?), and make it configurable for the different log levels.
|
|
27
27
|
let context = this.context();
|
|
28
|
+
this.enableTraceBack = '';
|
|
29
|
+
this.logLevelBack = 3;
|
|
30
|
+
this.forcedDisabled = Boolean(context.RELDENS_FORCED_DISABLED_LOGS || false);
|
|
31
|
+
this.addTimeStamp = Boolean(context.RELDENS_INCLUDE_LOGS_TIMESTAMP || true);
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
context()
|
|
@@ -38,6 +42,44 @@ class Logger
|
|
|
38
42
|
return context;
|
|
39
43
|
}
|
|
40
44
|
|
|
45
|
+
enableTraceAll()
|
|
46
|
+
{
|
|
47
|
+
this.enableTraceBack = this.context().RELDENS_ENABLE_TRACE_FOR;
|
|
48
|
+
this.context().RELDENS_ENABLE_TRACE_FOR = 'all';
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
restoreTraceFor()
|
|
53
|
+
{
|
|
54
|
+
this.context().RELDENS_ENABLE_TRACE_FOR = this.enableTraceBack;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
setLogLevel(level)
|
|
59
|
+
{
|
|
60
|
+
this.logLevelBack = this.context().RELDENS_LOG_LEVEL;
|
|
61
|
+
this.context().RELDENS_LOG_LEVEL = level;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
restoreLogLevel()
|
|
66
|
+
{
|
|
67
|
+
this.context().RELDENS_LOG_LEVEL = this.logLevelBack;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
setForcedDisabled(forced)
|
|
72
|
+
{
|
|
73
|
+
this.forcedDisabled = forced;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
setAddTimeStamp(addTimeStamp)
|
|
78
|
+
{
|
|
79
|
+
this.addTimeStamp = addTimeStamp;
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
41
83
|
logLevel()
|
|
42
84
|
{
|
|
43
85
|
return this.context().RELDENS_LOG_LEVEL || 0;
|
|
@@ -50,81 +92,106 @@ class Logger
|
|
|
50
92
|
|
|
51
93
|
log(levelLabel, ...args)
|
|
52
94
|
{
|
|
53
|
-
let date = (new Date()).toISOString().slice(0, 19).replace('T', ' ')+' - ';
|
|
95
|
+
let date = !this.addTimeStamp ? '' : (new Date()).toISOString().slice(0, 19).replace('T', ' ')+' - ';
|
|
54
96
|
console.log(date+levelLabel.toUpperCase()+' -', ...args);
|
|
55
97
|
if(-1 !== this.enableTraceFor().indexOf('all') || -1 !== this.enableTraceFor().indexOf(levelLabel)){
|
|
56
98
|
if('function' !== typeof Error?.captureStackTrace){
|
|
57
99
|
console.log('Error.captureStackTrace is not available.', typeof Error?.captureStackTrace);
|
|
58
|
-
return;
|
|
100
|
+
return this;
|
|
59
101
|
}
|
|
60
102
|
let stackHolder = {};
|
|
61
103
|
Error.captureStackTrace(stackHolder, levelLabel);
|
|
62
104
|
console.log(stackHolder.stack);
|
|
63
105
|
}
|
|
106
|
+
return this;
|
|
64
107
|
}
|
|
65
108
|
|
|
66
109
|
debug(...args)
|
|
67
110
|
{
|
|
111
|
+
if(this.forcedDisabled){
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
68
114
|
if(8 > this.logLevel()){
|
|
69
|
-
return;
|
|
115
|
+
return this;
|
|
70
116
|
}
|
|
71
|
-
this.log('debug', ...args);
|
|
117
|
+
return this.log('debug', ...args);
|
|
72
118
|
}
|
|
73
119
|
|
|
74
120
|
info(...args)
|
|
75
121
|
{
|
|
122
|
+
if(this.forcedDisabled){
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
76
125
|
if(7 > this.logLevel()){
|
|
77
|
-
return;
|
|
126
|
+
return this;
|
|
78
127
|
}
|
|
79
|
-
this.log('info', ...args);
|
|
128
|
+
return this.log('info', ...args);
|
|
80
129
|
}
|
|
81
130
|
|
|
82
131
|
notice(...args)
|
|
83
132
|
{
|
|
133
|
+
if(this.forcedDisabled){
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
84
136
|
if(6 > this.logLevel()){
|
|
85
|
-
return;
|
|
137
|
+
return this;
|
|
86
138
|
}
|
|
87
|
-
this.log('notice', ...args);
|
|
139
|
+
return this.log('notice', ...args);
|
|
88
140
|
}
|
|
89
141
|
|
|
90
142
|
warning(...args)
|
|
91
143
|
{
|
|
144
|
+
if(this.forcedDisabled){
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
92
147
|
if(5 > this.logLevel()){
|
|
93
|
-
return;
|
|
148
|
+
return this;
|
|
94
149
|
}
|
|
95
|
-
this.log('warning', ...args);
|
|
150
|
+
return this.log('warning', ...args);
|
|
96
151
|
}
|
|
97
152
|
|
|
98
153
|
error(...args)
|
|
99
154
|
{
|
|
155
|
+
if(this.forcedDisabled){
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
100
158
|
if(4 > this.logLevel()){
|
|
101
|
-
return;
|
|
159
|
+
return this;
|
|
102
160
|
}
|
|
103
|
-
this.log('error', ...args);
|
|
161
|
+
return this.log('error', ...args);
|
|
104
162
|
}
|
|
105
163
|
|
|
106
164
|
critical(...args)
|
|
107
165
|
{
|
|
166
|
+
if(this.forcedDisabled){
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
108
169
|
if(3 > this.logLevel()){
|
|
109
|
-
return;
|
|
170
|
+
return this;
|
|
110
171
|
}
|
|
111
|
-
this.log('critical', ...args);
|
|
172
|
+
return this.log('critical', ...args);
|
|
112
173
|
}
|
|
113
174
|
|
|
114
175
|
alert(...args)
|
|
115
176
|
{
|
|
177
|
+
if(this.forcedDisabled){
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
116
180
|
if(2 > this.logLevel()){
|
|
117
|
-
return;
|
|
181
|
+
return this;
|
|
118
182
|
}
|
|
119
|
-
this.log('alert', ...args);
|
|
183
|
+
return this.log('alert', ...args);
|
|
120
184
|
}
|
|
121
185
|
|
|
122
186
|
emergency(...args)
|
|
123
187
|
{
|
|
188
|
+
if(this.forcedDisabled){
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
124
191
|
if(1 > this.logLevel()){
|
|
125
|
-
return;
|
|
192
|
+
return this;
|
|
126
193
|
}
|
|
127
|
-
this.log('emergency', ...args);
|
|
194
|
+
return this.log('emergency', ...args);
|
|
128
195
|
}
|
|
129
196
|
|
|
130
197
|
}
|
package/lib/shortcuts.js
CHANGED
|
@@ -263,11 +263,11 @@ class Shortcuts
|
|
|
263
263
|
fetchAllByPropertyOnObject(objectList, propertyName, propertyValue)
|
|
264
264
|
{
|
|
265
265
|
if(!objectList){
|
|
266
|
-
return
|
|
266
|
+
return [];
|
|
267
267
|
}
|
|
268
268
|
let objKeys = Object.keys(objectList);
|
|
269
269
|
if(0 === objKeys.length){
|
|
270
|
-
return
|
|
270
|
+
return [];
|
|
271
271
|
}
|
|
272
272
|
let results = [];
|
|
273
273
|
for(let i of objKeys){
|
|
@@ -338,7 +338,7 @@ class Shortcuts
|
|
|
338
338
|
|
|
339
339
|
roundToPrecision(number, precision = 4)
|
|
340
340
|
{
|
|
341
|
-
return Number(number.toFixed(precision));
|
|
341
|
+
return Number(Number(number).toFixed(precision));
|
|
342
342
|
}
|
|
343
343
|
|
|
344
344
|
randomValueFromArray(array)
|
|
@@ -384,11 +384,6 @@ class Shortcuts
|
|
|
384
384
|
return result;
|
|
385
385
|
}
|
|
386
386
|
|
|
387
|
-
numberWithDecimals(number, decimals = 0)
|
|
388
|
-
{
|
|
389
|
-
return Number(Number(number).toFixed(decimals));
|
|
390
|
-
}
|
|
391
|
-
|
|
392
387
|
cleanMessage(message, characterLimit)
|
|
393
388
|
{
|
|
394
389
|
if(!message){
|