hermodlog 1.10.0 → 2.0.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/package.json
CHANGED
package/src/Logger.js
CHANGED
|
@@ -32,7 +32,7 @@ class Logger {
|
|
|
32
32
|
enumerable: false
|
|
33
33
|
},
|
|
34
34
|
historyLimit: {
|
|
35
|
-
value: props.historyLimit ??
|
|
35
|
+
value: props.historyLimit ?? 0,
|
|
36
36
|
writable: true,
|
|
37
37
|
enumerable: true
|
|
38
38
|
},
|
|
@@ -81,7 +81,6 @@ class Logger {
|
|
|
81
81
|
writable: true,
|
|
82
82
|
enumerable: true
|
|
83
83
|
}
|
|
84
|
-
|
|
85
84
|
});
|
|
86
85
|
|
|
87
86
|
if (props.date) {
|
|
@@ -114,11 +113,13 @@ class Logger {
|
|
|
114
113
|
const keepListener = opts?.keepListener ?? true;
|
|
115
114
|
const keepObject = opts?.keepObject ?? true;
|
|
116
115
|
const keepHandler = opts?.keepHandler ?? true;
|
|
116
|
+
const historyLimit = opts?.historyLimit ?? null;
|
|
117
117
|
|
|
118
118
|
return new Logger({
|
|
119
119
|
level: (keepLevel) ? this.level : opts.level ?? this.level,
|
|
120
120
|
date: (keepDate) ? this.date : opts.date ?? null,
|
|
121
121
|
history: keepHistory ? this.history : [],
|
|
122
|
+
historyLimit: historyLimit ?? this.historyLimit,
|
|
122
123
|
colors: (keepColors) ? this.LOG_COLORS : opts.colors ?? this.LOG_COLORS,
|
|
123
124
|
contextName: (keepContext) ? this.contextName : opts.contextName ?? null,
|
|
124
125
|
methodName: (keepMethod) ? this.methodName : opts.methodName ?? null,
|
package/src/Logger.spec.js
CHANGED
|
@@ -5,7 +5,7 @@ describe('Logger', () => {
|
|
|
5
5
|
let silentLogger
|
|
6
6
|
let silentModuleLogger
|
|
7
7
|
it('should create an logger', () => {
|
|
8
|
-
silentLogger = new Logger({date:new Date('2023-07-29T01:38:00.482Z')});
|
|
8
|
+
silentLogger = new Logger({date:new Date('2023-07-29T01:38:00.482Z'), historyLimit: 100});
|
|
9
9
|
silentLogger._log = () => {}
|
|
10
10
|
expect(silentLogger.history.length).toBe(0)
|
|
11
11
|
silentLogger.log('Hello');
|
|
@@ -42,7 +42,7 @@ describe('Logger', () => {
|
|
|
42
42
|
expect(silentMethodLogger.history[0]).toBe(expected)
|
|
43
43
|
})
|
|
44
44
|
it('should handle level', function () {
|
|
45
|
-
const levelLogger = new Logger({level: 'error'})
|
|
45
|
+
const levelLogger = new Logger({level: 'error', historyLimit: 100});
|
|
46
46
|
levelLogger._log = () => {}
|
|
47
47
|
levelLogger.error('Hello');
|
|
48
48
|
expect(levelLogger.history.length).toBe(1)
|
|
@@ -67,4 +67,18 @@ describe('Logger', () => {
|
|
|
67
67
|
const expected = `[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m - \u001b[32mReceived from ws client\u001b[0m \u001b[32mObject(\u001b[0m\u001b[32m{\n \"x\": 42,\n \"y\": {\n \"b\": [\n 1,\n 2,\n 3\n ]\n },\n \"req\": \"getCar\"\n}\u001b[0m\u001b[32m)\u001b[0m`;
|
|
68
68
|
expect(silentModuleLogger.history[1]).toBe(expected)
|
|
69
69
|
});
|
|
70
|
+
|
|
71
|
+
it('should transmit history config to child loggers', () => {
|
|
72
|
+
const parentLogger = new Logger({date:new Date('2023-07-29T01:38:00.482Z'), historyLimit: 123});
|
|
73
|
+
parentLogger._log = () => {}
|
|
74
|
+
parentLogger.log('hello');
|
|
75
|
+
expect(parentLogger.historyLimit).toBe(123)
|
|
76
|
+
expect(parentLogger.history.length).toBe(1)
|
|
77
|
+
|
|
78
|
+
const childLogger = parentLogger.listener('onTest')
|
|
79
|
+
expect(childLogger.historyLimit).toBe(123)
|
|
80
|
+
expect(childLogger.history.length).toBe(0)
|
|
81
|
+
childLogger.log('Hello');
|
|
82
|
+
expect(childLogger.history.length).toBe(1)
|
|
83
|
+
})
|
|
70
84
|
})
|
package/src/utils/log.js
CHANGED
|
@@ -166,10 +166,12 @@ export default function log(level, args, context) {
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
+
const historyLimit = context?.historyLimit;
|
|
170
|
+
if (historyLimit) {
|
|
169
171
|
context.history.push(message);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
172
|
+
if (context.history.length > historyLimit) {
|
|
173
|
+
context.history.shift();
|
|
174
|
+
}
|
|
173
175
|
}
|
|
174
176
|
|
|
175
177
|
context._log(message);
|
|
File without changes
|