@vvlad1973/simple-logger 1.2.2 → 2.0.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/CHANGELOG.md +149 -0
- package/README.md +91 -41
- package/dist/__test__/simple-logger.test.js +54 -101
- package/dist/__test__/simple-logger.test.js.map +1 -1
- package/dist/classes/simple-logger.d.ts +125 -0
- package/dist/classes/simple-logger.js +290 -0
- package/dist/classes/simple-logger.js.map +1 -0
- package/dist/constants/constants.d.ts +9 -0
- package/dist/constants/constants.js +10 -0
- package/dist/constants/constants.js.map +1 -0
- package/dist/helpers/helpers.d.ts +31 -0
- package/dist/helpers/helpers.js +81 -0
- package/dist/helpers/helpers.js.map +1 -0
- package/dist/helpers/validators.d.ts +17 -0
- package/dist/helpers/validators.js +27 -0
- package/dist/helpers/validators.js.map +1 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/types/simple-logger.types.d.ts +27 -0
- package/dist/types/simple-logger.types.js +2 -0
- package/dist/types/simple-logger.types.js.map +1 -0
- package/docs/assets/highlight.css +9 -2
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/SimpleLogger.html +27 -15
- package/docs/index.html +51 -22
- package/docs/interfaces/ExternalLogger.html +10 -0
- package/docs/interfaces/LogFn.html +1 -0
- package/docs/interfaces/LoggerOptions.html +5 -0
- package/docs/modules.html +1 -1
- package/docs/types/LoggerFactory.html +1 -0
- package/docs/types/LoggerLevel.html +1 -1
- package/docs/types/PreparedLogCall.html +1 -0
- package/docs/variables/LoggerLevels.html +1 -0
- package/package.json +20 -7
- package/src/__test__/simple-logger.test.ts +67 -55
- package/src/classes/simple-logger.ts +354 -0
- package/src/constants/constants.ts +9 -0
- package/src/helpers/helpers.ts +92 -0
- package/src/helpers/validators.ts +36 -0
- package/src/index.ts +7 -2
- package/src/types/simple-logger.types.ts +40 -0
- package/docs/media/index.html +0 -39
- package/docs/types/ExternalLogger.html +0 -1
- package/jsdoc.json +0 -14
- package/src/simple-logger.ts +0 -190
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [2.0.2](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v2.0.1...v2.0.2)
|
|
4
|
+
|
|
5
|
+
### Fixes
|
|
6
|
+
|
|
7
|
+
- **fix**: Fixed exports of types (2025-05-04) [`b30a2a592dd11af6df352403f48e8b63144ef4af`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/b30a2a592dd11af6df352403f48e8b63144ef4af)
|
|
8
|
+
|
|
9
|
+
## [2.0.1](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v2.0.0...v2.0.1)
|
|
10
|
+
|
|
11
|
+
### Documentation
|
|
12
|
+
|
|
13
|
+
- **docs**: Documentation has been updated (2025-05-04) [`b902f966507fe2e114ba4021e67ce011b24566fb`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/b902f966507fe2e114ba4021e67ce011b24566fb)
|
|
14
|
+
|
|
15
|
+
## [2.0.0](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.2.2...v2.0.0)
|
|
16
|
+
|
|
17
|
+
### ⚠️ Breaking Changes
|
|
18
|
+
|
|
19
|
+
- **feat**: Added .child() method, support creating child instances of logger with different bindings and message prefixes (2025-05-04) [`2f9591c48007bc29e441c32fc1480353a691ec47`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/2f9591c48007bc29e441c32fc1480353a691ec47)
|
|
20
|
+
|
|
21
|
+
Changed signature of constructor of the class
|
|
22
|
+
|
|
23
|
+
### Documentation
|
|
24
|
+
|
|
25
|
+
- **docs**: Documentation has been updated (2025-05-04) [`7599a27dc0a31f1418ee920cb8d341f16c062334`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/7599a27dc0a31f1418ee920cb8d341f16c062334)
|
|
26
|
+
|
|
27
|
+
Changed signature of constructor of the class
|
|
28
|
+
|
|
29
|
+
## [1.2.2](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.2.1...v1.2.2)
|
|
30
|
+
|
|
31
|
+
### Documentation
|
|
32
|
+
|
|
33
|
+
- **docs**: Docs updated (2025-03-03) [`ec39cdecd7e2c8b7b070e301c89ed492779f235c`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/ec39cdecd7e2c8b7b070e301c89ed492779f235c)
|
|
34
|
+
|
|
35
|
+
## [1.2.1](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.2.0...v1.2.1)
|
|
36
|
+
|
|
37
|
+
### Documentation
|
|
38
|
+
|
|
39
|
+
- **docs**: Moved to TypeDoc, generated docs (2025-03-03) [`ca09fad9642b2a2327f133cb360432d87ae588a9`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/ca09fad9642b2a2327f133cb360432d87ae588a9)
|
|
40
|
+
|
|
41
|
+
## [1.2.0](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.29...v1.2.0)
|
|
42
|
+
|
|
43
|
+
### Features
|
|
44
|
+
|
|
45
|
+
- **feat**(SimpleLogger): Added property isExternal (2024-08-06) [`06432b0bd0454f0297bbbb6886e9174d3cdfc7ab`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/06432b0bd0454f0297bbbb6886e9174d3cdfc7ab)
|
|
46
|
+
|
|
47
|
+
## [1.1.29](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.28...v1.1.29)
|
|
48
|
+
|
|
49
|
+
### Fixes
|
|
50
|
+
|
|
51
|
+
- **fix**(setExternalLogger()): Fixed bug with initialize log level (2024-07-28) [`4e678c4479e84ec11ffd34d505ff2e16c8d4b72e`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/4e678c4479e84ec11ffd34d505ff2e16c8d4b72e)
|
|
52
|
+
|
|
53
|
+
## [1.1.27](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.26...v1.1.27)
|
|
54
|
+
|
|
55
|
+
### Features
|
|
56
|
+
|
|
57
|
+
- **feat**: Add export LoggerLevel type (2024-07-28) [`51f2f44f1b7979ad3539e72b06d2c3ce76e94c6d`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/51f2f44f1b7979ad3539e72b06d2c3ce76e94c6d)
|
|
58
|
+
|
|
59
|
+
## [1.1.25](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.24...v1.1.25)
|
|
60
|
+
|
|
61
|
+
### Fixes
|
|
62
|
+
|
|
63
|
+
- **fix**(constructor): Changed initialization of level logging when the object creating (2024-07-28) [`2f0a7979fe30098bf2a211ead265fa09d770b4cf`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/2f0a7979fe30098bf2a211ead265fa09d770b4cf)
|
|
64
|
+
|
|
65
|
+
## [1.1.24](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.23...v1.1.24)
|
|
66
|
+
|
|
67
|
+
### Fixes
|
|
68
|
+
|
|
69
|
+
- **fix**(constructor): Changed initialization of level logging when the object creating (2024-07-28) [`85a8e9de3dd15fe18ede74c2654f39d9256095c5`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/85a8e9de3dd15fe18ede74c2654f39d9256095c5)
|
|
70
|
+
|
|
71
|
+
## [1.1.23](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.22...v1.1.23)
|
|
72
|
+
|
|
73
|
+
### Fixes
|
|
74
|
+
|
|
75
|
+
- **fix**(constructor): Changed initialization of level logging when the object creating (2024-07-28) [`cd9649314135f2e5f403a01372f19c13c6c94057`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/cd9649314135f2e5f403a01372f19c13c6c94057)
|
|
76
|
+
|
|
77
|
+
## [1.1.21](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.20...v1.1.21)
|
|
78
|
+
|
|
79
|
+
### Fixes
|
|
80
|
+
|
|
81
|
+
- **fix**(simple-logger.ys): Fixed bug in .setLevel() (2024-07-19) [`26a26901491fede98508d195612dacf6ab8dc694`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/26a26901491fede98508d195612dacf6ab8dc694)
|
|
82
|
+
|
|
83
|
+
## [1.1.20](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.19...v1.1.20)
|
|
84
|
+
|
|
85
|
+
### Fixes
|
|
86
|
+
|
|
87
|
+
- **fix**(simple-logger.ts): Fixed settings log level (2024-07-19) [`f50f4530b61e230a93d6e55a1c573ddbf8bd549e`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/f50f4530b61e230a93d6e55a1c573ddbf8bd549e)
|
|
88
|
+
|
|
89
|
+
## [1.1.19](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.18...v1.1.19)
|
|
90
|
+
|
|
91
|
+
### Fixes
|
|
92
|
+
|
|
93
|
+
- **fix**(simple-logger.ts): Fixed settings log level (2024-07-19) [`7e9f06b6b91f800727f1dd13e38cbb5ea578775a`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/7e9f06b6b91f800727f1dd13e38cbb5ea578775a)
|
|
94
|
+
|
|
95
|
+
## [1.1.18](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.17...v1.1.18)
|
|
96
|
+
|
|
97
|
+
### Fixes
|
|
98
|
+
|
|
99
|
+
- **fix**(simple-logger.ts): Fixed settings log level (2024-07-19) [`9027c4231b36998d32afd1b03c81a32828b2c91a`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/9027c4231b36998d32afd1b03c81a32828b2c91a)
|
|
100
|
+
|
|
101
|
+
## [1.1.16](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.15...v1.1.16)
|
|
102
|
+
|
|
103
|
+
### Fixes
|
|
104
|
+
|
|
105
|
+
- **fix**(simple-logger.ts): Fixed settings log level (2024-07-19) [`3af08379e662e9df7fbe89bf0b85d482efa83bf3`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/3af08379e662e9df7fbe89bf0b85d482efa83bf3)
|
|
106
|
+
|
|
107
|
+
## [1.1.14](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.13...v1.1.14)
|
|
108
|
+
|
|
109
|
+
### Fixes
|
|
110
|
+
|
|
111
|
+
- **fix**(simple-logger.ts): Fixed bug with set log lever for external logger (2024-07-18) [`a3a945b4220997611b706dd1ca7ef33bfbc2efd7`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/a3a945b4220997611b706dd1ca7ef33bfbc2efd7)
|
|
112
|
+
|
|
113
|
+
## [1.1.13](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.12...v1.1.13)
|
|
114
|
+
|
|
115
|
+
### Fixes
|
|
116
|
+
|
|
117
|
+
- **fix**(simple-logger.ts): Fixed bug with set log lever for external logger (2024-07-18) [`2f181e8d5ed4590411242896f7eda0709f4039df`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/2f181e8d5ed4590411242896f7eda0709f4039df)
|
|
118
|
+
|
|
119
|
+
## [1.1.11](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.10...v1.1.11)
|
|
120
|
+
|
|
121
|
+
### Fixes
|
|
122
|
+
|
|
123
|
+
- **fix**(simple-logger.ts): Delete debug console output messages (2024-07-05) [`fe98428c8393166e52c0bd9a60aeefb6a88d4f2c`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/fe98428c8393166e52c0bd9a60aeefb6a88d4f2c)
|
|
124
|
+
|
|
125
|
+
## [1.1.10](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.9...v1.1.10)
|
|
126
|
+
|
|
127
|
+
### Fixes
|
|
128
|
+
|
|
129
|
+
- **fix**(.getExternalLoggerMethod()): Minor fix debug info (2024-07-05) [`e81c536a567453e8404d3bb2e24a68e1f6c5ea2d`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/e81c536a567453e8404d3bb2e24a68e1f6c5ea2d)
|
|
130
|
+
|
|
131
|
+
## [1.1.9](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.8...v1.1.9)
|
|
132
|
+
|
|
133
|
+
### Fixes
|
|
134
|
+
|
|
135
|
+
- **fix**(.getExternalLoggerMethod()): Fixed console out (2024-07-05) [`d67ba19d547265bb78f3d8ad7dbc39a1ffdfe8ad`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/d67ba19d547265bb78f3d8ad7dbc39a1ffdfe8ad)
|
|
136
|
+
|
|
137
|
+
## [1.1.8](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.7...v1.1.8)
|
|
138
|
+
|
|
139
|
+
### Features
|
|
140
|
+
|
|
141
|
+
- **feat**(SimpleLogger): Added possibility to set fallback logging method in internal method .getExternalLoggerMethod# (2024-07-05) [`1b5d6dd74ace8a2daba95f0536ebbc6a3f18f7a7`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/1b5d6dd74ace8a2daba95f0536ebbc6a3f18f7a7)
|
|
142
|
+
|
|
143
|
+
Added private notation to #
|
|
144
|
+
|
|
145
|
+
## [1.1.6](https://github.com/vvlad1973/vvlad1973-simple-logger/compare/v1.1.5...v1.1.6)
|
|
146
|
+
|
|
147
|
+
### Fixes
|
|
148
|
+
|
|
149
|
+
- **fix**(index.ts): Fixed exports (2024-07-05) [`7441a0a0c7ffd0cfa802c43060996221060bab03`](https://github.com/vvlad1973/vvlad1973-simple-logger/commit/7441a0a0c7ffd0cfa802c43060996221060bab03)
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @vvlad1973/simple-logger
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A flexible and lightweight logger for Node.js with support for custom levels, bindings, child loggers, and external loggers like Pino.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,88 +10,138 @@ npm install @vvlad1973/simple-logger
|
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
-
|
|
13
|
+
- Log levels: trace, debug, info, warn, error, fatal, silent
|
|
14
|
+
- Pretty-printed logs to console
|
|
15
|
+
- Optional bindings/context injection (e.g. userId, requestId)
|
|
16
|
+
- Optional msgPrefix
|
|
17
|
+
- Integration with external loggers (e.g. Pino) or logger factories
|
|
18
|
+
- Automatic detection of external logger .child() support
|
|
19
|
+
- Supports creating child loggers with merged context
|
|
20
|
+
- Function entry/exit logging
|
|
15
21
|
|
|
16
22
|
## Usage
|
|
17
23
|
|
|
18
|
-
###
|
|
19
|
-
|
|
20
|
-
```javascript
|
|
24
|
+
### Basic usage
|
|
21
25
|
|
|
26
|
+
```ts
|
|
22
27
|
import SimpleLogger from '@vvlad1973/simple-logger';
|
|
23
28
|
|
|
24
|
-
const logger = new SimpleLogger(
|
|
29
|
+
const logger = new SimpleLogger(null, {
|
|
30
|
+
level: 'debug',
|
|
31
|
+
msgPrefix: '[MyApp]',
|
|
32
|
+
bindings: { service: 'auth' },
|
|
33
|
+
});
|
|
25
34
|
|
|
26
35
|
logger.debug('Debug message');
|
|
27
|
-
logger.info('
|
|
28
|
-
logger.warn('
|
|
29
|
-
logger.error('
|
|
30
|
-
logger.fatal('
|
|
36
|
+
logger.info({ userId: 123 }, 'User logged in');
|
|
37
|
+
logger.warn('Something might be wrong');
|
|
38
|
+
logger.error('Something is wrong');
|
|
39
|
+
logger.fatal('System failure');
|
|
31
40
|
```
|
|
32
41
|
|
|
33
42
|
### Integration with Pino
|
|
34
43
|
|
|
35
|
-
```
|
|
44
|
+
```ts
|
|
36
45
|
import pino from 'pino';
|
|
37
46
|
import SimpleLogger from '@vvlad1973/simple-logger';
|
|
38
47
|
|
|
39
|
-
// Initialize Pino logger
|
|
40
48
|
const pinoLogger = pino({
|
|
41
|
-
|
|
49
|
+
level: 'info'
|
|
42
50
|
});
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
const logger = new SimpleLogger(pinoLogger, {
|
|
53
|
+
bindings: { component: 'api' },
|
|
54
|
+
msgPrefix: '[API]',
|
|
55
|
+
}); // this will create child logger with merged bindings
|
|
46
56
|
|
|
47
|
-
logger.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
57
|
+
logger.info({ userId: 42 }, 'User fetched');
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Or you can use a factory function:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import pino from 'pino';
|
|
64
|
+
import SimpleLogger from '@vvlad1973/simple-logger';
|
|
65
|
+
|
|
66
|
+
const logger = new SimpleLogger(pino, {
|
|
67
|
+
bindings: { component: 'api' },
|
|
68
|
+
msgPrefix: '[API]',
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
logger.info({ userId: 42 }, 'User fetched');
|
|
52
72
|
```
|
|
53
73
|
|
|
54
74
|
## API
|
|
55
75
|
|
|
56
|
-
### new SimpleLogger(
|
|
76
|
+
### new SimpleLogger(externalLogger?: ExternalLogger | LoggerFactory, options?: LoggerOptions)
|
|
57
77
|
|
|
58
|
-
Creates a new instance
|
|
78
|
+
Creates a new logger instance.
|
|
59
79
|
|
|
60
|
-
|
|
61
|
-
- externalLogger (optional): External logger instance. Can be an object with methods (trace, debug, info, warn, error, fatal) or any other logger compatible with these methods.
|
|
80
|
+
Parameters:
|
|
62
81
|
|
|
63
|
-
|
|
82
|
+
- externalLogger – optional:
|
|
64
83
|
|
|
65
|
-
|
|
84
|
+
- An existing logger instance (pino, winston, etc.)
|
|
85
|
+
- A factory function (e.g. pino)
|
|
66
86
|
|
|
67
|
-
-
|
|
87
|
+
- options – optional:
|
|
68
88
|
|
|
69
|
-
|
|
89
|
+
- level: Logging level
|
|
90
|
+
- bindings: Default context
|
|
91
|
+
- msgPrefix: Prefix for messages
|
|
92
|
+
- transport: Optional transport (used if logger is a factory)
|
|
70
93
|
|
|
71
|
-
|
|
94
|
+
### logger.setLevel(level: LoggerLevel)
|
|
72
95
|
|
|
73
|
-
|
|
96
|
+
Sets the logging level.
|
|
74
97
|
|
|
75
|
-
###
|
|
98
|
+
### logger.getLevel(): LoggerLevel
|
|
76
99
|
|
|
77
|
-
|
|
100
|
+
Returns the current log level.
|
|
78
101
|
|
|
79
|
-
|
|
102
|
+
### logger.setExternalLogger(externalLogger, options?)
|
|
80
103
|
|
|
81
|
-
|
|
104
|
+
Replace the underlying logger with a new one and optionally provide options for bindings, prefix, etc.
|
|
105
|
+
|
|
106
|
+
### logger.child(bindings: Record<string, unknown>): SimpleLogger
|
|
107
|
+
|
|
108
|
+
Creates a child logger with merged bindings.
|
|
109
|
+
|
|
110
|
+
### logger.logFunctionStart(name?: string)
|
|
111
|
+
|
|
112
|
+
Logs the beginning of a function.
|
|
113
|
+
|
|
114
|
+
### logger.logFunctionEnd(name?: string)
|
|
82
115
|
|
|
83
116
|
Logs the end of a function.
|
|
84
117
|
|
|
85
|
-
|
|
118
|
+
### Logging methods
|
|
119
|
+
|
|
120
|
+
Each of the following is available as a method:
|
|
121
|
+
|
|
122
|
+
- trace
|
|
123
|
+
- debug
|
|
124
|
+
- info
|
|
125
|
+
- warn
|
|
126
|
+
- error
|
|
127
|
+
- fatal
|
|
128
|
+
- silent (noop)
|
|
129
|
+
|
|
130
|
+
Each method accepts:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
logger.info(message: string, ...args: unknown[]);
|
|
134
|
+
logger.info(context: object, message?: string, ...args: unknown[]);
|
|
135
|
+
```
|
|
86
136
|
|
|
87
|
-
|
|
137
|
+
## Documentation
|
|
88
138
|
|
|
89
|
-
|
|
139
|
+
More details are available in the ./docs directory.
|
|
90
140
|
|
|
91
|
-
|
|
141
|
+
## License
|
|
92
142
|
|
|
93
|
-
|
|
143
|
+
MIT License (including commercial use)
|
|
94
144
|
|
|
95
145
|
## Author
|
|
96
146
|
|
|
97
|
-
Vladislav Vnukovskiy <vvlad1973@gmail.com>
|
|
147
|
+
Vladislav Vnukovskiy <[vvlad1973@gmail.com](mailto:vvlad1973@gmail.com)>
|
|
@@ -1,113 +1,66 @@
|
|
|
1
|
-
import { describe, it, beforeEach } from '
|
|
2
|
-
import
|
|
3
|
-
import SimpleLogger from '../simple-logger.js';
|
|
4
|
-
function createMockLoggerMethod() {
|
|
5
|
-
const calls = [];
|
|
6
|
-
const mockFn = function (...args) {
|
|
7
|
-
calls.push(args);
|
|
8
|
-
};
|
|
9
|
-
mockFn.mock = { calls };
|
|
10
|
-
return mockFn;
|
|
11
|
-
}
|
|
12
|
-
function createNoopMethod() {
|
|
13
|
-
const calls = [];
|
|
14
|
-
const mockFn = function (...args) { };
|
|
15
|
-
mockFn.mock = { calls };
|
|
16
|
-
return mockFn;
|
|
17
|
-
}
|
|
18
|
-
function createExternalLoggerMock() {
|
|
19
|
-
return {
|
|
20
|
-
trace: createMockLoggerMethod(),
|
|
21
|
-
debug: createMockLoggerMethod(),
|
|
22
|
-
info: createMockLoggerMethod(),
|
|
23
|
-
warn: createMockLoggerMethod(),
|
|
24
|
-
error: createMockLoggerMethod(),
|
|
25
|
-
fatal: createMockLoggerMethod(),
|
|
26
|
-
silent: createMockLoggerMethod(),
|
|
27
|
-
};
|
|
28
|
-
}
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import SimpleLogger from '../classes/simple-logger.js';
|
|
29
3
|
describe('SimpleLogger', () => {
|
|
30
|
-
let
|
|
31
|
-
let externalLoggerMock;
|
|
4
|
+
let consoleSpy;
|
|
32
5
|
beforeEach(() => {
|
|
33
|
-
|
|
34
|
-
logger = new SimpleLogger('info', externalLoggerMock);
|
|
6
|
+
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
35
7
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
logger.debug('debug message');
|
|
39
|
-
logger.info('info message');
|
|
40
|
-
logger.warn('warn message');
|
|
41
|
-
logger.error('error message');
|
|
42
|
-
logger.fatal('fatal message');
|
|
43
|
-
assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 0);
|
|
44
|
-
assert.strictEqual(externalLoggerMock.debug.mock.calls.length, 0);
|
|
45
|
-
assert.strictEqual(externalLoggerMock.info.mock.calls.length, 1);
|
|
46
|
-
assert.strictEqual(externalLoggerMock.info.mock.calls[0][0], 'info message');
|
|
47
|
-
assert.strictEqual(externalLoggerMock.warn.mock.calls.length, 1);
|
|
48
|
-
assert.strictEqual(externalLoggerMock.warn.mock.calls[0][0], 'warn message');
|
|
49
|
-
assert.strictEqual(externalLoggerMock.error.mock.calls.length, 1);
|
|
50
|
-
assert.strictEqual(externalLoggerMock.error.mock.calls[0][0], 'error message');
|
|
51
|
-
assert.strictEqual(externalLoggerMock.fatal.mock.calls.length, 1);
|
|
52
|
-
assert.strictEqual(externalLoggerMock.fatal.mock.calls[0][0], 'fatal message');
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
consoleSpy.mockRestore();
|
|
53
10
|
});
|
|
54
|
-
it('should log
|
|
55
|
-
logger
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
testFunction();
|
|
61
|
-
assert.strictEqual(externalLoggerMock.trace.mock.calls.length, 2);
|
|
62
|
-
assert.strictEqual(externalLoggerMock.trace.mock.calls[0][0], 'Function start: testFunction');
|
|
63
|
-
assert.strictEqual(externalLoggerMock.trace.mock.calls[1][0], 'Function end: testFunction');
|
|
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/);
|
|
64
16
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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\]/);
|
|
72
28
|
});
|
|
73
|
-
it('should use
|
|
74
|
-
const
|
|
75
|
-
|
|
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');
|
|
76
38
|
});
|
|
77
|
-
it('should
|
|
78
|
-
logger
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
assert.strictEqual(typeof logger.info, 'function');
|
|
39
|
+
it('should fallback to console for invalid external logger', () => {
|
|
40
|
+
const logger = new SimpleLogger({});
|
|
41
|
+
logger.info('fallback');
|
|
42
|
+
expect(consoleSpy).toHaveBeenCalled();
|
|
82
43
|
});
|
|
83
|
-
it('should
|
|
84
|
-
logger
|
|
85
|
-
logger.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
assert.strictEqual(externalLoggerMock.info.mock.calls.length, 1);
|
|
93
|
-
assert.strictEqual(externalLoggerMock.info.mock.calls[0][0], 'info message');
|
|
94
|
-
assert.strictEqual(externalLoggerMock.warn.mock.calls.length, 1);
|
|
95
|
-
assert.strictEqual(externalLoggerMock.warn.mock.calls[0][0], 'warn message');
|
|
96
|
-
assert.strictEqual(externalLoggerMock.error.mock.calls.length, 1);
|
|
97
|
-
assert.strictEqual(externalLoggerMock.error.mock.calls[0][0], 'error message');
|
|
98
|
-
assert.strictEqual(externalLoggerMock.fatal.mock.calls.length, 1);
|
|
99
|
-
assert.strictEqual(externalLoggerMock.fatal.mock.calls[0][0], 'fatal message');
|
|
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/);
|
|
100
53
|
});
|
|
101
|
-
it('should
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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' });
|
|
111
64
|
});
|
|
112
65
|
});
|
|
113
66
|
//# sourceMappingURL=simple-logger.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simple-logger.test.js","sourceRoot":"","sources":["../../src/__test__/simple-logger.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,
|
|
1
|
+
{"version":3,"file":"simple-logger.test.js","sourceRoot":"","sources":["../../src/__test__/simple-logger.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,E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