@63klabs/cache-data 1.3.0 → 1.3.3
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/CHANGELOG.md +11 -0
- package/LICENSE.txt +1 -1
- package/package.json +2 -2
- package/src/lib/tools/APIRequest.class.js +1 -3
- package/src/lib/tools/DebugAndLog.class.js +206 -148
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,17 @@ Report all vulnerabilities under the [Security menu](https://github.com/63Klabs/
|
|
|
8
8
|
|
|
9
9
|
> Note: This project is still in beta. Even though changes are tested and breaking changes are avoided, things may break.
|
|
10
10
|
|
|
11
|
+
## 1.3.3 (2025-09-14)
|
|
12
|
+
|
|
13
|
+
### Enhancements
|
|
14
|
+
|
|
15
|
+
- `DebugAndLog`: The environment variable `AWS_LAMBDA_LOG_LEVEL` is now checked as well for setting logging level. `LOG_LEVEL` has priority. (v1.3.2)
|
|
16
|
+
- `DebugAndLog`: The log level environment variable now accepts strings as well as numbers `ERROR`, `WARN`, `INFO`, `MSG`, `DIAG`, `DEBUG` or `0`, `1`, `2`, `3`, `4`, `5` respectively.
|
|
17
|
+
|
|
18
|
+
### Fixes
|
|
19
|
+
|
|
20
|
+
- `DebugAndLog`: Environment and Logging Level value checks are fixed (v1.3.2)
|
|
21
|
+
|
|
11
22
|
## 1.3.0 (2025-07-16)
|
|
12
23
|
|
|
13
24
|
### Enhancements
|
package/LICENSE.txt
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@63klabs/cache-data",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Cache data from an API endpoint or application process using AWS S3 and DynamoDb",
|
|
5
5
|
"author": "Chad Leigh Kluck (https://chadkluck.me)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@aws-sdk/client-s3": "3.x",
|
|
26
26
|
"@aws-sdk/client-ssm": "3.x",
|
|
27
27
|
"@aws-sdk/lib-dynamodb": "3.x",
|
|
28
|
-
"chai": "^
|
|
28
|
+
"chai": "^6.0.1",
|
|
29
29
|
"chai-http": "^5.1.2",
|
|
30
30
|
"mocha": "^11.7.1",
|
|
31
31
|
"sinon": "^21.0.0"
|
|
@@ -178,7 +178,7 @@ const _httpGetExecute = async function (options, requestObject, xRaySegment = xR
|
|
|
178
178
|
}
|
|
179
179
|
};
|
|
180
180
|
|
|
181
|
-
DebugAndLog.debug(`Response status ${res.statusCode}`, {status: res.statusCode, headers: res.headers});
|
|
181
|
+
DebugAndLog.debug(`Response status ${res.statusCode}`, {status: res.statusCode, method: requestObject.getMethod(), host: requestObject.getHost(), url: requestObject.getURI(false), headers: res.headers});
|
|
182
182
|
|
|
183
183
|
if (res.statusCode >= 500) {
|
|
184
184
|
xRaySegment.addFaultFlag();
|
|
@@ -585,8 +585,6 @@ class APIRequest {
|
|
|
585
585
|
|
|
586
586
|
const result = await _httpGetExecute(options, this, subsegment);
|
|
587
587
|
|
|
588
|
-
console.log("RESULT", result);
|
|
589
|
-
|
|
590
588
|
subsegment.addAnnotation('success', result ? "true" : "false");
|
|
591
589
|
subsegment.addAnnotation('status_code', this.#response?.statusCode || 500);
|
|
592
590
|
subsegment.addAnnotation('note', this.getNote());
|
|
@@ -1,153 +1,242 @@
|
|
|
1
1
|
const { sanitize } = require("./utils");
|
|
2
2
|
const util = require('util');
|
|
3
3
|
|
|
4
|
+
const _getNodeEnv = function() {
|
|
5
|
+
return process.env?.NODE_ENV === "development" ? "development" : "production";
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
"use strict"
|
|
9
|
+
|
|
4
10
|
/**
|
|
5
11
|
* A simple Debug and Logging class.
|
|
6
12
|
*/
|
|
7
13
|
class DebugAndLog {
|
|
8
14
|
|
|
9
15
|
static #logLevel = -1;
|
|
10
|
-
static #
|
|
16
|
+
static #environment = null;
|
|
17
|
+
static #initialNodeEnv = _getNodeEnv();
|
|
18
|
+
static #nodeEnvChangeWarnCount = 0;
|
|
19
|
+
|
|
20
|
+
// in priority order
|
|
21
|
+
static ALLOWED_ENV_TYPE_VAR_NAMES = ["CACHE_DATA_ENV", "DEPLOY_ENVIRONMENT", "DEPLOY_ENV", "ENV_TYPE", "deploy_environment", "ENV", "env", "deployEnvironment", "ENVIRONMENT", "environment"];
|
|
22
|
+
static ALLOWED_LOG_VAR_NAMES = ["CACHE_DATA_LOG_LEVEL", "LOG_LEVEL", "log_level", "detailedLogs", "logLevel", "AWS_LAMBDA_LOG_LEVEL"]
|
|
11
23
|
|
|
12
24
|
static PROD = "PROD";
|
|
13
25
|
static TEST = "TEST";
|
|
14
26
|
static DEV = "DEV";
|
|
15
27
|
|
|
16
|
-
static ENVIRONMENTS = [DebugAndLog.PROD, DebugAndLog.TEST, DebugAndLog.DEV];
|
|
28
|
+
static ENVIRONMENTS = ["PROD", "TEST", "DEV"];//[DebugAndLog.PROD, DebugAndLog.TEST, DebugAndLog.DEV];
|
|
29
|
+
|
|
30
|
+
static LOG = "LOG";
|
|
31
|
+
static ERROR = "ERROR";
|
|
32
|
+
static WARN = "WARN";
|
|
33
|
+
static INFO = "INFO";
|
|
34
|
+
static MSG = "MSG";
|
|
35
|
+
static DIAG = "DIAG";
|
|
36
|
+
static DEBUG = "DEBUG";
|
|
37
|
+
|
|
38
|
+
static ALLOWED_LOG_LEVEL_STRINGS = ["ERROR", "WARN", "INFO", "MSG", "DIAG", "DEBUG"];
|
|
17
39
|
|
|
18
|
-
static
|
|
19
|
-
static
|
|
20
|
-
static
|
|
21
|
-
static
|
|
22
|
-
static
|
|
23
|
-
static
|
|
40
|
+
static LOG_LEVEL_NUM = 0;
|
|
41
|
+
static ERROR_LEVEL_NUM = 0;
|
|
42
|
+
static WARN_LEVEL_NUM = 1;
|
|
43
|
+
static INFO_LEVEL_NUM = 2;
|
|
44
|
+
static MSG_LEVEL_NUM = 3;
|
|
45
|
+
static DIAG_LEVEL_NUM = 4;
|
|
46
|
+
static DEBUG_LEVEL_NUM = 5;
|
|
47
|
+
|
|
48
|
+
static PROD_DEFAULT_LEVEL_NUM = 2;
|
|
24
49
|
|
|
25
50
|
constructor() {
|
|
26
51
|
};
|
|
27
52
|
|
|
28
53
|
/**
|
|
29
54
|
* Set the log level.
|
|
55
|
+
* Deprecated. Set Lambda Environment variable CACHE_DATA_LOG_LEVEL, LOG_LEVEL, or AWS_LAMBDA_LOG_LEVEL instead.
|
|
30
56
|
* @param {number} logLevel 0 - 5
|
|
31
|
-
* @param {*} expiration
|
|
57
|
+
* @param {*} expiration Deprecated - no effect
|
|
32
58
|
*/
|
|
33
59
|
static setLogLevel(logLevel = -1, expiration = -1) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let time = new Date( expiration );
|
|
40
|
-
this.#expiration = time.toISOString();
|
|
41
|
-
} else {
|
|
42
|
-
this.#expiration = -1;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if ( logLevel === -1 || this.nonDefaultLogLevelExpired()) {
|
|
46
|
-
this.#logLevel = this.getDefaultLogLevel();
|
|
60
|
+
DebugAndLog.warn(`DebugAndLog.setLogLevel(${logLevel}, ${expiration}) is deprecated. Use CACHE_DATA_LOG_LEVEL, LOG_LEVEL, or AWS_LAMBDA_LOG_LEVEL environment variable instead.`);
|
|
61
|
+
|
|
62
|
+
if (DebugAndLog.isProduction()) {
|
|
63
|
+
if (Number.isFinite(Number(logLevel)) && Number(logLevel) <= DebugAndLog.PROD_DEFAULT_LEVEL_NUM) {
|
|
64
|
+
this.#logLevel = Number(logLevel);
|
|
47
65
|
} else {
|
|
48
|
-
|
|
49
|
-
DebugAndLog.warn("DebugAndLog: Production environment. Cannot set logLevel higher than 0. Ignoring call to DebugAndLog.setLogLevel("+logLevel+"). Default LogLevel override code should be removed before production");
|
|
50
|
-
this.#logLevel = this.getDefaultLogLevel();
|
|
51
|
-
} else {
|
|
52
|
-
this.#logLevel = logLevel;
|
|
53
|
-
DebugAndLog.msg("DebugAndLog: Override of log level default set: "+logLevel+". Default LogLevel override code should be removed before production");
|
|
54
|
-
if ( this.#expiration === -1 ) {
|
|
55
|
-
DebugAndLog.warn("DebugAndLog: Override of log level default set WITHOUT EXPIRATION. An expiration is recommended.");
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
static nonDefaultLogLevelExpired() {
|
|
64
|
-
let r = false;
|
|
65
|
-
|
|
66
|
-
if ( this.#expiration !== -1 ) {
|
|
67
|
-
let now = new Date();
|
|
68
|
-
if ( now.toISOString() > this.#expiration ) {
|
|
69
|
-
DebugAndLog.warn("DebugAndLog: Override of log level default expired. Call to DebugAndLog.setLogLevel() should be commented out or removed");
|
|
70
|
-
r = true;
|
|
66
|
+
this.#logLevel = DebugAndLog.PROD_DEFAULT_LEVEL_NUM;
|
|
71
67
|
}
|
|
68
|
+
} else if (Number.isFinite(Number(logLevel))) {
|
|
69
|
+
this.#logLevel = Number(logLevel);
|
|
72
70
|
}
|
|
73
71
|
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
*
|
|
79
|
-
* @returns {string} The expiration date of the set log level
|
|
80
|
-
*/
|
|
81
|
-
static getExpiration() {
|
|
82
|
-
return this.#expiration;
|
|
83
|
-
}
|
|
72
|
+
return this.#logLevel;
|
|
73
|
+
};
|
|
84
74
|
|
|
85
75
|
/**
|
|
86
76
|
*
|
|
87
77
|
* @returns {number} The current log level
|
|
88
78
|
*/
|
|
89
79
|
static getLogLevel() {
|
|
90
|
-
|
|
91
|
-
|
|
80
|
+
|
|
81
|
+
if ( this.#logLevel < 0 || DebugAndLog.nodeEnvIsDevelopment() || DebugAndLog.nodeEnvHasChanged() ) {
|
|
82
|
+
this.#logLevel = this.getDefaultLogLevel();
|
|
92
83
|
}
|
|
93
84
|
|
|
94
85
|
return this.#logLevel;
|
|
95
86
|
|
|
96
87
|
}
|
|
97
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Alias for getEnvType()
|
|
91
|
+
*/
|
|
92
|
+
static getEnv() {
|
|
93
|
+
return DebugAndLog.getEnvType();
|
|
94
|
+
};
|
|
95
|
+
|
|
98
96
|
/**
|
|
99
97
|
* Check process.env for an environment variable named
|
|
100
98
|
* env, deployEnvironment, environment, or stage. If they
|
|
101
99
|
* are not set it will return DebugAndLog.PROD which
|
|
102
100
|
* is considered safe (most restrictive)
|
|
103
101
|
* Note: This is the application environment, not the NODE_ENV
|
|
104
|
-
* @returns {string} The current environment.
|
|
102
|
+
* @returns {string} PROD|TEST|DEV - The current environment.
|
|
105
103
|
*/
|
|
106
|
-
static
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
let uE = possibleVars[i].toUpperCase();
|
|
114
|
-
if (e in process.env && process.env[e] !== "" && process.env[e] !== null) {
|
|
115
|
-
env = process.env[e].toUpperCase();
|
|
116
|
-
break; // break out of the for loop
|
|
117
|
-
} else if (uE in process.env && process.env[uE] !== "" && process.env[uE] !== null) {
|
|
118
|
-
env = process.env[uE].toUpperCase();
|
|
119
|
-
break; // break out of the for loop
|
|
120
|
-
}
|
|
121
|
-
};
|
|
104
|
+
static getEnvType() {
|
|
105
|
+
// We can switch if NODE_ENV is set to "development"
|
|
106
|
+
if ( this.#environment === null || DebugAndLog.nodeEnvIsDevelopment() || DebugAndLog.nodeEnvHasChanged) {
|
|
107
|
+
const nodeEnvType = (DebugAndLog.nodeEnvIsDevelopment() ? DebugAndLog.DEV : DebugAndLog.PROD); // if env or deployEnvironment not set, fail to safe
|
|
108
|
+
const envType = DebugAndLog.getEnvTypeFromEnvVar();
|
|
109
|
+
|
|
110
|
+
this.#environment = (DebugAndLog.ENVIRONMENTS.includes(envType) ? envType : nodeEnvType);
|
|
122
111
|
}
|
|
123
|
-
|
|
124
|
-
|
|
112
|
+
|
|
113
|
+
return this.#environment;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
*
|
|
117
|
+
* @returns {string} PROD|TEST|DEV|NONE based upon first environment variable from DebugAndLog.ALLOWED_ENV_TYPE_VAR_NAMES found
|
|
118
|
+
*/
|
|
119
|
+
static getEnvTypeFromEnvVar() {
|
|
120
|
+
let environmentType = "none";
|
|
121
|
+
const possibleVars = DebugAndLog.ALLOWED_ENV_TYPE_VAR_NAMES; // - this is the application env, not the NODE_ENV
|
|
122
|
+
|
|
123
|
+
for (let i in possibleVars) {
|
|
124
|
+
let e = possibleVars[i];
|
|
125
|
+
if (e in process.env && process.env[e] !== "" && process.env[e] !== null && DebugAndLog.ENVIRONMENTS.includes(process.env[e].toUpperCase())) {
|
|
126
|
+
environmentType = process.env[e].toUpperCase();
|
|
127
|
+
break; // break out of the for loop
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
return environmentType;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Is Environment Variable NODE_ENV set to "development"?
|
|
136
|
+
*/
|
|
137
|
+
static nodeEnvIsDevelopment() {
|
|
138
|
+
return DebugAndLog.getNodeEnv() === "development";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Is Environment Variable NODE_ENV set to "production" or "" or undefined?
|
|
143
|
+
*/
|
|
144
|
+
static nodeEnvIsProduction() {
|
|
145
|
+
return !this.nodeEnvIsDevelopment();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Get the current NODE_ENV (returns "production" if not set or if NODE_ENV is set to anything other than "development")
|
|
150
|
+
* Calls DebugAndLog.nodeEnvHasChanged() to log a warning if the value has changed since initialization. Should only change during testing.
|
|
151
|
+
* @returns {string} development|production
|
|
152
|
+
*/
|
|
153
|
+
static getNodeEnv() {
|
|
154
|
+
DebugAndLog.nodeEnvHasChanged();
|
|
155
|
+
return _getNodeEnv();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Checks to see if the current NODE_ENV environment variable has changed since DebugAndLog was initialized.
|
|
160
|
+
* The only time this should happen is while running tests. This should never happen in a production application.
|
|
161
|
+
* If these warnings are triggered as you application is running, something is modifying process.env.NODE_ENV during execution.
|
|
162
|
+
* @returns {boolean}
|
|
163
|
+
*/
|
|
164
|
+
static nodeEnvHasChanged() {
|
|
165
|
+
const hasChanged = _getNodeEnv() !== this.#initialNodeEnv;
|
|
166
|
+
if (hasChanged && this.#logLevel > -1) {
|
|
167
|
+
this.#nodeEnvChangeWarnCount++;
|
|
168
|
+
// if this.#nodeEnvChangeWarnCount == 1 or is divisible by 25
|
|
169
|
+
if (this.#nodeEnvChangeWarnCount === 1 || this.#nodeEnvChangeWarnCount % 100 === 0) {
|
|
170
|
+
DebugAndLog.warn(`DebugAndLog: NODE_ENV changed from initial value of '${this.#initialNodeEnv}' to '${_getNodeEnv()}' during execution. This is not recommended outside of tests.`);
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
this.#nodeEnvChangeWarnCount = 0;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return hasChanged;
|
|
177
|
+
}
|
|
125
178
|
|
|
126
179
|
/**
|
|
127
180
|
*
|
|
128
181
|
* @returns {number} log level
|
|
129
182
|
*/
|
|
130
183
|
static getDefaultLogLevel() {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
184
|
+
let possibleVars = DebugAndLog.ALLOWED_LOG_VAR_NAMES; // in priority order and we want to evaluate AWS_LAMBDA_LOG_LEVEL as upper
|
|
185
|
+
let logLevel = DebugAndLog.PROD_DEFAULT_LEVEL_NUM;
|
|
186
|
+
let found = false;
|
|
187
|
+
|
|
188
|
+
for (let i in possibleVars) {
|
|
189
|
+
let lev = possibleVars[i];
|
|
190
|
+
if (lev in process.env && process.env[lev] !== "" && process.env[lev] !== null) {
|
|
191
|
+
if (lev === "AWS_LAMBDA_LOG_LEVEL") {
|
|
192
|
+
|
|
193
|
+
switch (process.env.AWS_LAMBDA_LOG_LEVEL) {
|
|
194
|
+
case "DEBUG":
|
|
195
|
+
logLevel = DebugAndLog.DEBUG_LEVEL_NUM;
|
|
196
|
+
found = true;
|
|
197
|
+
break;
|
|
198
|
+
case "INFO":
|
|
199
|
+
logLevel = DebugAndLog.INFO_LEVEL_NUM;
|
|
200
|
+
found = true;
|
|
201
|
+
break;
|
|
202
|
+
case "WARN":
|
|
203
|
+
logLevel = DebugAndLog.WARN_LEVEL_NUM;
|
|
204
|
+
found = true;
|
|
205
|
+
break;
|
|
206
|
+
case "ERROR":
|
|
207
|
+
logLevel = DebugAndLog.ERROR_LEVEL_NUM;
|
|
208
|
+
found = true;
|
|
209
|
+
break;
|
|
210
|
+
case "CRITICAL":
|
|
211
|
+
logLevel = DebugAndLog.ERROR_LEVEL_NUM; // This is lowest we go and will let Lambda filter out
|
|
212
|
+
found = true;
|
|
213
|
+
break;
|
|
214
|
+
case "SILENT":
|
|
215
|
+
logLevel = DebugAndLog.ERROR_LEVEL_NUM; // This is lowest we go and will let Lambda filter out
|
|
216
|
+
found = true;
|
|
217
|
+
break;
|
|
218
|
+
default: // invalid
|
|
219
|
+
break;
|
|
146
220
|
}
|
|
147
|
-
}
|
|
148
|
-
|
|
221
|
+
} else if (typeof process.env[lev] === "string" && DebugAndLog.ALLOWED_LOG_LEVEL_STRINGS.includes(process.env[lev].toUpperCase())) {
|
|
222
|
+
logLevel = DebugAndLog[process.env[lev].toUpperCase().concat("_LEVEL_NUM")];
|
|
223
|
+
found = true;
|
|
224
|
+
} else if (Number.isFinite(Number(process.env[lev]))) {
|
|
225
|
+
logLevel = Number(process.env[lev]);
|
|
226
|
+
found = true;
|
|
227
|
+
}
|
|
149
228
|
|
|
150
|
-
|
|
229
|
+
if (found) {
|
|
230
|
+
if (DebugAndLog.isProduction()) {
|
|
231
|
+
if (logLevel > DebugAndLog.PROD_DEFAULT_LEVEL_NUM) {
|
|
232
|
+
DebugAndLog.warn(`DebugAndLog: ${lev} set to ${logLevel} in production environment. Only 0-2 is allowed in production. Setting to ${DebugAndLog.PROD_DEFAULT_LEVEL_NUM}`);
|
|
233
|
+
logLevel = DebugAndLog.PROD_DEFAULT_LEVEL_NUM;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
break; // break out of the for loop
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
};
|
|
151
240
|
|
|
152
241
|
return logLevel;
|
|
153
242
|
};
|
|
@@ -205,44 +294,6 @@ class DebugAndLog {
|
|
|
205
294
|
const FORMAT_WITH_OBJ = '[%s] %s | %s';
|
|
206
295
|
const FORMAT_WITHOUT_OBJ = '[%s] %s';
|
|
207
296
|
|
|
208
|
-
// const baseLog = function(level, tag, message, obj = null) {
|
|
209
|
-
// // Validate inputs
|
|
210
|
-
// if (typeof level !== 'string') {
|
|
211
|
-
// throw new TypeError('Log level must be a string');
|
|
212
|
-
// }
|
|
213
|
-
|
|
214
|
-
// // Ensure tag and message are strings
|
|
215
|
-
// const safeTag = String(tag || '');
|
|
216
|
-
// const safeMessage = String(message || '');
|
|
217
|
-
|
|
218
|
-
// // Validate log level is allowed
|
|
219
|
-
// if (!Object.prototype.hasOwnProperty.call(logLevels, level)) {
|
|
220
|
-
// level = 'info'; // Default to info if invalid level
|
|
221
|
-
// }
|
|
222
|
-
|
|
223
|
-
// const logFn = logLevels[level];
|
|
224
|
-
|
|
225
|
-
// try {
|
|
226
|
-
// let formattedMessage;
|
|
227
|
-
// if (obj !== null) {
|
|
228
|
-
// formattedMessage = util.format(
|
|
229
|
-
// '[%s] %s | %s',
|
|
230
|
-
// safeTag,
|
|
231
|
-
// safeMessage,
|
|
232
|
-
// util.inspect(sanitize(obj), { depth: null })
|
|
233
|
-
// );
|
|
234
|
-
// } else {
|
|
235
|
-
// formattedMessage = util.format(
|
|
236
|
-
// '[%s] %s',
|
|
237
|
-
// safeTag,
|
|
238
|
-
// safeMessage
|
|
239
|
-
// );
|
|
240
|
-
// }
|
|
241
|
-
// logFn(formattedMessage);
|
|
242
|
-
// } catch (error) {
|
|
243
|
-
// console.error('Logging failed:', error);
|
|
244
|
-
// }
|
|
245
|
-
// };
|
|
246
297
|
const baseLog = function(level, tag, message, obj = null) {
|
|
247
298
|
// Early return for invalid input
|
|
248
299
|
if (typeof level !== 'string') {
|
|
@@ -288,33 +339,29 @@ class DebugAndLog {
|
|
|
288
339
|
const info = (tag, message, obj) => baseLog('info', tag, message, obj);
|
|
289
340
|
const debug = (tag, message, obj) => baseLog('debug', tag, message, obj);
|
|
290
341
|
|
|
291
|
-
let lvl = DebugAndLog.getLogLevel();
|
|
292
|
-
|
|
342
|
+
// let lvl = (this.#logLevel > -1) ? DebugAndLog.getLogLevel() : DebugAndLog.MSG_LEVEL_NUM;
|
|
343
|
+
let lvl = (this.#logLevel > -1) ? this.#logLevel : DebugAndLog.INFO_LEVEL_NUM;
|
|
293
344
|
|
|
294
|
-
|
|
295
|
-
// let msgObj = obj;
|
|
296
|
-
// if ( Array.isArray(msgObj)) { msgObj = { array: msgObj};}
|
|
297
|
-
// if ( ""+msgObj === "[object Object]" || ""+msgObj === "[object Array]") {
|
|
298
|
-
// msgObj = JSON.stringify(sanitize(msgObj));
|
|
299
|
-
// }
|
|
300
|
-
// message += " | "+msgObj;
|
|
301
|
-
// }
|
|
345
|
+
tag = tag.toUpperCase();
|
|
302
346
|
|
|
303
347
|
switch (tag) {
|
|
304
348
|
case DebugAndLog.ERROR:
|
|
305
349
|
error(tag, message, obj);
|
|
306
350
|
break;
|
|
307
351
|
case DebugAndLog.WARN:
|
|
308
|
-
warn(tag, message, obj);
|
|
352
|
+
if (lvl >= DebugAndLog.WARN_LEVEL_NUM) { warn(tag, message, obj); }
|
|
309
353
|
break;
|
|
354
|
+
case DebugAndLog.INFO:
|
|
355
|
+
if (lvl >= DebugAndLog.INFO_LEVEL_NUM) { info(tag, message, obj); }
|
|
356
|
+
break;
|
|
310
357
|
case DebugAndLog.MSG:
|
|
311
|
-
if (lvl >=
|
|
358
|
+
if (lvl >= DebugAndLog.MSG_LEVEL_NUM) { info(tag, message, obj); }
|
|
312
359
|
break;
|
|
313
360
|
case DebugAndLog.DIAG:
|
|
314
|
-
if (lvl >=
|
|
361
|
+
if (lvl >= DebugAndLog.DIAG_LEVEL_NUM) { debug(tag, message, obj); }
|
|
315
362
|
break;
|
|
316
363
|
case DebugAndLog.DEBUG:
|
|
317
|
-
if (lvl >=
|
|
364
|
+
if (lvl >= DebugAndLog.DEBUG_LEVEL_NUM) { debug(tag, message, obj); }
|
|
318
365
|
break;
|
|
319
366
|
default: // log
|
|
320
367
|
log(tag, message, obj);
|
|
@@ -343,7 +390,7 @@ class DebugAndLog {
|
|
|
343
390
|
};
|
|
344
391
|
|
|
345
392
|
/**
|
|
346
|
-
* Level
|
|
393
|
+
* Level 2 - Short messages and status
|
|
347
394
|
* @param {string} message
|
|
348
395
|
* @param {object} obj
|
|
349
396
|
*/
|
|
@@ -352,7 +399,7 @@ class DebugAndLog {
|
|
|
352
399
|
};
|
|
353
400
|
|
|
354
401
|
/**
|
|
355
|
-
* Level
|
|
402
|
+
* Level 2 - Short messages and status
|
|
356
403
|
* (same as DebugAndLog.msg() )
|
|
357
404
|
* @param {string} message
|
|
358
405
|
* @param {object} obj
|
|
@@ -361,6 +408,15 @@ class DebugAndLog {
|
|
|
361
408
|
return DebugAndLog.msg(message, obj);
|
|
362
409
|
};
|
|
363
410
|
|
|
411
|
+
/**
|
|
412
|
+
* Level 1 - Short messages and status
|
|
413
|
+
* @param {string} message
|
|
414
|
+
* @param {object} obj
|
|
415
|
+
*/
|
|
416
|
+
static async info(message, obj = null) {
|
|
417
|
+
return DebugAndLog.writeLog(DebugAndLog.INFO, message, obj);
|
|
418
|
+
};
|
|
419
|
+
|
|
364
420
|
/**
|
|
365
421
|
* Level 0 - Production worthy log entries that are not errors or warnings
|
|
366
422
|
* These should be formatted in a consistent manner and typically only
|
|
@@ -413,4 +469,6 @@ class DebugAndLog {
|
|
|
413
469
|
|
|
414
470
|
};
|
|
415
471
|
|
|
472
|
+
DebugAndLog.getLogLevel();
|
|
473
|
+
|
|
416
474
|
module.exports = DebugAndLog;
|