infront-logger 1.1.11 → 1.1.13
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 +36 -2
- package/dist/browser.es.js +11 -24
- package/dist/browser.umd.js +11 -24
- package/dist/index.es.js +73 -29
- package/dist/index.umd.js +72 -28
- package/dist/main.d.ts +16 -2
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Infront Logger
|
|
2
2
|
## Logging done right
|
|
3
3
|
|
|
4
|
-
The logger supports both
|
|
4
|
+
The logger supports both writing to file and to console.
|
|
5
5
|
|
|
6
6
|
npm i @infront/logger
|
|
7
7
|
|
|
@@ -13,6 +13,21 @@ The logger supports both writhing to file and to console.
|
|
|
13
13
|
let logger = new BaseLogger("component1", options);
|
|
14
14
|
logger.log("this is a test message");
|
|
15
15
|
|
|
16
|
+
### StaticLogger
|
|
17
|
+
const {staticLogger} = require('infront-logger');
|
|
18
|
+
|
|
19
|
+
// Simple logging
|
|
20
|
+
staticLogger.info("User logged in");
|
|
21
|
+
|
|
22
|
+
// Logging with context
|
|
23
|
+
staticLogger.info("User action", { userId: 123, action: "login" });
|
|
24
|
+
|
|
25
|
+
// Different log levels
|
|
26
|
+
staticLogger.debug("Debug information");
|
|
27
|
+
staticLogger.info("Information message");
|
|
28
|
+
staticLogger.warn("Warning message");
|
|
29
|
+
staticLogger.error("Error occurred", { error: "Details here" });
|
|
30
|
+
|
|
16
31
|
### HttpLogger (Express Logger)
|
|
17
32
|
const {HttpLogger} = require('infront-logger');
|
|
18
33
|
|
|
@@ -130,9 +145,22 @@ Make sure to use before defining schemas;
|
|
|
130
145
|
- session(id)
|
|
131
146
|
- log(msg)
|
|
132
147
|
- info(msg)
|
|
148
|
+
- warn(msg)
|
|
133
149
|
- error(err)
|
|
150
|
+
- debug(msg)
|
|
134
151
|
- profile(action, options)
|
|
135
152
|
- profileMem(options)
|
|
153
|
+
- exclude(pattern) - Exclude messages matching string or regex pattern
|
|
154
|
+
|
|
155
|
+
### StaticLogger (extends BaseLogger)
|
|
156
|
+
#### Methods
|
|
157
|
+
- log(msg, context?) - Logs at info level
|
|
158
|
+
- info(msg, context?)
|
|
159
|
+
- warn(msg, context?)
|
|
160
|
+
- error(msg, context?)
|
|
161
|
+
- debug(msg, context?)
|
|
162
|
+
|
|
163
|
+
All methods support both string messages and context objects. Context objects are automatically merged into the log context.
|
|
136
164
|
|
|
137
165
|
### HttpLogger (extends BaseLogger)
|
|
138
166
|
#### Methods
|
|
@@ -142,6 +170,9 @@ Make sure to use before defining schemas;
|
|
|
142
170
|
- success(req, res, body)
|
|
143
171
|
- error(err)
|
|
144
172
|
|
|
173
|
+
#### Options
|
|
174
|
+
- maxBodyLength - -1 = unlimited, default: 128
|
|
175
|
+
|
|
145
176
|
### FastifyLogger (extends BaseLogger)
|
|
146
177
|
#### Methods
|
|
147
178
|
- request(req)
|
|
@@ -150,7 +181,10 @@ Make sure to use before defining schemas;
|
|
|
150
181
|
- success(req, res, body)
|
|
151
182
|
- error(err)
|
|
152
183
|
|
|
153
|
-
|
|
184
|
+
#### Options
|
|
185
|
+
- maxBodyLength - -1 = unlimited, default: 128
|
|
186
|
+
|
|
187
|
+
## Options
|
|
154
188
|
|
|
155
189
|
- dirname - The directory in which the logs will be created (default: "logs")
|
|
156
190
|
- levels - An object of "level name : hierarchical value"
|
package/dist/browser.es.js
CHANGED
|
@@ -193,14 +193,12 @@ function getZoneJsOriginalValue(target, name) {
|
|
|
193
193
|
return original;
|
|
194
194
|
}
|
|
195
195
|
var __spreadArray = function(to, from, pack) {
|
|
196
|
-
if (pack || arguments.length === 2)
|
|
197
|
-
|
|
198
|
-
if (ar
|
|
199
|
-
|
|
200
|
-
ar = Array.prototype.slice.call(from, 0, i);
|
|
201
|
-
ar[i] = from[i];
|
|
202
|
-
}
|
|
196
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
197
|
+
if (ar || !(i in from)) {
|
|
198
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
199
|
+
ar[i] = from[i];
|
|
203
200
|
}
|
|
201
|
+
}
|
|
204
202
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
205
203
|
};
|
|
206
204
|
var onMonitorErrorCollected;
|
|
@@ -313,8 +311,6 @@ function mergeObservables() {
|
|
|
313
311
|
});
|
|
314
312
|
}
|
|
315
313
|
function throttle(fn, wait, options) {
|
|
316
|
-
var needLeadingExecution = options && options.leading !== void 0 ? options.leading : true;
|
|
317
|
-
var needTrailingExecution = options && options.trailing !== void 0 ? options.trailing : true;
|
|
318
314
|
var inWaitPeriod = false;
|
|
319
315
|
var pendingExecutionWithParameters;
|
|
320
316
|
var pendingTimeoutId;
|
|
@@ -328,14 +324,12 @@ function throttle(fn, wait, options) {
|
|
|
328
324
|
pendingExecutionWithParameters = parameters;
|
|
329
325
|
return;
|
|
330
326
|
}
|
|
331
|
-
|
|
327
|
+
{
|
|
332
328
|
fn.apply(void 0, parameters);
|
|
333
|
-
} else {
|
|
334
|
-
pendingExecutionWithParameters = parameters;
|
|
335
329
|
}
|
|
336
330
|
inWaitPeriod = true;
|
|
337
331
|
pendingTimeoutId = setTimeout(function() {
|
|
338
|
-
if (
|
|
332
|
+
if (pendingExecutionWithParameters) {
|
|
339
333
|
fn.apply(void 0, pendingExecutionWithParameters);
|
|
340
334
|
}
|
|
341
335
|
inWaitPeriod = false;
|
|
@@ -385,9 +379,6 @@ function findCommaSeparatedValues(rawString) {
|
|
|
385
379
|
return result;
|
|
386
380
|
}
|
|
387
381
|
function safeTruncate(candidate, length, suffix) {
|
|
388
|
-
if (suffix === void 0) {
|
|
389
|
-
suffix = "";
|
|
390
|
-
}
|
|
391
382
|
var lastChar = candidate.charCodeAt(length - 1);
|
|
392
383
|
var isLastCharSurrogatePair = lastChar >= 55296 && lastChar <= 56319;
|
|
393
384
|
var correctedLength = isLastCharSurrogatePair ? length + 1 : length;
|
|
@@ -3192,7 +3183,7 @@ function readBytesFromStream(stream, callback, options) {
|
|
|
3192
3183
|
onDone();
|
|
3193
3184
|
return;
|
|
3194
3185
|
}
|
|
3195
|
-
|
|
3186
|
+
{
|
|
3196
3187
|
chunks.push(result.value);
|
|
3197
3188
|
}
|
|
3198
3189
|
readBytesCount += result.value.length;
|
|
@@ -3213,7 +3204,7 @@ function readBytesFromStream(stream, callback, options) {
|
|
|
3213
3204
|
);
|
|
3214
3205
|
var bytes;
|
|
3215
3206
|
var limitExceeded;
|
|
3216
|
-
|
|
3207
|
+
{
|
|
3217
3208
|
var completeBuffer_1;
|
|
3218
3209
|
if (chunks.length === 1) {
|
|
3219
3210
|
completeBuffer_1 = chunks[0];
|
|
@@ -3282,12 +3273,8 @@ var StatusType = {
|
|
|
3282
3273
|
var STATUS_PRIORITIES = (_a$3 = {}, _a$3[StatusType.ok] = 0, _a$3[StatusType.debug] = 1, _a$3[StatusType.info] = 2, _a$3[StatusType.notice] = 4, _a$3[StatusType.warn] = 5, _a$3[StatusType.error] = 6, _a$3[StatusType.critical] = 7, _a$3[StatusType.alert] = 8, _a$3[StatusType.emerg] = 9, _a$3);
|
|
3283
3274
|
var __decorate = function(decorators, target, key, desc) {
|
|
3284
3275
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3285
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
3286
|
-
|
|
3287
|
-
else
|
|
3288
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
3289
|
-
if (d = decorators[i])
|
|
3290
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
3276
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
3277
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
3291
3278
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
3292
3279
|
};
|
|
3293
3280
|
var HandlerType = {
|
package/dist/browser.umd.js
CHANGED
|
@@ -197,14 +197,12 @@
|
|
|
197
197
|
return original;
|
|
198
198
|
}
|
|
199
199
|
var __spreadArray = function(to, from, pack) {
|
|
200
|
-
if (pack || arguments.length === 2)
|
|
201
|
-
|
|
202
|
-
if (ar
|
|
203
|
-
|
|
204
|
-
ar = Array.prototype.slice.call(from, 0, i);
|
|
205
|
-
ar[i] = from[i];
|
|
206
|
-
}
|
|
200
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
201
|
+
if (ar || !(i in from)) {
|
|
202
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
203
|
+
ar[i] = from[i];
|
|
207
204
|
}
|
|
205
|
+
}
|
|
208
206
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
209
207
|
};
|
|
210
208
|
var onMonitorErrorCollected;
|
|
@@ -317,8 +315,6 @@
|
|
|
317
315
|
});
|
|
318
316
|
}
|
|
319
317
|
function throttle(fn, wait, options) {
|
|
320
|
-
var needLeadingExecution = options && options.leading !== void 0 ? options.leading : true;
|
|
321
|
-
var needTrailingExecution = options && options.trailing !== void 0 ? options.trailing : true;
|
|
322
318
|
var inWaitPeriod = false;
|
|
323
319
|
var pendingExecutionWithParameters;
|
|
324
320
|
var pendingTimeoutId;
|
|
@@ -332,14 +328,12 @@
|
|
|
332
328
|
pendingExecutionWithParameters = parameters;
|
|
333
329
|
return;
|
|
334
330
|
}
|
|
335
|
-
|
|
331
|
+
{
|
|
336
332
|
fn.apply(void 0, parameters);
|
|
337
|
-
} else {
|
|
338
|
-
pendingExecutionWithParameters = parameters;
|
|
339
333
|
}
|
|
340
334
|
inWaitPeriod = true;
|
|
341
335
|
pendingTimeoutId = setTimeout(function() {
|
|
342
|
-
if (
|
|
336
|
+
if (pendingExecutionWithParameters) {
|
|
343
337
|
fn.apply(void 0, pendingExecutionWithParameters);
|
|
344
338
|
}
|
|
345
339
|
inWaitPeriod = false;
|
|
@@ -389,9 +383,6 @@
|
|
|
389
383
|
return result;
|
|
390
384
|
}
|
|
391
385
|
function safeTruncate(candidate, length, suffix) {
|
|
392
|
-
if (suffix === void 0) {
|
|
393
|
-
suffix = "";
|
|
394
|
-
}
|
|
395
386
|
var lastChar = candidate.charCodeAt(length - 1);
|
|
396
387
|
var isLastCharSurrogatePair = lastChar >= 55296 && lastChar <= 56319;
|
|
397
388
|
var correctedLength = isLastCharSurrogatePair ? length + 1 : length;
|
|
@@ -3196,7 +3187,7 @@
|
|
|
3196
3187
|
onDone();
|
|
3197
3188
|
return;
|
|
3198
3189
|
}
|
|
3199
|
-
|
|
3190
|
+
{
|
|
3200
3191
|
chunks.push(result.value);
|
|
3201
3192
|
}
|
|
3202
3193
|
readBytesCount += result.value.length;
|
|
@@ -3217,7 +3208,7 @@
|
|
|
3217
3208
|
);
|
|
3218
3209
|
var bytes;
|
|
3219
3210
|
var limitExceeded;
|
|
3220
|
-
|
|
3211
|
+
{
|
|
3221
3212
|
var completeBuffer_1;
|
|
3222
3213
|
if (chunks.length === 1) {
|
|
3223
3214
|
completeBuffer_1 = chunks[0];
|
|
@@ -3286,12 +3277,8 @@
|
|
|
3286
3277
|
var STATUS_PRIORITIES = (_a$3 = {}, _a$3[StatusType.ok] = 0, _a$3[StatusType.debug] = 1, _a$3[StatusType.info] = 2, _a$3[StatusType.notice] = 4, _a$3[StatusType.warn] = 5, _a$3[StatusType.error] = 6, _a$3[StatusType.critical] = 7, _a$3[StatusType.alert] = 8, _a$3[StatusType.emerg] = 9, _a$3);
|
|
3287
3278
|
var __decorate = function(decorators, target, key, desc) {
|
|
3288
3279
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3289
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
3290
|
-
|
|
3291
|
-
else
|
|
3292
|
-
for (var i = decorators.length - 1; i >= 0; i--)
|
|
3293
|
-
if (d = decorators[i])
|
|
3294
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
3280
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
3281
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
3295
3282
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
3296
3283
|
};
|
|
3297
3284
|
var HandlerType = {
|
package/dist/index.es.js
CHANGED
|
@@ -102,10 +102,8 @@ function createTransport(options) {
|
|
|
102
102
|
class Logger {
|
|
103
103
|
constructor(options = {}) {
|
|
104
104
|
this.options = { ...OPTIONS$1, ...options };
|
|
105
|
-
if (!this.options.filename.includes("."))
|
|
106
|
-
|
|
107
|
-
if (!this.options.errorFilename.includes("."))
|
|
108
|
-
this.options.errorFilename += ".log";
|
|
105
|
+
if (!this.options.filename.includes(".")) this.options.filename += ".log";
|
|
106
|
+
if (!this.options.errorFilename.includes(".")) this.options.errorFilename += ".log";
|
|
109
107
|
let trans = [];
|
|
110
108
|
let exceptionHandlers = [new transports.Console({
|
|
111
109
|
format: getFormatter("console", this.options)
|
|
@@ -246,20 +244,52 @@ class BaseLogger {
|
|
|
246
244
|
this.ctx = {};
|
|
247
245
|
this.startTime = Date.now();
|
|
248
246
|
this.absaluteStartTime = Date.now();
|
|
247
|
+
this.excludePatterns = [];
|
|
249
248
|
}
|
|
250
249
|
session(id) {
|
|
251
250
|
this.ctx.sessionID = id;
|
|
252
251
|
return this;
|
|
253
252
|
}
|
|
253
|
+
exclude(pattern) {
|
|
254
|
+
if (pattern instanceof RegExp) {
|
|
255
|
+
this.excludePatterns.push({ type: "regex", pattern });
|
|
256
|
+
} else if (typeof pattern === "string") {
|
|
257
|
+
this.excludePatterns.push({ type: "string", pattern });
|
|
258
|
+
} else if (Array.isArray(pattern)) {
|
|
259
|
+
pattern.forEach((p) => this.exclude(p));
|
|
260
|
+
}
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
_shouldExclude(message) {
|
|
264
|
+
if (!this.excludePatterns.length) return false;
|
|
265
|
+
const stringMessage = typeof message === "object" ? JSON.stringify(message) : String(message);
|
|
266
|
+
return this.excludePatterns.some(({ type, pattern }) => {
|
|
267
|
+
if (type === "regex") {
|
|
268
|
+
return pattern.test(stringMessage);
|
|
269
|
+
} else if (type === "string") {
|
|
270
|
+
return stringMessage.includes(pattern);
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
254
275
|
log() {
|
|
276
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
255
279
|
this.logger.info(...arguments, this.ctx);
|
|
256
280
|
this._stopMemProfile();
|
|
257
281
|
}
|
|
258
282
|
info() {
|
|
283
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
259
286
|
this.logger.info(...arguments, this.ctx);
|
|
260
287
|
this._stopMemProfile();
|
|
261
288
|
}
|
|
262
289
|
error() {
|
|
290
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
263
293
|
this.logger.error(...arguments, this.ctx);
|
|
264
294
|
this._stopMemProfile();
|
|
265
295
|
}
|
|
@@ -270,13 +300,11 @@ class BaseLogger {
|
|
|
270
300
|
this.ctx.profiler[action] = Date.now() - startTime;
|
|
271
301
|
}
|
|
272
302
|
this.ctx.profiler.totalTime = Date.now() - this.absaluteStartTime;
|
|
273
|
-
if (!options.continue)
|
|
274
|
-
this.startTime = Date.now();
|
|
303
|
+
if (!options.continue) this.startTime = Date.now();
|
|
275
304
|
return this;
|
|
276
305
|
}
|
|
277
306
|
_stopMemProfile() {
|
|
278
|
-
if (this.memProfileInterval)
|
|
279
|
-
clearInterval(this.memProfileInterval);
|
|
307
|
+
if (this.memProfileInterval) clearInterval(this.memProfileInterval);
|
|
280
308
|
}
|
|
281
309
|
profileMem(options = {}) {
|
|
282
310
|
this.ctx.maxMemory = this.ctx.maxMemory || 0;
|
|
@@ -295,9 +323,12 @@ class BaseLogger {
|
|
|
295
323
|
return this;
|
|
296
324
|
}
|
|
297
325
|
context(key, value) {
|
|
298
|
-
if (
|
|
326
|
+
if (key === null || key === void 0) {
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
if (typeof key === "string" && arguments.length === 2) {
|
|
299
330
|
this.ctx[key] = value;
|
|
300
|
-
} else if (typeof key === "object") {
|
|
331
|
+
} else if (typeof key === "object" && !Array.isArray(key)) {
|
|
301
332
|
Object.assign(this.ctx, key);
|
|
302
333
|
}
|
|
303
334
|
return this;
|
|
@@ -344,7 +375,7 @@ class HTTPLogger extends BaseLogger {
|
|
|
344
375
|
this.ctx.response = {
|
|
345
376
|
headers: omit$1(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
346
377
|
statusCode: res.statusCode,
|
|
347
|
-
body: trim$1(body, MAX_BODY_LENGTH$1)
|
|
378
|
+
body: this.options.maxBodyLength <= 0 ? body : trim$1(body, this.options.maxBodyLength || MAX_BODY_LENGTH$1)
|
|
348
379
|
};
|
|
349
380
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
350
381
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -362,12 +393,9 @@ class HTTPLogger extends BaseLogger {
|
|
|
362
393
|
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
363
394
|
}
|
|
364
395
|
success(req, res, body) {
|
|
365
|
-
if (req)
|
|
366
|
-
|
|
367
|
-
if (
|
|
368
|
-
this.response(res);
|
|
369
|
-
if (body)
|
|
370
|
-
this.body(body);
|
|
396
|
+
if (req) this.request(req);
|
|
397
|
+
if (res) this.response(res);
|
|
398
|
+
if (body) this.body(body);
|
|
371
399
|
this._prepare();
|
|
372
400
|
super.info(this._message());
|
|
373
401
|
}
|
|
@@ -437,8 +465,7 @@ class MongooseLogger extends BaseLogger {
|
|
|
437
465
|
this.ctx.resultLength = Array.isArray(res) ? res.length : res ? 1 : 0;
|
|
438
466
|
} catch (err) {
|
|
439
467
|
}
|
|
440
|
-
if (!this.options.logRes)
|
|
441
|
-
return this;
|
|
468
|
+
if (!this.options.logRes) return this;
|
|
442
469
|
if (Array.isArray(res)) {
|
|
443
470
|
this.ctx.documentCount = res.length;
|
|
444
471
|
} else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
|
|
@@ -451,8 +478,7 @@ class MongooseLogger extends BaseLogger {
|
|
|
451
478
|
}
|
|
452
479
|
async function postHook(target, res) {
|
|
453
480
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
454
|
-
if ((_a = target == null ? void 0 : target.options) == null ? void 0 : _a.explain)
|
|
455
|
-
return;
|
|
481
|
+
if ((_a = target == null ? void 0 : target.options) == null ? void 0 : _a.explain) return;
|
|
456
482
|
const op = target.constructor.name === "Aggregate" ? "aggregate" : target.op || target.$op;
|
|
457
483
|
const collection = ((_b = target == null ? void 0 : target._collection) == null ? void 0 : _b.collectionName) || ((_c = target == null ? void 0 : target.collection) == null ? void 0 : _c.name) || ((_e = (_d = target == null ? void 0 : target._model) == null ? void 0 : _d.collection) == null ? void 0 : _e.collectionName);
|
|
458
484
|
if (!op || !collection) {
|
|
@@ -561,7 +587,7 @@ class FastifyLogger extends BaseLogger {
|
|
|
561
587
|
this.ctx.response = {
|
|
562
588
|
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
563
589
|
statusCode: this.getResponseStatusCode(res),
|
|
564
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
590
|
+
body: this.options.maxBodyLength <= 0 ? body : trim(body, this.options.maxBodyLength || MAX_BODY_LENGTH)
|
|
565
591
|
};
|
|
566
592
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
567
593
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -586,12 +612,9 @@ class FastifyLogger extends BaseLogger {
|
|
|
586
612
|
return this.req.ip || this.req._remoteAddress || void 0;
|
|
587
613
|
}
|
|
588
614
|
success(req, res, body) {
|
|
589
|
-
if (req)
|
|
590
|
-
|
|
591
|
-
if (
|
|
592
|
-
this.response(res);
|
|
593
|
-
if (body)
|
|
594
|
-
this.body(body);
|
|
615
|
+
if (req) this.request(req);
|
|
616
|
+
if (res) this.response(res);
|
|
617
|
+
if (body) this.body(body);
|
|
595
618
|
this._prepare();
|
|
596
619
|
super.info(this._message());
|
|
597
620
|
}
|
|
@@ -600,9 +623,30 @@ class FastifyLogger extends BaseLogger {
|
|
|
600
623
|
super.error(this._message(err));
|
|
601
624
|
}
|
|
602
625
|
}
|
|
626
|
+
class StaticLogger extends BaseLogger {
|
|
627
|
+
constructor(options = {}) {
|
|
628
|
+
options.console = false;
|
|
629
|
+
super("console", options);
|
|
630
|
+
}
|
|
631
|
+
log() {
|
|
632
|
+
console.log(...arguments);
|
|
633
|
+
let message = "";
|
|
634
|
+
Array.from(arguments).forEach((arg) => {
|
|
635
|
+
if (typeof arg === "object" && !Array.isArray(arg)) {
|
|
636
|
+
Object.assign(this.ctx, arg);
|
|
637
|
+
} else {
|
|
638
|
+
message += ` ${arg}`;
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
this.logger.info(message.trim(), this.ctx);
|
|
642
|
+
this.ctx = {};
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
const staticLogger = new StaticLogger();
|
|
603
646
|
export {
|
|
604
647
|
BaseLogger,
|
|
605
648
|
FastifyLogger,
|
|
606
649
|
HTTPLogger as HttpLogger,
|
|
607
|
-
plugin as MongooseLoggerPlugin
|
|
650
|
+
plugin as MongooseLoggerPlugin,
|
|
651
|
+
staticLogger as StaticLogger
|
|
608
652
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -106,10 +106,8 @@
|
|
|
106
106
|
class Logger {
|
|
107
107
|
constructor(options = {}) {
|
|
108
108
|
this.options = { ...OPTIONS$1, ...options };
|
|
109
|
-
if (!this.options.filename.includes("."))
|
|
110
|
-
|
|
111
|
-
if (!this.options.errorFilename.includes("."))
|
|
112
|
-
this.options.errorFilename += ".log";
|
|
109
|
+
if (!this.options.filename.includes(".")) this.options.filename += ".log";
|
|
110
|
+
if (!this.options.errorFilename.includes(".")) this.options.errorFilename += ".log";
|
|
113
111
|
let trans = [];
|
|
114
112
|
let exceptionHandlers = [new transports.Console({
|
|
115
113
|
format: getFormatter("console", this.options)
|
|
@@ -250,20 +248,52 @@
|
|
|
250
248
|
this.ctx = {};
|
|
251
249
|
this.startTime = Date.now();
|
|
252
250
|
this.absaluteStartTime = Date.now();
|
|
251
|
+
this.excludePatterns = [];
|
|
253
252
|
}
|
|
254
253
|
session(id) {
|
|
255
254
|
this.ctx.sessionID = id;
|
|
256
255
|
return this;
|
|
257
256
|
}
|
|
257
|
+
exclude(pattern) {
|
|
258
|
+
if (pattern instanceof RegExp) {
|
|
259
|
+
this.excludePatterns.push({ type: "regex", pattern });
|
|
260
|
+
} else if (typeof pattern === "string") {
|
|
261
|
+
this.excludePatterns.push({ type: "string", pattern });
|
|
262
|
+
} else if (Array.isArray(pattern)) {
|
|
263
|
+
pattern.forEach((p) => this.exclude(p));
|
|
264
|
+
}
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
_shouldExclude(message) {
|
|
268
|
+
if (!this.excludePatterns.length) return false;
|
|
269
|
+
const stringMessage = typeof message === "object" ? JSON.stringify(message) : String(message);
|
|
270
|
+
return this.excludePatterns.some(({ type, pattern }) => {
|
|
271
|
+
if (type === "regex") {
|
|
272
|
+
return pattern.test(stringMessage);
|
|
273
|
+
} else if (type === "string") {
|
|
274
|
+
return stringMessage.includes(pattern);
|
|
275
|
+
}
|
|
276
|
+
return false;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
258
279
|
log() {
|
|
280
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
259
283
|
this.logger.info(...arguments, this.ctx);
|
|
260
284
|
this._stopMemProfile();
|
|
261
285
|
}
|
|
262
286
|
info() {
|
|
287
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
263
290
|
this.logger.info(...arguments, this.ctx);
|
|
264
291
|
this._stopMemProfile();
|
|
265
292
|
}
|
|
266
293
|
error() {
|
|
294
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
267
297
|
this.logger.error(...arguments, this.ctx);
|
|
268
298
|
this._stopMemProfile();
|
|
269
299
|
}
|
|
@@ -274,13 +304,11 @@
|
|
|
274
304
|
this.ctx.profiler[action] = Date.now() - startTime;
|
|
275
305
|
}
|
|
276
306
|
this.ctx.profiler.totalTime = Date.now() - this.absaluteStartTime;
|
|
277
|
-
if (!options.continue)
|
|
278
|
-
this.startTime = Date.now();
|
|
307
|
+
if (!options.continue) this.startTime = Date.now();
|
|
279
308
|
return this;
|
|
280
309
|
}
|
|
281
310
|
_stopMemProfile() {
|
|
282
|
-
if (this.memProfileInterval)
|
|
283
|
-
clearInterval(this.memProfileInterval);
|
|
311
|
+
if (this.memProfileInterval) clearInterval(this.memProfileInterval);
|
|
284
312
|
}
|
|
285
313
|
profileMem(options = {}) {
|
|
286
314
|
this.ctx.maxMemory = this.ctx.maxMemory || 0;
|
|
@@ -299,9 +327,12 @@
|
|
|
299
327
|
return this;
|
|
300
328
|
}
|
|
301
329
|
context(key, value) {
|
|
302
|
-
if (
|
|
330
|
+
if (key === null || key === void 0) {
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
if (typeof key === "string" && arguments.length === 2) {
|
|
303
334
|
this.ctx[key] = value;
|
|
304
|
-
} else if (typeof key === "object") {
|
|
335
|
+
} else if (typeof key === "object" && !Array.isArray(key)) {
|
|
305
336
|
Object.assign(this.ctx, key);
|
|
306
337
|
}
|
|
307
338
|
return this;
|
|
@@ -348,7 +379,7 @@
|
|
|
348
379
|
this.ctx.response = {
|
|
349
380
|
headers: omit$1(res.getHeaders(), "set-cookie", "x-powered-by"),
|
|
350
381
|
statusCode: res.statusCode,
|
|
351
|
-
body: trim$1(body, MAX_BODY_LENGTH$1)
|
|
382
|
+
body: this.options.maxBodyLength <= 0 ? body : trim$1(body, this.options.maxBodyLength || MAX_BODY_LENGTH$1)
|
|
352
383
|
};
|
|
353
384
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
354
385
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -366,12 +397,9 @@
|
|
|
366
397
|
return `${method} ${url} ${statusCode} ${responseTimeMs} ${responseSize} ${ip} ${remoteAddress} ${msg || ""}`;
|
|
367
398
|
}
|
|
368
399
|
success(req, res, body) {
|
|
369
|
-
if (req)
|
|
370
|
-
|
|
371
|
-
if (
|
|
372
|
-
this.response(res);
|
|
373
|
-
if (body)
|
|
374
|
-
this.body(body);
|
|
400
|
+
if (req) this.request(req);
|
|
401
|
+
if (res) this.response(res);
|
|
402
|
+
if (body) this.body(body);
|
|
375
403
|
this._prepare();
|
|
376
404
|
super.info(this._message());
|
|
377
405
|
}
|
|
@@ -441,8 +469,7 @@
|
|
|
441
469
|
this.ctx.resultLength = Array.isArray(res) ? res.length : res ? 1 : 0;
|
|
442
470
|
} catch (err) {
|
|
443
471
|
}
|
|
444
|
-
if (!this.options.logRes)
|
|
445
|
-
return this;
|
|
472
|
+
if (!this.options.logRes) return this;
|
|
446
473
|
if (Array.isArray(res)) {
|
|
447
474
|
this.ctx.documentCount = res.length;
|
|
448
475
|
} else if (this.ctx.resultSizeBytes < this.options.maxResBytes) {
|
|
@@ -455,8 +482,7 @@
|
|
|
455
482
|
}
|
|
456
483
|
async function postHook(target, res) {
|
|
457
484
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
458
|
-
if ((_a = target == null ? void 0 : target.options) == null ? void 0 : _a.explain)
|
|
459
|
-
return;
|
|
485
|
+
if ((_a = target == null ? void 0 : target.options) == null ? void 0 : _a.explain) return;
|
|
460
486
|
const op = target.constructor.name === "Aggregate" ? "aggregate" : target.op || target.$op;
|
|
461
487
|
const collection = ((_b = target == null ? void 0 : target._collection) == null ? void 0 : _b.collectionName) || ((_c = target == null ? void 0 : target.collection) == null ? void 0 : _c.name) || ((_e = (_d = target == null ? void 0 : target._model) == null ? void 0 : _d.collection) == null ? void 0 : _e.collectionName);
|
|
462
488
|
if (!op || !collection) {
|
|
@@ -565,7 +591,7 @@
|
|
|
565
591
|
this.ctx.response = {
|
|
566
592
|
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
567
593
|
statusCode: this.getResponseStatusCode(res),
|
|
568
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
594
|
+
body: this.options.maxBodyLength <= 0 ? body : trim(body, this.options.maxBodyLength || MAX_BODY_LENGTH)
|
|
569
595
|
};
|
|
570
596
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
571
597
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -590,12 +616,9 @@
|
|
|
590
616
|
return this.req.ip || this.req._remoteAddress || void 0;
|
|
591
617
|
}
|
|
592
618
|
success(req, res, body) {
|
|
593
|
-
if (req)
|
|
594
|
-
|
|
595
|
-
if (
|
|
596
|
-
this.response(res);
|
|
597
|
-
if (body)
|
|
598
|
-
this.body(body);
|
|
619
|
+
if (req) this.request(req);
|
|
620
|
+
if (res) this.response(res);
|
|
621
|
+
if (body) this.body(body);
|
|
599
622
|
this._prepare();
|
|
600
623
|
super.info(this._message());
|
|
601
624
|
}
|
|
@@ -604,9 +627,30 @@
|
|
|
604
627
|
super.error(this._message(err));
|
|
605
628
|
}
|
|
606
629
|
}
|
|
630
|
+
class StaticLogger extends BaseLogger {
|
|
631
|
+
constructor(options = {}) {
|
|
632
|
+
options.console = false;
|
|
633
|
+
super("console", options);
|
|
634
|
+
}
|
|
635
|
+
log() {
|
|
636
|
+
console.log(...arguments);
|
|
637
|
+
let message = "";
|
|
638
|
+
Array.from(arguments).forEach((arg) => {
|
|
639
|
+
if (typeof arg === "object" && !Array.isArray(arg)) {
|
|
640
|
+
Object.assign(this.ctx, arg);
|
|
641
|
+
} else {
|
|
642
|
+
message += ` ${arg}`;
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
this.logger.info(message.trim(), this.ctx);
|
|
646
|
+
this.ctx = {};
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
const staticLogger = new StaticLogger();
|
|
607
650
|
exports2.BaseLogger = BaseLogger;
|
|
608
651
|
exports2.FastifyLogger = FastifyLogger;
|
|
609
652
|
exports2.HttpLogger = HTTPLogger;
|
|
610
653
|
exports2.MongooseLoggerPlugin = plugin;
|
|
654
|
+
exports2.StaticLogger = staticLogger;
|
|
611
655
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
612
656
|
});
|
package/dist/main.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export class BaseLogger {
|
|
|
3
3
|
|
|
4
4
|
session(id: string): this;
|
|
5
5
|
|
|
6
|
+
exclude(pattern: string | RegExp | Array<string | RegExp>): this;
|
|
7
|
+
|
|
6
8
|
log(...args: any[]): void;
|
|
7
9
|
|
|
8
10
|
info(...args: any[]): void;
|
|
@@ -13,10 +15,22 @@ export class BaseLogger {
|
|
|
13
15
|
|
|
14
16
|
profileMem(options?: { interval?: number }): this;
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Add context data to be included with all log messages
|
|
20
|
+
* @param key - Either a string key or an object containing key-value pairs
|
|
21
|
+
* @param value - The value to set (only used when key is a string)
|
|
22
|
+
*/
|
|
23
|
+
context(key: string, value: any): this;
|
|
24
|
+
context(contextObj: Record<string, any>): this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare class StaticLoggerClass extends BaseLogger {
|
|
28
|
+
constructor(options?: any);
|
|
29
|
+
log(...args: any[]): void;
|
|
18
30
|
}
|
|
19
31
|
|
|
32
|
+
export const StaticLogger: StaticLoggerClass;
|
|
33
|
+
|
|
20
34
|
export class HTTPLogger extends BaseLogger {
|
|
21
35
|
constructor(startTime: number, options?: any);
|
|
22
36
|
|