infront-logger 1.1.12 → 1.1.14
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 +29 -1
- package/dist/index.es.js +60 -4
- package/dist/index.umd.js +59 -3
- 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
|
package/dist/index.es.js
CHANGED
|
@@ -244,20 +244,52 @@ class BaseLogger {
|
|
|
244
244
|
this.ctx = {};
|
|
245
245
|
this.startTime = Date.now();
|
|
246
246
|
this.absaluteStartTime = Date.now();
|
|
247
|
+
this.excludePatterns = [];
|
|
247
248
|
}
|
|
248
249
|
session(id) {
|
|
249
250
|
this.ctx.sessionID = id;
|
|
250
251
|
return this;
|
|
251
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
|
+
}
|
|
252
275
|
log() {
|
|
276
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
253
279
|
this.logger.info(...arguments, this.ctx);
|
|
254
280
|
this._stopMemProfile();
|
|
255
281
|
}
|
|
256
282
|
info() {
|
|
283
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
257
286
|
this.logger.info(...arguments, this.ctx);
|
|
258
287
|
this._stopMemProfile();
|
|
259
288
|
}
|
|
260
289
|
error() {
|
|
290
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
261
293
|
this.logger.error(...arguments, this.ctx);
|
|
262
294
|
this._stopMemProfile();
|
|
263
295
|
}
|
|
@@ -291,9 +323,12 @@ class BaseLogger {
|
|
|
291
323
|
return this;
|
|
292
324
|
}
|
|
293
325
|
context(key, value) {
|
|
294
|
-
if (
|
|
326
|
+
if (key === null || key === void 0) {
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
if (typeof key === "string" && arguments.length === 2) {
|
|
295
330
|
this.ctx[key] = value;
|
|
296
|
-
} else if (typeof key === "object") {
|
|
331
|
+
} else if (typeof key === "object" && !Array.isArray(key)) {
|
|
297
332
|
Object.assign(this.ctx, key);
|
|
298
333
|
}
|
|
299
334
|
return this;
|
|
@@ -552,7 +587,7 @@ class FastifyLogger extends BaseLogger {
|
|
|
552
587
|
this.ctx.response = {
|
|
553
588
|
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
554
589
|
statusCode: this.getResponseStatusCode(res),
|
|
555
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
590
|
+
body: this.options.maxBodyLength <= 0 ? body : trim(body, this.options.maxBodyLength || MAX_BODY_LENGTH)
|
|
556
591
|
};
|
|
557
592
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
558
593
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -588,9 +623,30 @@ class FastifyLogger extends BaseLogger {
|
|
|
588
623
|
super.error(this._message(err));
|
|
589
624
|
}
|
|
590
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();
|
|
591
646
|
export {
|
|
592
647
|
BaseLogger,
|
|
593
648
|
FastifyLogger,
|
|
594
649
|
HTTPLogger as HttpLogger,
|
|
595
|
-
plugin as MongooseLoggerPlugin
|
|
650
|
+
plugin as MongooseLoggerPlugin,
|
|
651
|
+
staticLogger as StaticLogger
|
|
596
652
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -248,20 +248,52 @@
|
|
|
248
248
|
this.ctx = {};
|
|
249
249
|
this.startTime = Date.now();
|
|
250
250
|
this.absaluteStartTime = Date.now();
|
|
251
|
+
this.excludePatterns = [];
|
|
251
252
|
}
|
|
252
253
|
session(id) {
|
|
253
254
|
this.ctx.sessionID = id;
|
|
254
255
|
return this;
|
|
255
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
|
+
}
|
|
256
279
|
log() {
|
|
280
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
257
283
|
this.logger.info(...arguments, this.ctx);
|
|
258
284
|
this._stopMemProfile();
|
|
259
285
|
}
|
|
260
286
|
info() {
|
|
287
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
261
290
|
this.logger.info(...arguments, this.ctx);
|
|
262
291
|
this._stopMemProfile();
|
|
263
292
|
}
|
|
264
293
|
error() {
|
|
294
|
+
if (arguments.length > 0 && this._shouldExclude(arguments[0])) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
265
297
|
this.logger.error(...arguments, this.ctx);
|
|
266
298
|
this._stopMemProfile();
|
|
267
299
|
}
|
|
@@ -295,9 +327,12 @@
|
|
|
295
327
|
return this;
|
|
296
328
|
}
|
|
297
329
|
context(key, value) {
|
|
298
|
-
if (
|
|
330
|
+
if (key === null || key === void 0) {
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
if (typeof key === "string" && arguments.length === 2) {
|
|
299
334
|
this.ctx[key] = value;
|
|
300
|
-
} else if (typeof key === "object") {
|
|
335
|
+
} else if (typeof key === "object" && !Array.isArray(key)) {
|
|
301
336
|
Object.assign(this.ctx, key);
|
|
302
337
|
}
|
|
303
338
|
return this;
|
|
@@ -556,7 +591,7 @@
|
|
|
556
591
|
this.ctx.response = {
|
|
557
592
|
headers: omit(this.getResponseHeaders(res), "set-cookie", "x-powered-by"),
|
|
558
593
|
statusCode: this.getResponseStatusCode(res),
|
|
559
|
-
body: trim(body, MAX_BODY_LENGTH)
|
|
594
|
+
body: this.options.maxBodyLength <= 0 ? body : trim(body, this.options.maxBodyLength || MAX_BODY_LENGTH)
|
|
560
595
|
};
|
|
561
596
|
this.ctx.responseTimeMs = Date.now() - this.startTime;
|
|
562
597
|
this.ctx.responseSizeBytes = this.data ? JSON.stringify(this.data).length : 0;
|
|
@@ -592,9 +627,30 @@
|
|
|
592
627
|
super.error(this._message(err));
|
|
593
628
|
}
|
|
594
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();
|
|
595
650
|
exports2.BaseLogger = BaseLogger;
|
|
596
651
|
exports2.FastifyLogger = FastifyLogger;
|
|
597
652
|
exports2.HttpLogger = HTTPLogger;
|
|
598
653
|
exports2.MongooseLoggerPlugin = plugin;
|
|
654
|
+
exports2.StaticLogger = staticLogger;
|
|
599
655
|
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
600
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
|
|