@vvlad1973/simple-logger 1.2.0 → 1.2.2
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 +3 -3
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +78 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1611 -0
- package/docs/classes/SimpleLogger.html +25 -0
- package/docs/index.html +39 -129
- package/docs/media/index.html +39 -0
- package/docs/modules.html +1 -0
- package/docs/types/ExternalLogger.html +1 -0
- package/docs/types/LoggerLevel.html +1 -0
- package/package.json +6 -6
- package/src/__test__/simple-logger.test.ts +29 -148
- package/src/simple-logger.ts +25 -23
- package/typedoc.json +8 -0
- package/docs/module-SimpleLogger.SimpleLogger.html +0 -1000
- package/docs/module-SimpleLogger.html +0 -238
- package/docs/scripts/app.min.js +0 -1
- package/docs/scripts/linenumber.js +0 -26
- package/docs/scripts/search.js +0 -39
- package/docs/simple-logger.ts.html +0 -303
- package/docs/styles/app.min.css +0 -1
- package/docs/styles/iframe.css +0 -13
- package/docs/styles/prettify-jsdoc.css +0 -111
- package/docs/styles/prettify-tomorrow.css +0 -132
- package/docs/styles/reset.css +0 -44
|
@@ -1,51 +1,25 @@
|
|
|
1
|
-
import { describe, it, beforeEach } from '
|
|
2
|
-
import assert from 'assert';
|
|
1
|
+
import { describe, it, beforeEach, expect, vi } from 'vitest';
|
|
3
2
|
import SimpleLogger from '../simple-logger.js';
|
|
4
3
|
|
|
5
|
-
type MockFunction = {
|
|
6
|
-
(...args: any[]): void;
|
|
7
|
-
mock: { calls: any[][] };
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
type NoopFunction = {
|
|
11
|
-
(...args: any[]): void;
|
|
12
|
-
mock: { calls: any[][] };
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
function createMockLoggerMethod(): MockFunction {
|
|
16
|
-
const calls: any[][] = [];
|
|
17
|
-
const mockFn = function (...args: any[]) {
|
|
18
|
-
calls.push(args);
|
|
19
|
-
} as MockFunction;
|
|
20
|
-
mockFn.mock = { calls };
|
|
21
|
-
return mockFn;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function createNoopMethod(): MockFunction {
|
|
25
|
-
const calls: any[][] = [];
|
|
26
|
-
const mockFn = function (...args: any[]) {} as MockFunction;
|
|
27
|
-
mockFn.mock = { calls };
|
|
28
|
-
return mockFn;
|
|
29
|
-
}
|
|
30
4
|
type ExternalLoggerMock = {
|
|
31
|
-
trace:
|
|
32
|
-
debug:
|
|
33
|
-
info:
|
|
34
|
-
warn:
|
|
35
|
-
error:
|
|
36
|
-
fatal:
|
|
37
|
-
silent:
|
|
5
|
+
trace: ReturnType<typeof vi.fn>;
|
|
6
|
+
debug: ReturnType<typeof vi.fn>;
|
|
7
|
+
info: ReturnType<typeof vi.fn>;
|
|
8
|
+
warn: ReturnType<typeof vi.fn>;
|
|
9
|
+
error: ReturnType<typeof vi.fn>;
|
|
10
|
+
fatal: ReturnType<typeof vi.fn>;
|
|
11
|
+
silent: ReturnType<typeof vi.fn>;
|
|
38
12
|
};
|
|
39
13
|
|
|
40
14
|
function createExternalLoggerMock(): ExternalLoggerMock {
|
|
41
15
|
return {
|
|
42
|
-
trace:
|
|
43
|
-
debug:
|
|
44
|
-
info:
|
|
45
|
-
warn:
|
|
46
|
-
error:
|
|
47
|
-
fatal:
|
|
48
|
-
silent:
|
|
16
|
+
trace: vi.fn(),
|
|
17
|
+
debug: vi.fn(),
|
|
18
|
+
info: vi.fn(),
|
|
19
|
+
warn: vi.fn(),
|
|
20
|
+
error: vi.fn(),
|
|
21
|
+
fatal: vi.fn(),
|
|
22
|
+
silent: vi.fn(),
|
|
49
23
|
};
|
|
50
24
|
}
|
|
51
25
|
|
|
@@ -66,107 +40,20 @@ describe('SimpleLogger', () => {
|
|
|
66
40
|
logger.error('error message');
|
|
67
41
|
logger.fatal('fatal message');
|
|
68
42
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
'warn message'
|
|
80
|
-
);
|
|
81
|
-
assert.strictEqual(externalLoggerMock.error.mock.calls.length, 1);
|
|
82
|
-
assert.strictEqual(
|
|
83
|
-
externalLoggerMock.error.mock.calls[0][0],
|
|
84
|
-
'error message'
|
|
85
|
-
);
|
|
86
|
-
assert.strictEqual(externalLoggerMock.fatal.mock.calls.length, 1);
|
|
87
|
-
assert.strictEqual(
|
|
88
|
-
externalLoggerMock.fatal.mock.calls[0][0],
|
|
89
|
-
'fatal message'
|
|
90
|
-
);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('should log function start and end', () => {
|
|
94
|
-
logger.setLevel('TRACE');
|
|
95
|
-
|
|
96
|
-
function testFunction() {
|
|
97
|
-
logger.logFunctionStart();
|
|
98
|
-
logger.logFunctionEnd();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
testFunction();
|
|
102
|
-
|
|
103
|
-
assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 2);
|
|
104
|
-
assert.strictEqual(
|
|
105
|
-
externalLoggerMock.trace.mock.calls[0][0],
|
|
106
|
-
'Function start: testFunction'
|
|
107
|
-
);
|
|
108
|
-
assert.strictEqual(
|
|
109
|
-
externalLoggerMock.trace.mock.calls[1][0],
|
|
110
|
-
'Function end: testFunction'
|
|
111
|
-
);
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
describe('SimpleLogger2', () => {
|
|
116
|
-
let logger: SimpleLogger;
|
|
117
|
-
let externalLoggerMock: ExternalLoggerMock;
|
|
118
|
-
|
|
119
|
-
beforeEach(() => {
|
|
120
|
-
externalLoggerMock = createExternalLoggerMock();
|
|
121
|
-
logger = new SimpleLogger('info', externalLoggerMock);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('should use console as default external logger', () => {
|
|
125
|
-
const defaultLogger = new SimpleLogger();
|
|
126
|
-
assert.ok(defaultLogger);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it('should set the correct log level', () => {
|
|
130
|
-
logger.setLevel('DEBUG');
|
|
131
|
-
assert.notStrictEqual(logger.trace, () => {});
|
|
132
|
-
assert.strictEqual(typeof logger.debug, 'function');
|
|
133
|
-
assert.strictEqual(typeof logger.info, 'function');
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should call external logger methods appropriately', () => {
|
|
137
|
-
logger.trace('trace message');
|
|
138
|
-
logger.debug('debug message');
|
|
139
|
-
logger.info('info message');
|
|
140
|
-
logger.warn('warn message');
|
|
141
|
-
logger.error('error message');
|
|
142
|
-
logger.fatal('fatal message');
|
|
143
|
-
|
|
144
|
-
assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 0); // because default level is INFO
|
|
145
|
-
assert.strictEqual(externalLoggerMock.debug.mock.calls.length, 0); // because default level is INFO
|
|
146
|
-
assert.strictEqual(externalLoggerMock.info.mock.calls.length, 1);
|
|
147
|
-
assert.strictEqual(
|
|
148
|
-
externalLoggerMock.info.mock.calls[0][0],
|
|
149
|
-
'info message'
|
|
150
|
-
);
|
|
151
|
-
assert.strictEqual(externalLoggerMock.warn.mock.calls.length, 1);
|
|
152
|
-
assert.strictEqual(
|
|
153
|
-
externalLoggerMock.warn.mock.calls[0][0],
|
|
154
|
-
'warn message'
|
|
155
|
-
);
|
|
156
|
-
assert.strictEqual(externalLoggerMock.error.mock.calls.length, 1);
|
|
157
|
-
assert.strictEqual(
|
|
158
|
-
externalLoggerMock.error.mock.calls[0][0],
|
|
159
|
-
'error message'
|
|
160
|
-
);
|
|
161
|
-
assert.strictEqual(externalLoggerMock.fatal.mock.calls.length, 1);
|
|
162
|
-
assert.strictEqual(
|
|
163
|
-
externalLoggerMock.fatal.mock.calls[0][0],
|
|
164
|
-
'fatal message'
|
|
165
|
-
);
|
|
43
|
+
expect(externalLoggerMock.trace).not.toHaveBeenCalled();
|
|
44
|
+
expect(externalLoggerMock.debug).not.toHaveBeenCalled();
|
|
45
|
+
expect(externalLoggerMock.info).toHaveBeenCalledTimes(1);
|
|
46
|
+
expect(externalLoggerMock.info).toHaveBeenCalledWith('info message');
|
|
47
|
+
expect(externalLoggerMock.warn).toHaveBeenCalledTimes(1);
|
|
48
|
+
expect(externalLoggerMock.warn).toHaveBeenCalledWith('warn message');
|
|
49
|
+
expect(externalLoggerMock.error).toHaveBeenCalledTimes(1);
|
|
50
|
+
expect(externalLoggerMock.error).toHaveBeenCalledWith('error message');
|
|
51
|
+
expect(externalLoggerMock.fatal).toHaveBeenCalledTimes(1);
|
|
52
|
+
expect(externalLoggerMock.fatal).toHaveBeenCalledWith('fatal message');
|
|
166
53
|
});
|
|
167
54
|
|
|
168
55
|
it('should log function start and end', () => {
|
|
169
|
-
logger.setLevel('
|
|
56
|
+
logger.setLevel('trace');
|
|
170
57
|
|
|
171
58
|
function testFunction() {
|
|
172
59
|
logger.logFunctionStart();
|
|
@@ -175,14 +62,8 @@ describe('SimpleLogger2', () => {
|
|
|
175
62
|
|
|
176
63
|
testFunction();
|
|
177
64
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
'Function start: testFunction'
|
|
182
|
-
);
|
|
183
|
-
assert.strictEqual(
|
|
184
|
-
externalLoggerMock.trace.mock.calls[1][0],
|
|
185
|
-
'Function end: testFunction'
|
|
186
|
-
);
|
|
65
|
+
expect(externalLoggerMock.trace).toHaveBeenCalledTimes(2);
|
|
66
|
+
expect(externalLoggerMock.trace).toHaveBeenCalledWith('Function start: testFunction');
|
|
67
|
+
expect(externalLoggerMock.trace).toHaveBeenCalledWith('Function end: testFunction');
|
|
187
68
|
});
|
|
188
69
|
});
|
package/src/simple-logger.ts
CHANGED
|
@@ -31,6 +31,7 @@ const loggerLevels = [
|
|
|
31
31
|
'fatal',
|
|
32
32
|
'silent',
|
|
33
33
|
] as const;
|
|
34
|
+
|
|
34
35
|
export type LoggerLevel = (typeof loggerLevels)[number];
|
|
35
36
|
/**
|
|
36
37
|
* A simple logger class with various logging levels.
|
|
@@ -61,28 +62,23 @@ export class SimpleLogger {
|
|
|
61
62
|
fatal: LogFn = (...args: any[]) => {};
|
|
62
63
|
silent: LogFn = (...args: any[]) => {};
|
|
63
64
|
|
|
65
|
+
private static readonly noop: LogFn = (...args: any[]) => {};
|
|
66
|
+
|
|
64
67
|
/**
|
|
65
68
|
* Updates the logging methods based on the current logging level.
|
|
66
69
|
* @private
|
|
67
70
|
*/
|
|
68
71
|
private updateLoggingMethods() {
|
|
69
72
|
try {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.error =
|
|
80
|
-
this.#currentLevel <= 4 ? this.getExternalLoggerMethod('error') : noop;
|
|
81
|
-
this.fatal =
|
|
82
|
-
this.#currentLevel <= 5
|
|
83
|
-
? this.getExternalLoggerMethod('fatal', 'error')
|
|
84
|
-
: noop;
|
|
85
|
-
this.silent = noop;
|
|
73
|
+
this.#levels.forEach((level, index) => {
|
|
74
|
+
this[level] =
|
|
75
|
+
this.#currentLevel <= index
|
|
76
|
+
? this.getExternalLoggerMethod(
|
|
77
|
+
level,
|
|
78
|
+
level === 'fatal' ? 'error' : undefined
|
|
79
|
+
)
|
|
80
|
+
: SimpleLogger.noop;
|
|
81
|
+
});
|
|
86
82
|
} catch (error) {
|
|
87
83
|
console.error(error);
|
|
88
84
|
}
|
|
@@ -95,16 +91,18 @@ export class SimpleLogger {
|
|
|
95
91
|
* @returns {boolean} - True if the logger is valid, otherwise false.
|
|
96
92
|
*/
|
|
97
93
|
private isValidLogger(logger: any): boolean {
|
|
98
|
-
return
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
return (
|
|
95
|
+
typeof logger === 'object' &&
|
|
96
|
+
logger !== null &&
|
|
97
|
+
loggerLevels.some((level) => typeof logger[level] === 'function')
|
|
98
|
+
);
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
/**
|
|
104
102
|
* Gets the external logger method for a specific level.
|
|
105
103
|
* @param {string} method - The logger method to get.
|
|
106
104
|
* @param {string} [fallbackMethod] - The fallback logger method to get.
|
|
107
|
-
* @returns {
|
|
105
|
+
* @returns {LogFn} - The logger method or a no-op function.
|
|
108
106
|
* @private
|
|
109
107
|
*/
|
|
110
108
|
private getExternalLoggerMethod(
|
|
@@ -125,10 +123,10 @@ export class SimpleLogger {
|
|
|
125
123
|
return fallbackLoggerMethod.bind(this.#logger);
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
|
-
return
|
|
126
|
+
return SimpleLogger.noop;
|
|
129
127
|
} catch (error) {
|
|
130
128
|
console.error(error);
|
|
131
|
-
return
|
|
129
|
+
return SimpleLogger.noop;
|
|
132
130
|
}
|
|
133
131
|
}
|
|
134
132
|
|
|
@@ -163,7 +161,11 @@ export class SimpleLogger {
|
|
|
163
161
|
this.#logger = console;
|
|
164
162
|
this.isExternal = false;
|
|
165
163
|
}
|
|
166
|
-
this.setLevel(
|
|
164
|
+
this.setLevel(
|
|
165
|
+
level ??
|
|
166
|
+
(logger?.level as LoggerLevel) ??
|
|
167
|
+
this.#levels[this.#currentLevel]
|
|
168
|
+
);
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
/**
|