@vvlad1973/simple-logger 2.1.9 → 2.1.10
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/dist/__test__/simple-logger.test.js +65 -65
- package/dist/classes/simple-logger.d.ts +125 -125
- package/dist/classes/simple-logger.js +294 -294
- package/dist/classes/simple-logger.js.map +1 -1
- package/dist/constants/constants.d.ts +9 -9
- package/dist/constants/constants.js +9 -9
- package/dist/helpers/helpers.d.ts +31 -31
- package/dist/helpers/helpers.js +80 -80
- package/dist/helpers/validators.d.ts +17 -17
- package/dist/helpers/validators.js +26 -26
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/dist/types/simple-logger.types.d.ts +33 -33
- package/dist/types/simple-logger.types.js +1 -1
- package/package.json +1 -1
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import SimpleLogger from '../classes/simple-logger.js';
|
|
3
|
-
describe('SimpleLogger', () => {
|
|
4
|
-
let consoleSpy;
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
7
|
-
});
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
consoleSpy.mockRestore();
|
|
10
|
-
});
|
|
11
|
-
it('should log using console when no external logger is provided', () => {
|
|
12
|
-
const logger = new SimpleLogger();
|
|
13
|
-
logger.info('test message');
|
|
14
|
-
expect(consoleSpy).toHaveBeenCalledOnce();
|
|
15
|
-
expect(consoleSpy.mock.calls[0][0]).toMatch(/test message/);
|
|
16
|
-
});
|
|
17
|
-
it('should not log below current level', () => {
|
|
18
|
-
const logger = new SimpleLogger(null, { level: 'warn' });
|
|
19
|
-
logger.info('should not log');
|
|
20
|
-
logger.warn('should log');
|
|
21
|
-
expect(consoleSpy).toHaveBeenCalledOnce();
|
|
22
|
-
expect(consoleSpy.mock.calls[0][0]).toMatch(/should log/);
|
|
23
|
-
});
|
|
24
|
-
it('should respect msgPrefix', () => {
|
|
25
|
-
const logger = new SimpleLogger(null, { msgPrefix: '[PREFIX]' });
|
|
26
|
-
logger.info('message');
|
|
27
|
-
expect(consoleSpy.mock.calls[0][0]).toMatch(/\[PREFIX\]/);
|
|
28
|
-
});
|
|
29
|
-
it('should use external logger methods if provided', () => {
|
|
30
|
-
const external = {
|
|
31
|
-
info: vi.fn(),
|
|
32
|
-
level: 'info',
|
|
33
|
-
};
|
|
34
|
-
const logger = new SimpleLogger(external);
|
|
35
|
-
logger.info('external log');
|
|
36
|
-
expect(external.info).toHaveBeenCalledOnce();
|
|
37
|
-
expect(external.info).toHaveBeenCalledWith('external log');
|
|
38
|
-
});
|
|
39
|
-
it('should fallback to console for invalid external logger', () => {
|
|
40
|
-
const logger = new SimpleLogger({});
|
|
41
|
-
logger.info('fallback');
|
|
42
|
-
expect(consoleSpy).toHaveBeenCalled();
|
|
43
|
-
});
|
|
44
|
-
it('should create child logger with merged bindings', () => {
|
|
45
|
-
const logger = new SimpleLogger(null, { bindings: { user: 'A' } });
|
|
46
|
-
const child = logger.child({ request: '123' });
|
|
47
|
-
child.info('child message');
|
|
48
|
-
expect(consoleSpy.mock.calls.length).toBe(1);
|
|
49
|
-
const output = consoleSpy.mock.calls[0][0];
|
|
50
|
-
expect(output).toMatch(/user=A/);
|
|
51
|
-
expect(output).toMatch(/request=123/);
|
|
52
|
-
expect(output).toMatch(/child message/);
|
|
53
|
-
});
|
|
54
|
-
it('should call .child if external logger supports it', () => {
|
|
55
|
-
const childFn = vi.fn(() => ({
|
|
56
|
-
info: vi.fn(),
|
|
57
|
-
level: 'info',
|
|
58
|
-
}));
|
|
59
|
-
const external = { level: 'info', child: childFn, info: vi.fn() };
|
|
60
|
-
const logger = new SimpleLogger(external);
|
|
61
|
-
const child = logger.child({ traceId: 'xyz' });
|
|
62
|
-
child.info('from child');
|
|
63
|
-
expect(childFn).toHaveBeenCalledWith({ traceId: 'xyz' });
|
|
64
|
-
});
|
|
65
|
-
});
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import SimpleLogger from '../classes/simple-logger.js';
|
|
3
|
+
describe('SimpleLogger', () => {
|
|
4
|
+
let consoleSpy;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
7
|
+
});
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
consoleSpy.mockRestore();
|
|
10
|
+
});
|
|
11
|
+
it('should log using console when no external logger is provided', () => {
|
|
12
|
+
const logger = new SimpleLogger();
|
|
13
|
+
logger.info('test message');
|
|
14
|
+
expect(consoleSpy).toHaveBeenCalledOnce();
|
|
15
|
+
expect(consoleSpy.mock.calls[0][0]).toMatch(/test message/);
|
|
16
|
+
});
|
|
17
|
+
it('should not log below current level', () => {
|
|
18
|
+
const logger = new SimpleLogger(null, { level: 'warn' });
|
|
19
|
+
logger.info('should not log');
|
|
20
|
+
logger.warn('should log');
|
|
21
|
+
expect(consoleSpy).toHaveBeenCalledOnce();
|
|
22
|
+
expect(consoleSpy.mock.calls[0][0]).toMatch(/should log/);
|
|
23
|
+
});
|
|
24
|
+
it('should respect msgPrefix', () => {
|
|
25
|
+
const logger = new SimpleLogger(null, { msgPrefix: '[PREFIX]' });
|
|
26
|
+
logger.info('message');
|
|
27
|
+
expect(consoleSpy.mock.calls[0][0]).toMatch(/\[PREFIX\]/);
|
|
28
|
+
});
|
|
29
|
+
it('should use external logger methods if provided', () => {
|
|
30
|
+
const external = {
|
|
31
|
+
info: vi.fn(),
|
|
32
|
+
level: 'info',
|
|
33
|
+
};
|
|
34
|
+
const logger = new SimpleLogger(external);
|
|
35
|
+
logger.info('external log');
|
|
36
|
+
expect(external.info).toHaveBeenCalledOnce();
|
|
37
|
+
expect(external.info).toHaveBeenCalledWith('external log');
|
|
38
|
+
});
|
|
39
|
+
it('should fallback to console for invalid external logger', () => {
|
|
40
|
+
const logger = new SimpleLogger({});
|
|
41
|
+
logger.info('fallback');
|
|
42
|
+
expect(consoleSpy).toHaveBeenCalled();
|
|
43
|
+
});
|
|
44
|
+
it('should create child logger with merged bindings', () => {
|
|
45
|
+
const logger = new SimpleLogger(null, { bindings: { user: 'A' } });
|
|
46
|
+
const child = logger.child({ request: '123' });
|
|
47
|
+
child.info('child message');
|
|
48
|
+
expect(consoleSpy.mock.calls.length).toBe(1);
|
|
49
|
+
const output = consoleSpy.mock.calls[0][0];
|
|
50
|
+
expect(output).toMatch(/user=A/);
|
|
51
|
+
expect(output).toMatch(/request=123/);
|
|
52
|
+
expect(output).toMatch(/child message/);
|
|
53
|
+
});
|
|
54
|
+
it('should call .child if external logger supports it', () => {
|
|
55
|
+
const childFn = vi.fn(() => ({
|
|
56
|
+
info: vi.fn(),
|
|
57
|
+
level: 'info',
|
|
58
|
+
}));
|
|
59
|
+
const external = { level: 'info', child: childFn, info: vi.fn() };
|
|
60
|
+
const logger = new SimpleLogger(external);
|
|
61
|
+
const child = logger.child({ traceId: 'xyz' });
|
|
62
|
+
child.info('from child');
|
|
63
|
+
expect(childFn).toHaveBeenCalledWith({ traceId: 'xyz' });
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
66
|
//# sourceMappingURL=simple-logger.test.js.map
|
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A module for simple logging with various logging levels.
|
|
3
|
-
* @module SimpleLogger
|
|
4
|
-
*/
|
|
5
|
-
import { ExternalLogger, LogFn, LoggerFactory, LoggerLevel, LoggerOptions } from '../types/simple-logger.types.js';
|
|
6
|
-
export default class SimpleLogger {
|
|
7
|
-
private currentLevelIndex;
|
|
8
|
-
private logger;
|
|
9
|
-
private bindings;
|
|
10
|
-
private msgPrefix?;
|
|
11
|
-
private enabled;
|
|
12
|
-
isExternal: boolean;
|
|
13
|
-
static readonly noop: LogFn;
|
|
14
|
-
trace: LogFn;
|
|
15
|
-
debug: LogFn;
|
|
16
|
-
info: LogFn;
|
|
17
|
-
warn: LogFn;
|
|
18
|
-
error: LogFn;
|
|
19
|
-
fatal: LogFn;
|
|
20
|
-
silent: LogFn;
|
|
21
|
-
/**
|
|
22
|
-
* Initializes a new instance of the SimpleLogger class.
|
|
23
|
-
*
|
|
24
|
-
* @param {ExternalLogger | LoggerFactory | null} externalLogger - The external logger to use, or null to use the console.
|
|
25
|
-
* @param {LoggerOptions} [options={}] - Options for the logger, such as the logging level and bindings.
|
|
26
|
-
*/
|
|
27
|
-
constructor(externalLogger?: ExternalLogger | LoggerFactory | null, options?: LoggerOptions);
|
|
28
|
-
/**
|
|
29
|
-
* Resolves the effective logging level based on the provided logger and explicit level.
|
|
30
|
-
*
|
|
31
|
-
* @param {ExternalLogger | Console} logger - The logger to resolve the level for.
|
|
32
|
-
* @param {LoggerLevel} [explicitLevel] - The explicit logging level to use, if provided.
|
|
33
|
-
* @return {LoggerLevel} The resolved effective logging level.
|
|
34
|
-
*/
|
|
35
|
-
private resolveEffectiveLevel;
|
|
36
|
-
/**
|
|
37
|
-
* Initializes the logger with the provided external logger and options.
|
|
38
|
-
*
|
|
39
|
-
* Handles different cases for the external logger, including:
|
|
40
|
-
* - No external logger provided (falls back to console)
|
|
41
|
-
* - External logger is a factory function (e.g. pino)
|
|
42
|
-
* - External logger is invalid or console (falls back to console)
|
|
43
|
-
* - External logger is provided without parameters
|
|
44
|
-
* - External logger has a .child() method
|
|
45
|
-
* - External logger does not have a .child() method (uses directly)
|
|
46
|
-
*
|
|
47
|
-
* @param {ExternalLogger | LoggerFactory | null} externalLogger - The external logger to use, or null to use the console.
|
|
48
|
-
* @param {LoggerOptions} options - Options for the logger, such as the logging level and bindings.
|
|
49
|
-
* @return {void}
|
|
50
|
-
*/
|
|
51
|
-
private initLogger;
|
|
52
|
-
/**
|
|
53
|
-
* Updates the logging methods based on the current logging level and external logger configuration.
|
|
54
|
-
*
|
|
55
|
-
* Iterates over the available logging levels and sets the corresponding logging method to either call the external log method or the internal pretty format log method, depending on the logger configuration.
|
|
56
|
-
*
|
|
57
|
-
* @return {void}
|
|
58
|
-
*/
|
|
59
|
-
private updateLoggingMethods;
|
|
60
|
-
/**
|
|
61
|
-
* Calls the external log method for the specified level with the provided arguments.
|
|
62
|
-
*
|
|
63
|
-
* @param {LoggerLevel} level - The logging level to use for the external log method.
|
|
64
|
-
* @param {unknown[]} args - The arguments to pass to the external log method.
|
|
65
|
-
* @return {void}
|
|
66
|
-
*/
|
|
67
|
-
private callExternalLogMethod;
|
|
68
|
-
/**
|
|
69
|
-
* Formats and logs a message with the specified level and arguments.
|
|
70
|
-
*
|
|
71
|
-
* @param {LoggerLevel} level - The logging level to use for the log message.
|
|
72
|
-
* @param {unknown[]} args - The arguments to use when formatting the log message.
|
|
73
|
-
* @return {void}
|
|
74
|
-
*/
|
|
75
|
-
private prettyFormatLog;
|
|
76
|
-
/**
|
|
77
|
-
* Retrieves the current logging level.
|
|
78
|
-
*
|
|
79
|
-
* @return {LoggerLevel} The current logging level.
|
|
80
|
-
*/
|
|
81
|
-
getLevel(): LoggerLevel;
|
|
82
|
-
/**
|
|
83
|
-
* Sets the logging level.
|
|
84
|
-
*
|
|
85
|
-
* @param {LoggerLevel} level - The logging level to set.
|
|
86
|
-
* @return {void}
|
|
87
|
-
*/
|
|
88
|
-
setLevel(level: LoggerLevel): void;
|
|
89
|
-
/**
|
|
90
|
-
* Enables the logger.
|
|
91
|
-
*
|
|
92
|
-
* @return {void}
|
|
93
|
-
*/
|
|
94
|
-
enable(): void;
|
|
95
|
-
/**
|
|
96
|
-
* Disables the logger.
|
|
97
|
-
*
|
|
98
|
-
* @return {void} Nothing is returned.
|
|
99
|
-
*/
|
|
100
|
-
disable(): void;
|
|
101
|
-
/**
|
|
102
|
-
* Creates a new child logger with the merged bindings.
|
|
103
|
-
*
|
|
104
|
-
* If the external logger supports the `child` method, it will be used to create the child logger.
|
|
105
|
-
* Otherwise, a new SimpleLogger instance will be created with the merged bindings.
|
|
106
|
-
*
|
|
107
|
-
* @param {Record<string, unknown>} newBindings - The new bindings to merge with the existing bindings.
|
|
108
|
-
* @return {SimpleLogger} The new child logger instance.
|
|
109
|
-
*/
|
|
110
|
-
child(newBindings: Record<string, unknown>): SimpleLogger;
|
|
111
|
-
/**
|
|
112
|
-
* Logs the start of a function.
|
|
113
|
-
*
|
|
114
|
-
* @param {string} name - Optional function name to log. If not provided, the caller function name will be used.
|
|
115
|
-
* @return {void}
|
|
116
|
-
*/
|
|
117
|
-
logFunctionStart(name?: string): void;
|
|
118
|
-
/**
|
|
119
|
-
* Logs the end of a function.
|
|
120
|
-
*
|
|
121
|
-
* @param {string} name - Optional function name to log. If not provided, the caller function name will be used.
|
|
122
|
-
* @return {void}
|
|
123
|
-
*/
|
|
124
|
-
logFunctionEnd(name?: string): void;
|
|
125
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A module for simple logging with various logging levels.
|
|
3
|
+
* @module SimpleLogger
|
|
4
|
+
*/
|
|
5
|
+
import { type ExternalLogger, type LogFn, type LoggerFactory, type LoggerLevel, type LoggerOptions } from '../types/simple-logger.types.js';
|
|
6
|
+
export default class SimpleLogger {
|
|
7
|
+
private currentLevelIndex;
|
|
8
|
+
private logger;
|
|
9
|
+
private bindings;
|
|
10
|
+
private msgPrefix?;
|
|
11
|
+
private enabled;
|
|
12
|
+
isExternal: boolean;
|
|
13
|
+
static readonly noop: LogFn;
|
|
14
|
+
trace: LogFn;
|
|
15
|
+
debug: LogFn;
|
|
16
|
+
info: LogFn;
|
|
17
|
+
warn: LogFn;
|
|
18
|
+
error: LogFn;
|
|
19
|
+
fatal: LogFn;
|
|
20
|
+
silent: LogFn;
|
|
21
|
+
/**
|
|
22
|
+
* Initializes a new instance of the SimpleLogger class.
|
|
23
|
+
*
|
|
24
|
+
* @param {ExternalLogger | LoggerFactory | null} externalLogger - The external logger to use, or null to use the console.
|
|
25
|
+
* @param {LoggerOptions} [options={}] - Options for the logger, such as the logging level and bindings.
|
|
26
|
+
*/
|
|
27
|
+
constructor(externalLogger?: ExternalLogger | LoggerFactory | null, options?: LoggerOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Resolves the effective logging level based on the provided logger and explicit level.
|
|
30
|
+
*
|
|
31
|
+
* @param {ExternalLogger | Console} logger - The logger to resolve the level for.
|
|
32
|
+
* @param {LoggerLevel} [explicitLevel] - The explicit logging level to use, if provided.
|
|
33
|
+
* @return {LoggerLevel} The resolved effective logging level.
|
|
34
|
+
*/
|
|
35
|
+
private resolveEffectiveLevel;
|
|
36
|
+
/**
|
|
37
|
+
* Initializes the logger with the provided external logger and options.
|
|
38
|
+
*
|
|
39
|
+
* Handles different cases for the external logger, including:
|
|
40
|
+
* - No external logger provided (falls back to console)
|
|
41
|
+
* - External logger is a factory function (e.g. pino)
|
|
42
|
+
* - External logger is invalid or console (falls back to console)
|
|
43
|
+
* - External logger is provided without parameters
|
|
44
|
+
* - External logger has a .child() method
|
|
45
|
+
* - External logger does not have a .child() method (uses directly)
|
|
46
|
+
*
|
|
47
|
+
* @param {ExternalLogger | LoggerFactory | null} externalLogger - The external logger to use, or null to use the console.
|
|
48
|
+
* @param {LoggerOptions} options - Options for the logger, such as the logging level and bindings.
|
|
49
|
+
* @return {void}
|
|
50
|
+
*/
|
|
51
|
+
private initLogger;
|
|
52
|
+
/**
|
|
53
|
+
* Updates the logging methods based on the current logging level and external logger configuration.
|
|
54
|
+
*
|
|
55
|
+
* Iterates over the available logging levels and sets the corresponding logging method to either call the external log method or the internal pretty format log method, depending on the logger configuration.
|
|
56
|
+
*
|
|
57
|
+
* @return {void}
|
|
58
|
+
*/
|
|
59
|
+
private updateLoggingMethods;
|
|
60
|
+
/**
|
|
61
|
+
* Calls the external log method for the specified level with the provided arguments.
|
|
62
|
+
*
|
|
63
|
+
* @param {LoggerLevel} level - The logging level to use for the external log method.
|
|
64
|
+
* @param {unknown[]} args - The arguments to pass to the external log method.
|
|
65
|
+
* @return {void}
|
|
66
|
+
*/
|
|
67
|
+
private callExternalLogMethod;
|
|
68
|
+
/**
|
|
69
|
+
* Formats and logs a message with the specified level and arguments.
|
|
70
|
+
*
|
|
71
|
+
* @param {LoggerLevel} level - The logging level to use for the log message.
|
|
72
|
+
* @param {unknown[]} args - The arguments to use when formatting the log message.
|
|
73
|
+
* @return {void}
|
|
74
|
+
*/
|
|
75
|
+
private prettyFormatLog;
|
|
76
|
+
/**
|
|
77
|
+
* Retrieves the current logging level.
|
|
78
|
+
*
|
|
79
|
+
* @return {LoggerLevel} The current logging level.
|
|
80
|
+
*/
|
|
81
|
+
getLevel(): LoggerLevel;
|
|
82
|
+
/**
|
|
83
|
+
* Sets the logging level.
|
|
84
|
+
*
|
|
85
|
+
* @param {LoggerLevel} level - The logging level to set.
|
|
86
|
+
* @return {void}
|
|
87
|
+
*/
|
|
88
|
+
setLevel(level: LoggerLevel): void;
|
|
89
|
+
/**
|
|
90
|
+
* Enables the logger.
|
|
91
|
+
*
|
|
92
|
+
* @return {void}
|
|
93
|
+
*/
|
|
94
|
+
enable(): void;
|
|
95
|
+
/**
|
|
96
|
+
* Disables the logger.
|
|
97
|
+
*
|
|
98
|
+
* @return {void} Nothing is returned.
|
|
99
|
+
*/
|
|
100
|
+
disable(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Creates a new child logger with the merged bindings.
|
|
103
|
+
*
|
|
104
|
+
* If the external logger supports the `child` method, it will be used to create the child logger.
|
|
105
|
+
* Otherwise, a new SimpleLogger instance will be created with the merged bindings.
|
|
106
|
+
*
|
|
107
|
+
* @param {Record<string, unknown>} newBindings - The new bindings to merge with the existing bindings.
|
|
108
|
+
* @return {SimpleLogger} The new child logger instance.
|
|
109
|
+
*/
|
|
110
|
+
child(newBindings: Record<string, unknown>): SimpleLogger;
|
|
111
|
+
/**
|
|
112
|
+
* Logs the start of a function.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} name - Optional function name to log. If not provided, the caller function name will be used.
|
|
115
|
+
* @return {void}
|
|
116
|
+
*/
|
|
117
|
+
logFunctionStart(name?: string): void;
|
|
118
|
+
/**
|
|
119
|
+
* Logs the end of a function.
|
|
120
|
+
*
|
|
121
|
+
* @param {string} name - Optional function name to log. If not provided, the caller function name will be used.
|
|
122
|
+
* @return {void}
|
|
123
|
+
*/
|
|
124
|
+
logFunctionEnd(name?: string): void;
|
|
125
|
+
}
|