hermodlog 1.7.0 → 1.8.1
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 +3 -3
- package/src/Logger.js +26 -2
- package/src/Logger.spec.js +23 -23
- package/usage.example.js +9 -1
package/package.json
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "hermodlog",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.8.1",
|
4
4
|
"description": "Simple contextual logger to simplify log display in heavy load context and provide easy parsing",
|
5
5
|
"main": "src/Logger.js",
|
6
6
|
"type": "module",
|
7
7
|
"scripts": {
|
8
|
-
"test": "
|
8
|
+
"test": "litest .",
|
9
9
|
"test:dev": "vitest --dir . --watch"
|
10
10
|
},
|
11
11
|
"repository": {
|
@@ -19,6 +19,6 @@
|
|
19
19
|
},
|
20
20
|
"homepage": "https://github.com/Alex-Werner/hermodlog#readme",
|
21
21
|
"devDependencies": {
|
22
|
-
"
|
22
|
+
"@scintilla-network/litest": "1.3.1"
|
23
23
|
}
|
24
24
|
}
|
package/src/Logger.js
CHANGED
@@ -116,9 +116,33 @@ class Logger {
|
|
116
116
|
})
|
117
117
|
}
|
118
118
|
|
119
|
-
getHistory(limit = 100) {
|
120
|
-
|
119
|
+
getHistory(limit = 100, options = {}) {
|
120
|
+
let history = this.history;
|
121
|
+
|
122
|
+
// options.from Date: filter history from this date
|
123
|
+
if (options.fromDate) {
|
124
|
+
history = history.filter(item => {
|
125
|
+
const date = item.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/);
|
126
|
+
if (date) {
|
127
|
+
return new Date(date[0]) >= options.fromDate;
|
128
|
+
}
|
129
|
+
return false;
|
130
|
+
});
|
131
|
+
}
|
132
|
+
|
133
|
+
if(options.toDate) {
|
134
|
+
history = history.filter(item => {
|
135
|
+
const date = item.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z/);
|
136
|
+
if (date) {
|
137
|
+
return new Date(date[0]) <= options.toDate;
|
138
|
+
}
|
139
|
+
return false;
|
140
|
+
});
|
141
|
+
}
|
142
|
+
|
143
|
+
return history.slice(-limit);
|
121
144
|
}
|
145
|
+
|
122
146
|
};
|
123
147
|
|
124
148
|
Logger.prototype.context = context;
|
package/src/Logger.spec.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import {describe, expect, it} from '@scintilla-network/litest'
|
2
2
|
import Logger from "./Logger.js";
|
3
3
|
|
4
4
|
describe('Logger', () => {
|
@@ -7,51 +7,51 @@ describe('Logger', () => {
|
|
7
7
|
it('should create an logger', () => {
|
8
8
|
silentLogger = new Logger({date:new Date('2023-07-29T01:38:00.482Z')});
|
9
9
|
silentLogger._log = () => {}
|
10
|
-
|
10
|
+
expect(silentLogger.history.length).toBe(0)
|
11
11
|
silentLogger.log('Hello');
|
12
|
-
|
13
|
-
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] \u001b[32mHello\u001b[0m'
|
14
|
-
|
12
|
+
expect(silentLogger.history.length).toBe(1)
|
13
|
+
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] - \u001b[32mHello\u001b[0m'
|
14
|
+
expect(silentLogger.history[0]).toBe(expected)
|
15
15
|
})
|
16
16
|
it('should create an logger with module', () => {
|
17
17
|
silentModuleLogger = silentLogger.module('test-module')
|
18
18
|
silentModuleLogger.log('Hello');
|
19
|
-
|
20
|
-
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m \u001b[32mHello\u001b[0m'
|
21
|
-
|
19
|
+
expect(silentModuleLogger.history.length).toBe(1)
|
20
|
+
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m - \u001b[32mHello\u001b[0m'
|
21
|
+
expect(silentModuleLogger.history[0]).toBe(expected)
|
22
22
|
})
|
23
23
|
it('should create an logger with context', () => {
|
24
24
|
const silentContextLogger = silentModuleLogger.context('test-context')
|
25
25
|
silentContextLogger.log('Hello');
|
26
|
-
|
27
|
-
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] context: \u001b[94mtest-context\u001b[0m | module:\u001b[90mtest-module\u001b[0m \u001b[32mHello\u001b[0m'
|
28
|
-
|
26
|
+
expect(silentContextLogger.history.length).toBe(1)
|
27
|
+
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] context: \u001b[94mtest-context\u001b[0m | module:\u001b[90mtest-module\u001b[0m - \u001b[32mHello\u001b[0m'
|
28
|
+
expect(silentContextLogger.history[0]).toBe(expected)
|
29
29
|
})
|
30
30
|
it('should create an logger with listener', () => {
|
31
31
|
const silentListenerLogger = silentModuleLogger.listener('onTest')
|
32
32
|
silentListenerLogger.log('Hello');
|
33
|
-
|
34
|
-
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m | listener: \u001b[36monTest\u001b[0m \u001b[32mHello\u001b[0m'
|
35
|
-
|
33
|
+
expect(silentListenerLogger.history.length).toBe(1)
|
34
|
+
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m | listener: \u001b[36monTest\u001b[0m - \u001b[32mHello\u001b[0m'
|
35
|
+
expect(silentListenerLogger.history[0]).toBe(expected)
|
36
36
|
})
|
37
37
|
it('should create an logger with method', () => {
|
38
38
|
const silentMethodLogger = silentModuleLogger.method('test-method')
|
39
39
|
silentMethodLogger.log('Hello');
|
40
|
-
|
41
|
-
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m | method: \u001b[33mtest-method\u001b[0m \u001b[32mHello\u001b[0m'
|
42
|
-
|
40
|
+
expect(silentMethodLogger.history.length).toBe(1)
|
41
|
+
const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m | method: \u001b[33mtest-method\u001b[0m - \u001b[32mHello\u001b[0m'
|
42
|
+
expect(silentMethodLogger.history[0]).toBe(expected)
|
43
43
|
})
|
44
44
|
it('should handle level', function () {
|
45
45
|
const levelLogger = new Logger({level: 'error'})
|
46
46
|
levelLogger._log = () => {}
|
47
47
|
levelLogger.error('Hello');
|
48
|
-
|
48
|
+
expect(levelLogger.history.length).toBe(1)
|
49
49
|
levelLogger.warn('Hello');
|
50
|
-
|
50
|
+
expect(levelLogger.history.length).toBe(1)
|
51
51
|
|
52
52
|
levelLogger.level = 'info'
|
53
53
|
levelLogger.trace('Hello');
|
54
|
-
|
54
|
+
expect(levelLogger.history.length).toBe(1)
|
55
55
|
});
|
56
56
|
|
57
57
|
it('should automatically beautify instead of [Object Object]', () => {
|
@@ -63,8 +63,8 @@ describe('Logger', () => {
|
|
63
63
|
req: 'getCar'
|
64
64
|
}
|
65
65
|
silentModuleLogger.log('Received from ws client', object)
|
66
|
-
|
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
|
-
|
66
|
+
expect(silentModuleLogger.history.length).toBe(2)
|
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
|
+
expect(silentModuleLogger.history[1]).toBe(expected)
|
69
69
|
});
|
70
70
|
})
|
package/usage.example.js
CHANGED
@@ -32,4 +32,12 @@ el.log(2);
|
|
32
32
|
el.log(3);
|
33
33
|
el.log(4);
|
34
34
|
el.log(5);
|
35
|
-
|
35
|
+
// Wait 5 seconds
|
36
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
37
|
+
el.log(6);
|
38
|
+
el.log(7);
|
39
|
+
el.log(8);
|
40
|
+
el.log(9);
|
41
|
+
el.log(10);
|
42
|
+
|
43
|
+
console.log(el.getHistory(2,{fromDate: new Date('2025-10-08T02:52:20')}));
|