@darksheep/logger 1.0.0 โ 1.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 +22 -0
- package/package.json +5 -5
- package/src/create-logger.js +2 -3
- package/src/formatter.js +2 -2
- package/src/formatters/formatter-console.js +2 -1
- package/src/index.js +6 -6
- package/src/logger.js +189 -187
- package/src/replacer.js +1 -0
- package/src/replacers/index.js +1 -1
- package/src/utilities/environment.js +1 -1
- package/src/utilities/stacktrace.js +2 -1
- package/types/index.d.ts +3 -3
- package/types/logger.d.ts +67 -67
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.2](https://github.com/DarkSheepSoftware/node-packages/compare/logger-v1.0.1...logger-v1.0.2) (2024-08-01)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ๐ฆ Dependencies
|
|
7
|
+
|
|
8
|
+
* **dev:** update dependency eslint to ~9.8.0 ([#284](https://github.com/DarkSheepSoftware/node-packages/issues/284)) ([dc02a80](https://github.com/DarkSheepSoftware/node-packages/commit/dc02a801cb982cd3ed2bf685db62a21b6c2c15cd))
|
|
9
|
+
|
|
10
|
+
## [1.0.1](https://github.com/DarkSheepSoftware/node-packages/compare/logger-v1.0.0...logger-v1.0.1) (2024-07-23)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ๐ฆ Dependencies
|
|
14
|
+
|
|
15
|
+
* **dev:** update dependency @darksheep/eslint to v5 ([25aabab](https://github.com/DarkSheepSoftware/node-packages/commit/25aababd9295e3e47b945c91b5d6fbf121f8168b))
|
|
16
|
+
* **dev:** update dependency @types/eslint to v9 ([#271](https://github.com/DarkSheepSoftware/node-packages/issues/271)) ([6600b26](https://github.com/DarkSheepSoftware/node-packages/commit/6600b267a99b0e320b52c78d2727f0576b0fb027))
|
|
17
|
+
* **dev:** update dependency eslint to ~9.7.0 ([#263](https://github.com/DarkSheepSoftware/node-packages/issues/263)) ([af98723](https://github.com/DarkSheepSoftware/node-packages/commit/af9872336b01026496bc1a48807fd396eb361483))
|
|
18
|
+
* **pkg:** update dependency deepmerge-ts to v7.1.0 ([#269](https://github.com/DarkSheepSoftware/node-packages/issues/269)) ([3123ead](https://github.com/DarkSheepSoftware/node-packages/commit/3123ead700e145ba35228d44ff7847b11764e15d))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### ๐งน Miscellaneous Chores
|
|
22
|
+
|
|
23
|
+
* eslint --fix . ([48b5728](https://github.com/DarkSheepSoftware/node-packages/commit/48b5728b6cd0bcd2ac2f8ec04f45d155c0682dc8))
|
|
24
|
+
|
|
3
25
|
## 1.0.0 (2024-07-02)
|
|
4
26
|
|
|
5
27
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darksheep/logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Logging your stuff to where ever you want it",
|
|
5
5
|
"license": "UNLICENCED",
|
|
6
6
|
"type": "module",
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"test:watch": "node --watch --test src/**/*.test.js"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"deepmerge-ts": "7.0
|
|
27
|
+
"deepmerge-ts": "7.1.0",
|
|
28
28
|
"stacktrace-parser": "0.1.10",
|
|
29
29
|
"traverse": "0.6.9"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@darksheep/eslint": "~
|
|
33
|
-
"@types/eslint": "~
|
|
32
|
+
"@darksheep/eslint": "~5.1.0",
|
|
33
|
+
"@types/eslint": "~9.6.0",
|
|
34
34
|
"@types/node": "~20.14.0",
|
|
35
35
|
"@types/traverse": "~0.6.33",
|
|
36
|
-
"eslint": "~9.
|
|
36
|
+
"eslint": "~9.8.0",
|
|
37
37
|
"nock": "~13.5.0",
|
|
38
38
|
"typescript": "~5.5.0",
|
|
39
39
|
"zod": "~3.23.0"
|
package/src/create-logger.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { Logger } from './logger.js';
|
|
2
|
-
|
|
3
2
|
import {
|
|
4
|
-
SecretDelete,
|
|
5
|
-
SecretObscure,
|
|
6
3
|
BufferReplacer,
|
|
7
4
|
ErrorReplacer,
|
|
8
5
|
HttpClientRequestReplacer,
|
|
@@ -10,6 +7,8 @@ import {
|
|
|
10
7
|
HttpServerResponseReplacer,
|
|
11
8
|
LongStringReplacer,
|
|
12
9
|
NetSocketReplacer,
|
|
10
|
+
SecretDelete,
|
|
11
|
+
SecretObscure,
|
|
13
12
|
} from './replacers/index.js';
|
|
14
13
|
|
|
15
14
|
/**
|
package/src/formatter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { environment } from './utilities/environment.js';
|
|
2
|
-
import { formatterJson } from './formatters/formatter-json.js';
|
|
3
1
|
import { formatterConsole } from './formatters/formatter-console.js';
|
|
2
|
+
import { formatterJson } from './formatters/formatter-json.js';
|
|
3
|
+
import { environment } from './utilities/environment.js';
|
|
4
4
|
|
|
5
5
|
const isConsole = (
|
|
6
6
|
process.stdout.isTTY === true &&
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
import { colourString, shouldColour } from '../utilities/colour.js';
|
|
4
|
+
import { LogNames } from '../utilities/log-types.js';
|
|
4
5
|
|
|
5
6
|
/** @typedef {import('../utilities/log-types.js').LogContext} LogContext */
|
|
6
7
|
/** @typedef {import('../utilities/log-types.js').LogEntry} LogEntry */
|
package/src/index.js
CHANGED
|
@@ -6,15 +6,11 @@ import { createLogger } from './create-logger.js';
|
|
|
6
6
|
/** @typedef {import('./formatter.js').Formatter} Formatter */
|
|
7
7
|
/** @typedef {import('./replacer.js').Replacer} Replacer */
|
|
8
8
|
|
|
9
|
-
export {
|
|
9
|
+
export { createLogger } from './create-logger.js';
|
|
10
10
|
export const logger = createLogger();
|
|
11
11
|
|
|
12
|
-
export {
|
|
13
|
-
export { createLogger } from './create-logger.js';
|
|
14
|
-
export { LogLevels } from './utilities/log-types.js';
|
|
12
|
+
export { Logger } from './logger.js';
|
|
15
13
|
export {
|
|
16
|
-
SecretDelete,
|
|
17
|
-
SecretObscure,
|
|
18
14
|
BufferReplacer,
|
|
19
15
|
ErrorReplacer,
|
|
20
16
|
HttpClientRequestReplacer,
|
|
@@ -22,4 +18,8 @@ export {
|
|
|
22
18
|
HttpServerResponseReplacer,
|
|
23
19
|
LongStringReplacer,
|
|
24
20
|
NetSocketReplacer,
|
|
21
|
+
SecretDelete,
|
|
22
|
+
SecretObscure,
|
|
25
23
|
} from './replacers/index.js';
|
|
24
|
+
export { environment } from './utilities/environment.js';
|
|
25
|
+
export { LogLevels } from './utilities/log-types.js';
|
package/src/logger.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import { deepmerge } from 'deepmerge-ts';
|
|
3
2
|
|
|
4
|
-
import {
|
|
5
|
-
import { getLastCallsite } from './utilities/last-callsite.js';
|
|
6
|
-
import { LogLevels } from './utilities/log-types.js';
|
|
7
|
-
import { checkLogLevel, checkLogFilters } from './utilities/log-filters.js';
|
|
3
|
+
import { deepmerge } from 'deepmerge-ts';
|
|
8
4
|
|
|
5
|
+
import { formatter } from './formatter.js';
|
|
9
6
|
import { replace } from './replacer.js';
|
|
10
7
|
import { ErrorReplacer } from './replacers/error.js';
|
|
11
8
|
import { stdoutWrite } from './stdout-write.js';
|
|
12
|
-
import {
|
|
9
|
+
import { environment } from './utilities/environment.js';
|
|
10
|
+
import { getLastCallsite } from './utilities/last-callsite.js';
|
|
11
|
+
import { checkLogFilters, checkLogLevel } from './utilities/log-filters.js';
|
|
12
|
+
import { LogLevels } from './utilities/log-types.js';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* @typedef {Object} Options
|
|
@@ -23,77 +23,20 @@ export class Logger {
|
|
|
23
23
|
/** @type {AsyncLocalStorage<import('./utilities/log-types.js').LogContext[]>} */
|
|
24
24
|
static storage = new AsyncLocalStorage();
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
|
|
28
|
-
* @overload
|
|
29
|
-
* @param {() => R} callback The function to wrap the context in
|
|
30
|
-
* @returns {R}
|
|
31
|
-
*/
|
|
32
|
-
/**
|
|
33
|
-
* @template [R=unknown]
|
|
34
|
-
* @overload
|
|
35
|
-
* @param {import('./utilities/log-types.js').LogContext} context Context to bind ot the async context
|
|
36
|
-
* @param {() => R} callback The function to wrap the context in
|
|
37
|
-
* @returns {R}
|
|
38
|
-
*/
|
|
39
|
-
/**
|
|
40
|
-
* @template [R=unknown]
|
|
41
|
-
* @param {unknown} context Context to bind ot the async context
|
|
42
|
-
* @param {unknown} [callback] The function to wrap the context in
|
|
43
|
-
* @returns {R}
|
|
44
|
-
*/
|
|
45
|
-
static wrap(context, callback) {
|
|
46
|
-
// If there is already some context, keep it around
|
|
47
|
-
const store = Logger.storage.getStore() ?? [];
|
|
48
|
-
|
|
49
|
-
if (typeof context === 'function') {
|
|
50
|
-
return Logger.storage.run(
|
|
51
|
-
[ ...store ],
|
|
52
|
-
/** @type {() => R} */ (context),
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return Logger.storage.run(
|
|
57
|
-
[ ...store, /** @type {import('./utilities/log-types.js').LogContext} */ (context) ],
|
|
58
|
-
/** @type {() => R} */ (callback),
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* @template [R=unknown]
|
|
64
|
-
* @param {() => R} callback The function to wrap the context in
|
|
65
|
-
* @returns {R}
|
|
66
|
-
*/
|
|
67
|
-
wrap(callback) {
|
|
68
|
-
return Logger.wrap(this.#context, callback);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @param {import('./utilities/log-types.js').LogContext} context Context to bind ot the async context
|
|
73
|
-
* @returns {void}
|
|
74
|
-
*/
|
|
75
|
-
static addAsyncContext(context) {
|
|
76
|
-
const store = Logger.storage.getStore();
|
|
77
|
-
|
|
78
|
-
if (store == null) {
|
|
79
|
-
throw new Error('Context not inside Context#wrap');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
store.push(context);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** @type {import('./replacer.js').Replacer[]} */
|
|
86
|
-
replacers = [];
|
|
26
|
+
/** @type {import('./utilities/log-types.js').LogContext} */
|
|
27
|
+
#context;
|
|
87
28
|
|
|
88
29
|
/** @type {typeof formatter} */
|
|
89
30
|
#format = formatter;
|
|
31
|
+
|
|
90
32
|
/** @type {typeof replace} */
|
|
91
33
|
#replace = replace;
|
|
34
|
+
|
|
92
35
|
/** @type {typeof stdoutWrite} */
|
|
93
36
|
#write = stdoutWrite;
|
|
94
|
-
/** @type {import('./utilities/log-types.js').LogContext} */
|
|
95
|
-
#context;
|
|
96
37
|
|
|
38
|
+
/** @type {import('./replacer.js').Replacer[]} */
|
|
39
|
+
replacers = [];
|
|
97
40
|
/**
|
|
98
41
|
* @param {import('./utilities/log-types.js').LogContext} [context] Context to add to the logger
|
|
99
42
|
* @param {Options} [options] Options for the loggers output
|
|
@@ -115,136 +58,55 @@ export class Logger {
|
|
|
115
58
|
}
|
|
116
59
|
|
|
117
60
|
/**
|
|
118
|
-
*
|
|
119
|
-
* @param {import('./utilities/log-types.js').LogLevel} level the log level of the message to log
|
|
120
|
-
* @param {Error | string} message The message to log
|
|
121
|
-
* @param {import('./utilities/log-types.js').LogContext} [context] Context to add to the log (merged with this.context, and Logger.contexts)
|
|
61
|
+
* @param {import('./utilities/log-types.js').LogContext} context Context to bind ot the async context
|
|
122
62
|
* @returns {void}
|
|
123
63
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const contexts = [];
|
|
127
|
-
|
|
128
|
-
const asyncContexts = Logger.storage.getStore();
|
|
129
|
-
if (asyncContexts instanceof Array) {
|
|
130
|
-
contexts.push(...asyncContexts);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
contexts.push(this.#context);
|
|
134
|
-
if (context instanceof Object) {
|
|
135
|
-
contexts.push(context);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const fullContext =
|
|
139
|
-
/** @type {import('./utilities/log-types.js').LogContext} */
|
|
140
|
-
(deepmerge(...contexts));
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
checkLogLevel(level) === false ||
|
|
144
|
-
checkLogFilters(fullContext?.channel) === false
|
|
145
|
-
) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** @type {import('./utilities/log-types.js').LogEntry} */
|
|
150
|
-
const entry = (
|
|
151
|
-
typeof message === 'string'
|
|
152
|
-
? { ...fullContext, message, level }
|
|
153
|
-
: { ...fullContext, ...ErrorReplacer.replace(message), level }
|
|
154
|
-
);
|
|
64
|
+
static addAsyncContext(context) {
|
|
65
|
+
const store = Logger.storage.getStore();
|
|
155
66
|
|
|
156
|
-
if (
|
|
157
|
-
|
|
67
|
+
if (store == null) {
|
|
68
|
+
throw new Error('Context not inside Context#wrap');
|
|
158
69
|
}
|
|
159
70
|
|
|
160
|
-
|
|
161
|
-
/** @type {import('./utilities/log-types.js').LogEntry} */
|
|
162
|
-
(this.#replace(entry, this.replacers));
|
|
163
|
-
|
|
164
|
-
const formatted = this.#format(normalised);
|
|
165
|
-
|
|
166
|
-
return this.#write(formatted);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// =========
|
|
170
|
-
// Level
|
|
171
|
-
// =========
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Write a log with the level 'critical' - A crucial part of the application is not working
|
|
175
|
-
*
|
|
176
|
-
* @param {Error | string} message The message to log
|
|
177
|
-
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
178
|
-
* @returns {void}
|
|
179
|
-
*/
|
|
180
|
-
critical(message, context) {
|
|
181
|
-
return this.write(LogLevels.critical, message, context);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Write a log with the level 'error' - A non critical operation fails
|
|
186
|
-
* @param {Error | string} message The message to log
|
|
187
|
-
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
188
|
-
* @returns {void}
|
|
189
|
-
*/
|
|
190
|
-
error(message, context) {
|
|
191
|
-
return this.write(LogLevels.error, message, context);
|
|
71
|
+
store.push(context);
|
|
192
72
|
}
|
|
193
73
|
|
|
194
74
|
/**
|
|
195
|
-
*
|
|
196
|
-
* @
|
|
197
|
-
* @param {
|
|
198
|
-
* @returns {
|
|
75
|
+
* @template [R=unknown]
|
|
76
|
+
* @overload
|
|
77
|
+
* @param {() => R} callback The function to wrap the context in
|
|
78
|
+
* @returns {R}
|
|
199
79
|
*/
|
|
200
|
-
warning(message, context) {
|
|
201
|
-
return this.write(LogLevels.warning, message, context);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
80
|
/**
|
|
205
|
-
*
|
|
206
|
-
* @
|
|
207
|
-
* @param {import('./utilities/log-types.js').LogContext}
|
|
208
|
-
* @
|
|
81
|
+
* @template [R=unknown]
|
|
82
|
+
* @overload
|
|
83
|
+
* @param {import('./utilities/log-types.js').LogContext} context Context to bind ot the async context
|
|
84
|
+
* @param {() => R} callback The function to wrap the context in
|
|
85
|
+
* @returns {R}
|
|
209
86
|
*/
|
|
210
|
-
notice(message, context) {
|
|
211
|
-
return this.write(LogLevels.notice, message, context);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
87
|
/**
|
|
215
|
-
*
|
|
216
|
-
* @param {
|
|
217
|
-
* @param {
|
|
218
|
-
* @returns {
|
|
88
|
+
* @template [R=unknown]
|
|
89
|
+
* @param {unknown} context Context to bind ot the async context
|
|
90
|
+
* @param {unknown} [callback] The function to wrap the context in
|
|
91
|
+
* @returns {R}
|
|
219
92
|
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
93
|
+
static wrap(context, callback) {
|
|
94
|
+
// If there is already some context, keep it around
|
|
95
|
+
const store = Logger.storage.getStore() ?? [];
|
|
223
96
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
debug(message, context) {
|
|
231
|
-
return this.write(LogLevels.debug, message, context);
|
|
232
|
-
}
|
|
97
|
+
if (typeof context === 'function') {
|
|
98
|
+
return Logger.storage.run(
|
|
99
|
+
[ ...store ],
|
|
100
|
+
/** @type {() => R} */ (context),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
233
103
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
* @returns {void}
|
|
239
|
-
*/
|
|
240
|
-
silly(message, context) {
|
|
241
|
-
return this.write(LogLevels.silly, message, context);
|
|
104
|
+
return Logger.storage.run(
|
|
105
|
+
[ ...store, /** @type {import('./utilities/log-types.js').LogContext} */ (context) ],
|
|
106
|
+
/** @type {() => R} */ (callback),
|
|
107
|
+
);
|
|
242
108
|
}
|
|
243
109
|
|
|
244
|
-
// =========
|
|
245
|
-
// Context
|
|
246
|
-
// =========
|
|
247
|
-
|
|
248
110
|
/**
|
|
249
111
|
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
250
112
|
* @param {import('./utilities/log-types.js').LogLevel} [level] The log level at which we allow this context
|
|
@@ -269,6 +131,19 @@ export class Logger {
|
|
|
269
131
|
);
|
|
270
132
|
}
|
|
271
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Set the channel in the new logger
|
|
136
|
+
* @param {string} channel The channel name
|
|
137
|
+
* @returns {Logger}
|
|
138
|
+
*/
|
|
139
|
+
channel(channel) {
|
|
140
|
+
return this.set('channel', channel);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// =========
|
|
144
|
+
// Level
|
|
145
|
+
// =========
|
|
146
|
+
|
|
272
147
|
/**
|
|
273
148
|
* Creating a new logger with {context, ...this.context}
|
|
274
149
|
* @overload
|
|
@@ -325,6 +200,37 @@ export class Logger {
|
|
|
325
200
|
throw new TypeError('Invalid LogContext in Logger#context()');
|
|
326
201
|
}
|
|
327
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Write a log with the level 'critical' - A crucial part of the application is not working
|
|
205
|
+
*
|
|
206
|
+
* @param {Error | string} message The message to log
|
|
207
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
208
|
+
* @returns {void}
|
|
209
|
+
*/
|
|
210
|
+
critical(message, context) {
|
|
211
|
+
return this.write(LogLevels.critical, message, context);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Write a log with the level 'debug' - Information that is unlikely to help in production
|
|
216
|
+
* @param {Error | string} message The message to log
|
|
217
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
218
|
+
* @returns {void}
|
|
219
|
+
*/
|
|
220
|
+
debug(message, context) {
|
|
221
|
+
return this.write(LogLevels.debug, message, context);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Write a log with the level 'error' - A non critical operation fails
|
|
226
|
+
* @param {Error | string} message The message to log
|
|
227
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
228
|
+
* @returns {void}
|
|
229
|
+
*/
|
|
230
|
+
error(message, context) {
|
|
231
|
+
return this.write(LogLevels.error, message, context);
|
|
232
|
+
}
|
|
233
|
+
|
|
328
234
|
/**
|
|
329
235
|
* Get a value from the context by key
|
|
330
236
|
* @param {string} key The key to get from the context
|
|
@@ -334,6 +240,30 @@ export class Logger {
|
|
|
334
240
|
return this.#context[key];
|
|
335
241
|
}
|
|
336
242
|
|
|
243
|
+
/**
|
|
244
|
+
* Write a log with the level 'info' - Information about successful operations
|
|
245
|
+
* @param {Error | string} message The message to log
|
|
246
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
247
|
+
* @returns {void}
|
|
248
|
+
*/
|
|
249
|
+
info(message, context) {
|
|
250
|
+
return this.write(LogLevels.info, message, context);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Write a log with the level 'notice' - Information about events that may be unusual
|
|
255
|
+
* @param {Error | string} message The message to log
|
|
256
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
257
|
+
* @returns {void}
|
|
258
|
+
*/
|
|
259
|
+
notice(message, context) {
|
|
260
|
+
return this.write(LogLevels.notice, message, context);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// =========
|
|
264
|
+
// Context
|
|
265
|
+
// =========
|
|
266
|
+
|
|
337
267
|
/**
|
|
338
268
|
* Set a value into a new context by key
|
|
339
269
|
* @param {string} key The key to set
|
|
@@ -345,12 +275,13 @@ export class Logger {
|
|
|
345
275
|
}
|
|
346
276
|
|
|
347
277
|
/**
|
|
348
|
-
*
|
|
349
|
-
* @param {string}
|
|
350
|
-
* @
|
|
278
|
+
* Write a log with the level 'silly' - Information to help resolve complex logic issues
|
|
279
|
+
* @param {Error | string} message The message to log
|
|
280
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
281
|
+
* @returns {void}
|
|
351
282
|
*/
|
|
352
|
-
|
|
353
|
-
return this.
|
|
283
|
+
silly(message, context) {
|
|
284
|
+
return this.write(LogLevels.silly, message, context);
|
|
354
285
|
}
|
|
355
286
|
|
|
356
287
|
/**
|
|
@@ -366,4 +297,75 @@ export class Logger {
|
|
|
366
297
|
|
|
367
298
|
return this.set('trail', trail);
|
|
368
299
|
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Write a log with the level 'warning' - An operation might fail in the future
|
|
303
|
+
* @param {Error | string} message The message to log
|
|
304
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
305
|
+
* @returns {void}
|
|
306
|
+
*/
|
|
307
|
+
warning(message, context) {
|
|
308
|
+
return this.write(LogLevels.warning, message, context);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @template [R=unknown]
|
|
313
|
+
* @param {() => R} callback The function to wrap the context in
|
|
314
|
+
* @returns {R}
|
|
315
|
+
*/
|
|
316
|
+
wrap(callback) {
|
|
317
|
+
return Logger.wrap(this.#context, callback);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Write a new log entry
|
|
322
|
+
* @param {import('./utilities/log-types.js').LogLevel} level the log level of the message to log
|
|
323
|
+
* @param {Error | string} message The message to log
|
|
324
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Context to add to the log (merged with this.context, and Logger.contexts)
|
|
325
|
+
* @returns {void}
|
|
326
|
+
*/
|
|
327
|
+
write(level, message, context) {
|
|
328
|
+
/** @type {import('./utilities/log-types.js').LogContext[]} */
|
|
329
|
+
const contexts = [];
|
|
330
|
+
|
|
331
|
+
const asyncContexts = Logger.storage.getStore();
|
|
332
|
+
if (asyncContexts instanceof Array) {
|
|
333
|
+
contexts.push(...asyncContexts);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
contexts.push(this.#context);
|
|
337
|
+
if (context instanceof Object) {
|
|
338
|
+
contexts.push(context);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const fullContext =
|
|
342
|
+
/** @type {import('./utilities/log-types.js').LogContext} */
|
|
343
|
+
(deepmerge(...contexts));
|
|
344
|
+
|
|
345
|
+
if (
|
|
346
|
+
checkLogLevel(level) === false ||
|
|
347
|
+
checkLogFilters(fullContext?.channel) === false
|
|
348
|
+
) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** @type {import('./utilities/log-types.js').LogEntry} */
|
|
353
|
+
const entry = (
|
|
354
|
+
typeof message === 'string'
|
|
355
|
+
? { ...fullContext, message, level }
|
|
356
|
+
: { ...fullContext, ...ErrorReplacer.replace(message), level }
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
if (environment.includeCallsite === true) {
|
|
360
|
+
entry.source = getLastCallsite();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const normalised =
|
|
364
|
+
/** @type {import('./utilities/log-types.js').LogEntry} */
|
|
365
|
+
(this.#replace(entry, this.replacers));
|
|
366
|
+
|
|
367
|
+
const formatted = this.#format(normalised);
|
|
368
|
+
|
|
369
|
+
return this.#write(formatted);
|
|
370
|
+
}
|
|
369
371
|
}
|
package/src/replacer.js
CHANGED
package/src/replacers/index.js
CHANGED
|
@@ -4,5 +4,5 @@ export { HttpClientRequestReplacer } from './http-client-request.js';
|
|
|
4
4
|
export { HttpIncomingMessageReplacer } from './http-incoming-message.js';
|
|
5
5
|
export { HttpServerResponseReplacer } from './http-server-response.js';
|
|
6
6
|
export { LongStringReplacer } from './long-strings.js';
|
|
7
|
-
export { SecretDelete, SecretObscure } from './secrets.js';
|
|
8
7
|
export { NetSocketReplacer } from './net-socket.js';
|
|
8
|
+
export { SecretDelete, SecretObscure } from './secrets.js';
|
package/types/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createLogger } from "./create-logger.js";
|
|
2
2
|
export const logger: import("./logger.js").Logger;
|
|
3
|
+
export { Logger } from "./logger.js";
|
|
3
4
|
export { environment } from "./utilities/environment.js";
|
|
4
|
-
export { createLogger } from "./create-logger.js";
|
|
5
5
|
export { LogLevels } from "./utilities/log-types.js";
|
|
6
6
|
export type LogContext = import("./utilities/log-types.js").LogContext;
|
|
7
7
|
export type LogEntry = import("./utilities/log-types.js").LogEntry;
|
|
8
8
|
export type LogLevel = import("./utilities/log-types.js").LogLevel;
|
|
9
9
|
export type Formatter = import("./formatter.js").Formatter;
|
|
10
10
|
export type Replacer = import("./replacer.js").Replacer;
|
|
11
|
-
export {
|
|
11
|
+
export { BufferReplacer, ErrorReplacer, HttpClientRequestReplacer, HttpIncomingMessageReplacer, HttpServerResponseReplacer, LongStringReplacer, NetSocketReplacer, SecretDelete, SecretObscure } from "./replacers/index.js";
|
package/types/logger.d.ts
CHANGED
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
export class Logger {
|
|
9
9
|
/** @type {AsyncLocalStorage<import('./utilities/log-types.js').LogContext[]>} */
|
|
10
10
|
static storage: AsyncLocalStorage<import("./utilities/log-types.js").LogContext[]>;
|
|
11
|
+
/**
|
|
12
|
+
* @param {import('./utilities/log-types.js').LogContext} context Context to bind ot the async context
|
|
13
|
+
* @returns {void}
|
|
14
|
+
*/
|
|
15
|
+
static addAsyncContext(context: import("./utilities/log-types.js").LogContext): void;
|
|
11
16
|
/**
|
|
12
17
|
* @template [R=unknown]
|
|
13
18
|
* @overload
|
|
@@ -23,32 +28,42 @@ export class Logger {
|
|
|
23
28
|
* @returns {R}
|
|
24
29
|
*/
|
|
25
30
|
static wrap<R = unknown>(context: import("./utilities/log-types.js").LogContext, callback: () => R): R;
|
|
26
|
-
/**
|
|
27
|
-
* @param {import('./utilities/log-types.js').LogContext} context Context to bind ot the async context
|
|
28
|
-
* @returns {void}
|
|
29
|
-
*/
|
|
30
|
-
static addAsyncContext(context: import("./utilities/log-types.js").LogContext): void;
|
|
31
31
|
/**
|
|
32
32
|
* @param {import('./utilities/log-types.js').LogContext} [context] Context to add to the logger
|
|
33
33
|
* @param {Options} [options] Options for the loggers output
|
|
34
34
|
*/
|
|
35
35
|
constructor(context?: import("./utilities/log-types.js").LogContext | undefined, options?: Options | undefined);
|
|
36
|
-
/**
|
|
37
|
-
* @template [R=unknown]
|
|
38
|
-
* @param {() => R} callback The function to wrap the context in
|
|
39
|
-
* @returns {R}
|
|
40
|
-
*/
|
|
41
|
-
wrap<R = unknown>(callback: () => R): R;
|
|
42
36
|
/** @type {import('./replacer.js').Replacer[]} */
|
|
43
37
|
replacers: import("./replacer.js").Replacer[];
|
|
44
38
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @param {
|
|
47
|
-
* @
|
|
48
|
-
* @param {import('./utilities/log-types.js').LogContext} [context] Context to add to the log (merged with this.context, and Logger.contexts)
|
|
49
|
-
* @returns {void}
|
|
39
|
+
* Set the channel in the new logger
|
|
40
|
+
* @param {string} channel The channel name
|
|
41
|
+
* @returns {Logger}
|
|
50
42
|
*/
|
|
51
|
-
|
|
43
|
+
channel(channel: string): Logger;
|
|
44
|
+
/**
|
|
45
|
+
* Creating a new logger with {context, ...this.context}
|
|
46
|
+
* @overload
|
|
47
|
+
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
48
|
+
* @returns {Logger}
|
|
49
|
+
*/
|
|
50
|
+
context(context: import("./utilities/log-types.js").LogContext): Logger;
|
|
51
|
+
/**
|
|
52
|
+
* Creating a new logger with {context, ...this.context}
|
|
53
|
+
* @overload
|
|
54
|
+
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
55
|
+
* @param {import('./utilities/log-types.js').LogLevel} level The log level at which we allow this context
|
|
56
|
+
* @returns {Logger}
|
|
57
|
+
*/
|
|
58
|
+
context(context: import("./utilities/log-types.js").LogContext, level: import("./utilities/log-types.js").LogLevel): Logger;
|
|
59
|
+
/**
|
|
60
|
+
* Creating a new logger with {context, ...this.context}
|
|
61
|
+
* @overload
|
|
62
|
+
* @param {import('./utilities/log-types.js').LogLevel} level The log level at which we allow this context
|
|
63
|
+
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
64
|
+
* @returns {Logger}
|
|
65
|
+
*/
|
|
66
|
+
context(level: import("./utilities/log-types.js").LogLevel, context: import("./utilities/log-types.js").LogContext): Logger;
|
|
52
67
|
/**
|
|
53
68
|
* Write a log with the level 'critical' - A crucial part of the application is not working
|
|
54
69
|
*
|
|
@@ -58,26 +73,25 @@ export class Logger {
|
|
|
58
73
|
*/
|
|
59
74
|
critical(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
60
75
|
/**
|
|
61
|
-
* Write a log with the level '
|
|
76
|
+
* Write a log with the level 'debug' - Information that is unlikely to help in production
|
|
62
77
|
* @param {Error | string} message The message to log
|
|
63
78
|
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
64
79
|
* @returns {void}
|
|
65
80
|
*/
|
|
66
|
-
|
|
81
|
+
debug(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
67
82
|
/**
|
|
68
|
-
* Write a log with the level '
|
|
83
|
+
* Write a log with the level 'error' - A non critical operation fails
|
|
69
84
|
* @param {Error | string} message The message to log
|
|
70
85
|
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
71
86
|
* @returns {void}
|
|
72
87
|
*/
|
|
73
|
-
|
|
88
|
+
error(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
74
89
|
/**
|
|
75
|
-
*
|
|
76
|
-
* @param {
|
|
77
|
-
* @
|
|
78
|
-
* @returns {void}
|
|
90
|
+
* Get a value from the context by key
|
|
91
|
+
* @param {string} key The key to get from the context
|
|
92
|
+
* @returns {unknown}
|
|
79
93
|
*/
|
|
80
|
-
|
|
94
|
+
get(key: string): unknown;
|
|
81
95
|
/**
|
|
82
96
|
* Write a log with the level 'info' - Information about successful operations
|
|
83
97
|
* @param {Error | string} message The message to log
|
|
@@ -86,12 +100,19 @@ export class Logger {
|
|
|
86
100
|
*/
|
|
87
101
|
info(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
88
102
|
/**
|
|
89
|
-
* Write a log with the level '
|
|
103
|
+
* Write a log with the level 'notice' - Information about events that may be unusual
|
|
90
104
|
* @param {Error | string} message The message to log
|
|
91
105
|
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
92
106
|
* @returns {void}
|
|
93
107
|
*/
|
|
94
|
-
|
|
108
|
+
notice(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
109
|
+
/**
|
|
110
|
+
* Set a value into a new context by key
|
|
111
|
+
* @param {string} key The key to set
|
|
112
|
+
* @param {unknown} value The value to set
|
|
113
|
+
* @returns {Logger}
|
|
114
|
+
*/
|
|
115
|
+
set(key: string, value: unknown): Logger;
|
|
95
116
|
/**
|
|
96
117
|
* Write a log with the level 'silly' - Information to help resolve complex logic issues
|
|
97
118
|
* @param {Error | string} message The message to log
|
|
@@ -100,53 +121,32 @@ export class Logger {
|
|
|
100
121
|
*/
|
|
101
122
|
silly(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
102
123
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
106
|
-
* @returns {Logger}
|
|
107
|
-
*/
|
|
108
|
-
context(context: import("./utilities/log-types.js").LogContext): Logger;
|
|
109
|
-
/**
|
|
110
|
-
* Creating a new logger with {context, ...this.context}
|
|
111
|
-
* @overload
|
|
112
|
-
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
113
|
-
* @param {import('./utilities/log-types.js').LogLevel} level The log level at which we allow this context
|
|
114
|
-
* @returns {Logger}
|
|
115
|
-
*/
|
|
116
|
-
context(context: import("./utilities/log-types.js").LogContext, level: import("./utilities/log-types.js").LogLevel): Logger;
|
|
117
|
-
/**
|
|
118
|
-
* Creating a new logger with {context, ...this.context}
|
|
119
|
-
* @overload
|
|
120
|
-
* @param {import('./utilities/log-types.js').LogLevel} level The log level at which we allow this context
|
|
121
|
-
* @param {import('./utilities/log-types.js').LogContext} context The new context
|
|
124
|
+
* Set the trail in the new logger
|
|
125
|
+
* @param {string} [trail] The trail id
|
|
122
126
|
* @returns {Logger}
|
|
123
127
|
*/
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Get a value from the context by key
|
|
127
|
-
* @param {string} key The key to get from the context
|
|
128
|
-
* @returns {unknown}
|
|
129
|
-
*/
|
|
130
|
-
get(key: string): unknown;
|
|
128
|
+
trail(trail?: string | undefined): Logger;
|
|
131
129
|
/**
|
|
132
|
-
*
|
|
133
|
-
* @param {string}
|
|
134
|
-
* @param {
|
|
135
|
-
* @returns {
|
|
130
|
+
* Write a log with the level 'warning' - An operation might fail in the future
|
|
131
|
+
* @param {Error | string} message The message to log
|
|
132
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Additional context
|
|
133
|
+
* @returns {void}
|
|
136
134
|
*/
|
|
137
|
-
|
|
135
|
+
warning(message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
138
136
|
/**
|
|
139
|
-
*
|
|
140
|
-
* @param {
|
|
141
|
-
* @returns {
|
|
137
|
+
* @template [R=unknown]
|
|
138
|
+
* @param {() => R} callback The function to wrap the context in
|
|
139
|
+
* @returns {R}
|
|
142
140
|
*/
|
|
143
|
-
|
|
141
|
+
wrap<R = unknown>(callback: () => R): R;
|
|
144
142
|
/**
|
|
145
|
-
*
|
|
146
|
-
* @param {
|
|
147
|
-
* @
|
|
143
|
+
* Write a new log entry
|
|
144
|
+
* @param {import('./utilities/log-types.js').LogLevel} level the log level of the message to log
|
|
145
|
+
* @param {Error | string} message The message to log
|
|
146
|
+
* @param {import('./utilities/log-types.js').LogContext} [context] Context to add to the log (merged with this.context, and Logger.contexts)
|
|
147
|
+
* @returns {void}
|
|
148
148
|
*/
|
|
149
|
-
|
|
149
|
+
write(level: import("./utilities/log-types.js").LogLevel, message: Error | string, context?: import("./utilities/log-types.js").LogContext | undefined): void;
|
|
150
150
|
#private;
|
|
151
151
|
}
|
|
152
152
|
export type Options = {
|