@darksheep/logger 1.3.0 → 1.3.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/CHANGELOG.md +7 -0
- package/README.md +1 -3
- package/package.json +1 -1
- package/src/create-logger.js +1 -1
- package/src/index.js +15 -5
- package/src/logger.js +43 -38
- package/src/replacer.js +17 -8
- package/src/replacers/buffers.js +5 -1
- package/src/replacers/error.js +5 -2
- package/src/replacers/http-client-request.js +5 -1
- package/src/replacers/http-incoming-message.js +7 -2
- package/src/replacers/http-server-response.js +5 -1
- package/src/replacers/long-strings.js +5 -1
- package/src/replacers/net-socket.js +5 -1
- package/src/replacers/secrets.js +6 -2
- package/src/utilities/log-types.js +12 -4
- package/src/utilities/parse-filters.js +1 -1
- package/src/utilities/parse-log-level.js +5 -1
- package/types/create-logger.d.ts +3 -2
- package/types/logger.d.ts +40 -37
- package/types/replacer.d.ts +2 -2
- package/types/replacers/buffers.d.ts +3 -2
- package/types/replacers/error.d.ts +3 -2
- package/types/replacers/http-client-request.d.ts +3 -2
- package/types/replacers/http-incoming-message.d.ts +6 -4
- package/types/replacers/http-server-response.d.ts +3 -2
- package/types/replacers/long-strings.d.ts +3 -2
- package/types/replacers/net-socket.d.ts +3 -2
- package/types/replacers/secrets.d.ts +5 -4
- package/types/utilities/parse-log-level.d.ts +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.3.1](https://github.com/DarkSheepSoftware/node-packages/compare/logger-v1.3.0...logger-v1.3.1) (2026-04-17)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### 📚 Documentation
|
|
7
|
+
|
|
8
|
+
* Update the READMEs ([#824](https://github.com/DarkSheepSoftware/node-packages/issues/824)) ([9d282b7](https://github.com/DarkSheepSoftware/node-packages/commit/9d282b724fe056ec275578088bf495985e22c25f))
|
|
9
|
+
|
|
3
10
|
## [1.3.0](https://github.com/DarkSheepSoftware/node-packages/compare/logger-v1.2.0...logger-v1.3.0) (2025-10-09)
|
|
4
11
|
|
|
5
12
|
|
package/README.md
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# @darksheep/logger
|
|
2
2
|
Log configuration for NodeJS repos
|
|
3
3
|
|
|
4
|
-
This
|
|
5
|
-
|
|
6
|
-
This can be added using `yarn add @darksheep/logger` how ever you require a valid npm token for github packages!
|
|
4
|
+
This can be added using `yarn add @darksheep/logger`
|
|
7
5
|
|
|
8
6
|
### Quick start
|
|
9
7
|
#### Get something logging:
|
package/package.json
CHANGED
package/src/create-logger.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { createLogger } from './create-logger.js';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('./utilities/log-types.js').LogContext} LogContext
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('./utilities/log-types.js').LogEntry} LogEntry
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {import('./utilities/log-types.js').LogLevel} LogLevel
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {import('./formatter.js').Formatter} Formatter
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('./replacer.js').Replacer} Replacer
|
|
17
|
+
*/
|
|
8
18
|
|
|
9
19
|
export { createLogger } from './create-logger.js';
|
|
10
20
|
export const logger = createLogger();
|
package/src/logger.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { LogLevel, LogContext, LogEntry} from './utilities/log-types.js'
|
|
3
|
+
* @import { Replacer } from './replacer.js';
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
7
|
|
|
3
8
|
import { deepmerge } from 'deepmerge-ts';
|
|
@@ -15,16 +20,16 @@ import { LogLevels } from './utilities/log-types.js';
|
|
|
15
20
|
/**
|
|
16
21
|
* @typedef {Object} Options
|
|
17
22
|
* @property {typeof replace} [replace] - The replacer function to use.
|
|
18
|
-
* @property {
|
|
23
|
+
* @property {Replacer[]} [replacers] - The replacers to use in the replacer.
|
|
19
24
|
* @property {typeof formatter} [format] - The formatter function to use (eg console or json formatter).
|
|
20
25
|
* @property {typeof stdoutWrite} [write] - The place to write the log to.
|
|
21
26
|
*/
|
|
22
27
|
|
|
23
28
|
export class Logger {
|
|
24
|
-
/** @type {AsyncLocalStorage<
|
|
29
|
+
/** @type {AsyncLocalStorage<LogContext[]>} */
|
|
25
30
|
static storage = new AsyncLocalStorage();
|
|
26
31
|
|
|
27
|
-
/** @type {
|
|
32
|
+
/** @type {LogContext} */
|
|
28
33
|
#context;
|
|
29
34
|
|
|
30
35
|
/** @type {typeof formatter} */
|
|
@@ -36,10 +41,10 @@ export class Logger {
|
|
|
36
41
|
/** @type {typeof stdoutWrite} */
|
|
37
42
|
#write = stdoutWrite;
|
|
38
43
|
|
|
39
|
-
/** @type {
|
|
44
|
+
/** @type {Replacer[]} */
|
|
40
45
|
replacers = [];
|
|
41
46
|
/**
|
|
42
|
-
* @param {
|
|
47
|
+
* @param {LogContext} [context] - Context to add to the logger.
|
|
43
48
|
* @param {Options} [options] - Options for the loggers output.
|
|
44
49
|
*/
|
|
45
50
|
constructor(context, options) {
|
|
@@ -59,7 +64,7 @@ export class Logger {
|
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
/**
|
|
62
|
-
* @param {
|
|
67
|
+
* @param {LogContext} context - Context to bind ot the async context.
|
|
63
68
|
* @returns {void}
|
|
64
69
|
*/
|
|
65
70
|
static addAsyncContext(context) {
|
|
@@ -81,7 +86,7 @@ export class Logger {
|
|
|
81
86
|
/**
|
|
82
87
|
* @template [R=unknown]
|
|
83
88
|
* @overload
|
|
84
|
-
* @param {
|
|
89
|
+
* @param {LogContext} context - Context to bind ot the async context.
|
|
85
90
|
* @param {() => R} callback - The function to wrap the context in.
|
|
86
91
|
* @returns {R}
|
|
87
92
|
*/
|
|
@@ -103,14 +108,14 @@ export class Logger {
|
|
|
103
108
|
}
|
|
104
109
|
|
|
105
110
|
return Logger.storage.run(
|
|
106
|
-
[ ...store, /** @type {
|
|
111
|
+
[ ...store, /** @type {LogContext} */ (context) ],
|
|
107
112
|
/** @type {() => R} */ (callback),
|
|
108
113
|
);
|
|
109
114
|
}
|
|
110
115
|
|
|
111
116
|
/**
|
|
112
|
-
* @param {
|
|
113
|
-
* @param {
|
|
117
|
+
* @param {LogContext} context - The new context.
|
|
118
|
+
* @param {LogLevel} [level] - The log level at which we allow this context.
|
|
114
119
|
* @returns {Logger}
|
|
115
120
|
*/
|
|
116
121
|
#overrideContext(context, level) {
|
|
@@ -150,27 +155,27 @@ export class Logger {
|
|
|
150
155
|
/**
|
|
151
156
|
* Creating a new logger with {context, ...this.context}.
|
|
152
157
|
* @overload
|
|
153
|
-
* @param {
|
|
158
|
+
* @param {LogContext} context - The new context.
|
|
154
159
|
* @returns {Logger}
|
|
155
160
|
*/
|
|
156
161
|
/**
|
|
157
162
|
* Creating a new logger with {context, ...this.context}.
|
|
158
163
|
* @overload
|
|
159
|
-
* @param {
|
|
160
|
-
* @param {
|
|
164
|
+
* @param {LogContext} context - The new context.
|
|
165
|
+
* @param {LogLevel} level - The log level at which we allow this context.
|
|
161
166
|
* @returns {Logger}
|
|
162
167
|
*/
|
|
163
168
|
/**
|
|
164
169
|
* Creating a new logger with {context, ...this.context}.
|
|
165
170
|
* @overload
|
|
166
|
-
* @param {
|
|
167
|
-
* @param {
|
|
171
|
+
* @param {LogLevel} level - The log level at which we allow this context.
|
|
172
|
+
* @param {LogContext} context - The new context.
|
|
168
173
|
* @returns {Logger}
|
|
169
174
|
*/
|
|
170
175
|
/**
|
|
171
176
|
* Creating a new logger with {context, ...this.context}.
|
|
172
|
-
* @param {
|
|
173
|
-
* @param {
|
|
177
|
+
* @param {LogContext | LogLevel} contextOrLevel - The log level at which we allow this context.
|
|
178
|
+
* @param {LogLevel | LogContext} [levelOrContext] - The new context.
|
|
174
179
|
* @returns {Logger}
|
|
175
180
|
*/
|
|
176
181
|
context(contextOrLevel, levelOrContext) {
|
|
@@ -207,31 +212,31 @@ export class Logger {
|
|
|
207
212
|
* Write a log with the level 'critical' - A crucial part of the application is not working.
|
|
208
213
|
*
|
|
209
214
|
* @param {Error | string} message - The message to log.
|
|
210
|
-
* @param {
|
|
215
|
+
* @param {LogContext} [context] - Additional context.
|
|
211
216
|
* @returns {void}
|
|
212
217
|
*/
|
|
213
218
|
critical(message, context) {
|
|
214
|
-
|
|
219
|
+
this.write(LogLevels.critical, message, context);
|
|
215
220
|
}
|
|
216
221
|
|
|
217
222
|
/**
|
|
218
223
|
* Write a log with the level 'debug' - Information that is unlikely to help in production.
|
|
219
224
|
* @param {Error | string} message - The message to log.
|
|
220
|
-
* @param {
|
|
225
|
+
* @param {LogContext} [context] - Additional context.
|
|
221
226
|
* @returns {void}
|
|
222
227
|
*/
|
|
223
228
|
debug(message, context) {
|
|
224
|
-
|
|
229
|
+
this.write(LogLevels.debug, message, context);
|
|
225
230
|
}
|
|
226
231
|
|
|
227
232
|
/**
|
|
228
233
|
* Write a log with the level 'error' - A non critical operation fails.
|
|
229
234
|
* @param {Error | string} message - The message to log.
|
|
230
|
-
* @param {
|
|
235
|
+
* @param {LogContext} [context] - Additional context.
|
|
231
236
|
* @returns {void}
|
|
232
237
|
*/
|
|
233
238
|
error(message, context) {
|
|
234
|
-
|
|
239
|
+
this.write(LogLevels.error, message, context);
|
|
235
240
|
}
|
|
236
241
|
|
|
237
242
|
/**
|
|
@@ -246,21 +251,21 @@ export class Logger {
|
|
|
246
251
|
/**
|
|
247
252
|
* Write a log with the level 'info' - Information about successful operations.
|
|
248
253
|
* @param {Error | string} message - The message to log.
|
|
249
|
-
* @param {
|
|
254
|
+
* @param {LogContext} [context] - Additional context.
|
|
250
255
|
* @returns {void}
|
|
251
256
|
*/
|
|
252
257
|
info(message, context) {
|
|
253
|
-
|
|
258
|
+
this.write(LogLevels.info, message, context);
|
|
254
259
|
}
|
|
255
260
|
|
|
256
261
|
/**
|
|
257
262
|
* Write a log with the level 'notice' - Information about events that may be unusual.
|
|
258
263
|
* @param {Error | string} message - The message to log.
|
|
259
|
-
* @param {
|
|
264
|
+
* @param {LogContext} [context] - Additional context.
|
|
260
265
|
* @returns {void}
|
|
261
266
|
*/
|
|
262
267
|
notice(message, context) {
|
|
263
|
-
|
|
268
|
+
this.write(LogLevels.notice, message, context);
|
|
264
269
|
}
|
|
265
270
|
|
|
266
271
|
/*
|
|
@@ -282,11 +287,11 @@ export class Logger {
|
|
|
282
287
|
/**
|
|
283
288
|
* Write a log with the level 'silly' - Information to help resolve complex logic issues.
|
|
284
289
|
* @param {Error | string} message - The message to log.
|
|
285
|
-
* @param {
|
|
290
|
+
* @param {LogContext} [context] - Additional context.
|
|
286
291
|
* @returns {void}
|
|
287
292
|
*/
|
|
288
293
|
silly(message, context) {
|
|
289
|
-
|
|
294
|
+
this.write(LogLevels.silly, message, context);
|
|
290
295
|
}
|
|
291
296
|
|
|
292
297
|
/**
|
|
@@ -306,11 +311,11 @@ export class Logger {
|
|
|
306
311
|
/**
|
|
307
312
|
* Write a log with the level 'warning' - An operation might fail in the future.
|
|
308
313
|
* @param {Error | string} message - The message to log.
|
|
309
|
-
* @param {
|
|
314
|
+
* @param {LogContext} [context] - Additional context.
|
|
310
315
|
* @returns {void}
|
|
311
316
|
*/
|
|
312
317
|
warning(message, context) {
|
|
313
|
-
|
|
318
|
+
this.write(LogLevels.warning, message, context);
|
|
314
319
|
}
|
|
315
320
|
|
|
316
321
|
/**
|
|
@@ -324,13 +329,13 @@ export class Logger {
|
|
|
324
329
|
|
|
325
330
|
/**
|
|
326
331
|
* Write a new log entry.
|
|
327
|
-
* @param {
|
|
332
|
+
* @param {LogLevel} level - The log level of the message to log.
|
|
328
333
|
* @param {Error | string} message - The message to log.
|
|
329
|
-
* @param {
|
|
334
|
+
* @param {LogContext} [context] - Context to add to the log (merged with this.context, and Logger.contexts).
|
|
330
335
|
* @returns {void}
|
|
331
336
|
*/
|
|
332
337
|
write(level, message, context) {
|
|
333
|
-
/** @type {
|
|
338
|
+
/** @type {LogContext[]} */
|
|
334
339
|
const contexts = [];
|
|
335
340
|
|
|
336
341
|
const asyncContexts = Logger.storage.getStore();
|
|
@@ -344,7 +349,7 @@ export class Logger {
|
|
|
344
349
|
}
|
|
345
350
|
|
|
346
351
|
const fullContext =
|
|
347
|
-
/** @type {
|
|
352
|
+
/** @type {LogContext} */
|
|
348
353
|
(deepmerge(...contexts));
|
|
349
354
|
|
|
350
355
|
if (
|
|
@@ -354,7 +359,7 @@ export class Logger {
|
|
|
354
359
|
return;
|
|
355
360
|
}
|
|
356
361
|
|
|
357
|
-
/** @type {
|
|
362
|
+
/** @type {LogEntry} */
|
|
358
363
|
const entry = (
|
|
359
364
|
typeof message === 'string'
|
|
360
365
|
? { ...fullContext, message, level }
|
|
@@ -374,11 +379,11 @@ export class Logger {
|
|
|
374
379
|
}
|
|
375
380
|
|
|
376
381
|
const normalised =
|
|
377
|
-
/** @type {
|
|
382
|
+
/** @type {LogEntry} */
|
|
378
383
|
(this.#replace(entry, this.replacers));
|
|
379
384
|
|
|
380
385
|
const formatted = this.#format(normalised);
|
|
381
386
|
|
|
382
|
-
|
|
387
|
+
this.#write(formatted);
|
|
383
388
|
}
|
|
384
389
|
}
|
package/src/replacer.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { TraverseContext } from 'traverse';
|
|
3
|
+
*/
|
|
1
4
|
import Traverse from 'traverse';
|
|
2
5
|
|
|
3
6
|
import { nodesToPath } from './utilities/json-path.js';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
|
-
* @template [T=
|
|
9
|
+
* @template [T=any]
|
|
7
10
|
* @template [O=unknown]
|
|
8
11
|
* @typedef {Object} Replacer
|
|
9
12
|
* @property {string} name - The name of the replacer.
|
|
@@ -23,15 +26,17 @@ export function replace(object, replacers = []) {
|
|
|
23
26
|
/**
|
|
24
27
|
* @param {unknown} value - Current property in the traversed object.
|
|
25
28
|
* @returns {void}
|
|
26
|
-
* @this {
|
|
29
|
+
* @this {TraverseContext}
|
|
27
30
|
*/
|
|
28
31
|
function (value) {
|
|
29
32
|
if (value == null) {
|
|
30
|
-
|
|
33
|
+
this.remove();
|
|
34
|
+
return;
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
if (this.circular != null) {
|
|
34
|
-
|
|
38
|
+
this.update({ $ref: nodesToPath(this.circular.path) });
|
|
39
|
+
return;
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
for (const replacer of replacers) {
|
|
@@ -39,10 +44,12 @@ export function replace(object, replacers = []) {
|
|
|
39
44
|
const replaced = replacer.replace(value, this.path);
|
|
40
45
|
|
|
41
46
|
if (replaced == null) {
|
|
42
|
-
|
|
47
|
+
this.remove(replacer.stopHere);
|
|
48
|
+
return;
|
|
43
49
|
}
|
|
44
50
|
|
|
45
|
-
|
|
51
|
+
this.update(replaced, replacer.stopHere);
|
|
52
|
+
return;
|
|
46
53
|
}
|
|
47
54
|
}
|
|
48
55
|
|
|
@@ -52,7 +59,8 @@ export function replace(object, replacers = []) {
|
|
|
52
59
|
typeof value.toJSON === 'function'
|
|
53
60
|
) {
|
|
54
61
|
try {
|
|
55
|
-
|
|
62
|
+
this.update(value.toJSON());
|
|
63
|
+
return;
|
|
56
64
|
} catch { }
|
|
57
65
|
}
|
|
58
66
|
|
|
@@ -61,7 +69,8 @@ export function replace(object, replacers = []) {
|
|
|
61
69
|
Object.getPrototypeOf(value) !== Object.getPrototypeOf({}) &&
|
|
62
70
|
Object.getPrototypeOf(value) !== Object.getPrototypeOf([])
|
|
63
71
|
) {
|
|
64
|
-
|
|
72
|
+
this.update({ $class: value.constructor.name });
|
|
73
|
+
return;
|
|
65
74
|
}
|
|
66
75
|
|
|
67
76
|
if (Object.getPrototypeOf(value)?.constructor === Object) {
|
package/src/replacers/buffers.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* @param {Buffer} buffer - The buffer to convert to a string.
|
|
3
7
|
* @returns {string}
|
|
@@ -6,7 +10,7 @@ function stringBuffer(buffer) {
|
|
|
6
10
|
return buffer.toString('utf8');
|
|
7
11
|
}
|
|
8
12
|
|
|
9
|
-
/** @type {
|
|
13
|
+
/** @type {Replacer<Buffer>} */
|
|
10
14
|
export const BufferReplacer = {
|
|
11
15
|
name: 'Buffer',
|
|
12
16
|
stopHere: true,
|
package/src/replacers/error.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
1
4
|
import { parseStack } from '../utilities/stacktrace.js';
|
|
2
5
|
|
|
3
6
|
/**
|
|
@@ -11,7 +14,7 @@ import { parseStack } from '../utilities/stacktrace.js';
|
|
|
11
14
|
* @typedef {BaseError & { [k: string]: unknown }} NormalisedError
|
|
12
15
|
*/
|
|
13
16
|
|
|
14
|
-
/** @type {
|
|
17
|
+
/** @type {Replacer<Error, NormalisedError>} */
|
|
15
18
|
export const ErrorReplacer = {
|
|
16
19
|
name: 'Error',
|
|
17
20
|
shouldReplace: (input) => input instanceof Error,
|
|
@@ -21,7 +24,7 @@ export const ErrorReplacer = {
|
|
|
21
24
|
|
|
22
25
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
23
26
|
if (Object.hasOwn(value, key)) {
|
|
24
|
-
// @ts-
|
|
27
|
+
// @ts-expect-error - This is always going to be defined
|
|
25
28
|
error[key] = value[key];
|
|
26
29
|
}
|
|
27
30
|
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import { ClientRequest } from 'node:http';
|
|
2
6
|
|
|
3
|
-
/** @type {
|
|
7
|
+
/** @type {Replacer<ClientRequest>} */
|
|
4
8
|
export const HttpClientRequestReplacer = {
|
|
5
9
|
name: 'HttpClientRequest',
|
|
6
10
|
shouldReplace: (input) => input instanceof ClientRequest,
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { ClientRequest } from 'node:http'
|
|
3
|
+
* @import { Replacer } from '../replacer.js'
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { IncomingMessage } from 'node:http';
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
|
-
* @type {
|
|
9
|
+
* @type {Replacer<
|
|
5
10
|
* IncomingMessage & {
|
|
6
11
|
* hostname?: string,
|
|
7
12
|
* originalUrl?: string,
|
|
8
|
-
* req?:
|
|
13
|
+
* req?: ClientRequest
|
|
9
14
|
* }
|
|
10
15
|
* >}
|
|
11
16
|
*/
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import { ServerResponse, STATUS_CODES } from 'node:http';
|
|
2
6
|
|
|
3
|
-
/** @type {
|
|
7
|
+
/** @type {Replacer<ServerResponse>} */
|
|
4
8
|
export const HttpServerResponseReplacer = {
|
|
5
9
|
name: 'HttpServerResponse',
|
|
6
10
|
shouldReplace: (input) => input instanceof ServerResponse,
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import { environment } from '../utilities/environment.js';
|
|
2
6
|
|
|
3
|
-
/** @type {
|
|
7
|
+
/** @type {Replacer<string>} */
|
|
4
8
|
export const LongStringReplacer = {
|
|
5
9
|
name: 'LongString',
|
|
6
10
|
shouldReplace: (input) => (
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import { Socket } from 'node:net';
|
|
2
6
|
|
|
3
|
-
/** @type {
|
|
7
|
+
/** @type {Replacer<Socket>} */
|
|
4
8
|
export const NetSocketReplacer = {
|
|
5
9
|
name: 'NetSocket',
|
|
6
10
|
shouldReplace: (input) => input instanceof Socket,
|
package/src/replacers/secrets.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Replacer } from '../replacer.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import { environment } from '../utilities/environment.js';
|
|
2
6
|
import { nodesToPath } from '../utilities/json-path.js';
|
|
3
7
|
|
|
4
|
-
/** @type {
|
|
8
|
+
/** @type {Replacer} */
|
|
5
9
|
export const SecretDelete = {
|
|
6
10
|
name: 'SecretDelete',
|
|
7
11
|
stopHere: true,
|
|
@@ -22,7 +26,7 @@ export const SecretDelete = {
|
|
|
22
26
|
replace: () => null,
|
|
23
27
|
};
|
|
24
28
|
|
|
25
|
-
/** @type {
|
|
29
|
+
/** @type {Replacer} */
|
|
26
30
|
export const SecretObscure = {
|
|
27
31
|
name: 'SecretObscure',
|
|
28
32
|
stopHere: true,
|
|
@@ -23,11 +23,19 @@ export const LogNames = Object.fromEntries(
|
|
|
23
23
|
Object.entries(LogLevels).map(([ k, v ]) => [ v, k ]),
|
|
24
24
|
);
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {keyof LogLevels} LogLevelNames
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {LogLevels[LogLevelNames]} LogLevel
|
|
31
|
+
*/
|
|
28
32
|
|
|
29
|
-
/**
|
|
30
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {import('./stacktrace.js').Callsite} Callsite
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {import('../replacers/error.js').NormalisedError} NormalisedError
|
|
38
|
+
*/
|
|
31
39
|
|
|
32
40
|
/**
|
|
33
41
|
* @typedef {Object} LogInternal
|
|
@@ -34,7 +34,7 @@ export function parseFilters(filter = '', fallback = []) {
|
|
|
34
34
|
|
|
35
35
|
// Allow for multi level wildcards at the start of a filter
|
|
36
36
|
if (regex.startsWith(String.raw`.*\.`)) {
|
|
37
|
-
regex = `(
|
|
37
|
+
regex = String.raw`(?:.*\.)?${regex.slice(4)}`;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
const pattern = new RegExp(`^${regex}$`);
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { LogLevel } from './log-types.js';
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import { LogLevels } from './log-types.js';
|
|
2
6
|
|
|
3
7
|
/**
|
|
4
8
|
* Convert a given string to a LogLevel.
|
|
5
9
|
* @param {string} [input] - The string to convert.
|
|
6
10
|
* @param {string} [node] - The current NODE_ENV.
|
|
7
|
-
* @returns {
|
|
11
|
+
* @returns {LogLevel}
|
|
8
12
|
*/
|
|
9
13
|
export function parseLogLevel(input, node) {
|
|
10
14
|
switch (input) {
|
package/types/create-logger.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @param {string} [channel] - The logging channel.
|
|
3
|
-
* @returns {
|
|
3
|
+
* @returns {Logger}
|
|
4
4
|
*/
|
|
5
|
-
export function createLogger(channel?: string):
|
|
5
|
+
export function createLogger(channel?: string): Logger;
|
|
6
|
+
import { Logger } from './logger.js';
|
package/types/logger.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @typedef {Object} Options
|
|
3
3
|
* @property {typeof replace} [replace] - The replacer function to use.
|
|
4
|
-
* @property {
|
|
4
|
+
* @property {Replacer[]} [replacers] - The replacers to use in the replacer.
|
|
5
5
|
* @property {typeof formatter} [format] - The formatter function to use (eg console or json formatter).
|
|
6
6
|
* @property {typeof stdoutWrite} [write] - The place to write the log to.
|
|
7
7
|
*/
|
|
8
8
|
export class Logger {
|
|
9
|
-
/** @type {AsyncLocalStorage<
|
|
10
|
-
static storage: AsyncLocalStorage<
|
|
9
|
+
/** @type {AsyncLocalStorage<LogContext[]>} */
|
|
10
|
+
static storage: AsyncLocalStorage<LogContext[]>;
|
|
11
11
|
/**
|
|
12
|
-
* @param {
|
|
12
|
+
* @param {LogContext} context - Context to bind ot the async context.
|
|
13
13
|
* @returns {void}
|
|
14
14
|
*/
|
|
15
|
-
static addAsyncContext(context:
|
|
15
|
+
static addAsyncContext(context: LogContext): void;
|
|
16
16
|
/**
|
|
17
17
|
* @template [R=unknown]
|
|
18
18
|
* @overload
|
|
@@ -23,18 +23,18 @@ export class Logger {
|
|
|
23
23
|
/**
|
|
24
24
|
* @template [R=unknown]
|
|
25
25
|
* @overload
|
|
26
|
-
* @param {
|
|
26
|
+
* @param {LogContext} context - Context to bind ot the async context.
|
|
27
27
|
* @param {() => R} callback - The function to wrap the context in.
|
|
28
28
|
* @returns {R}
|
|
29
29
|
*/
|
|
30
|
-
static wrap<R = unknown>(context:
|
|
30
|
+
static wrap<R = unknown>(context: LogContext, callback: () => R): R;
|
|
31
31
|
/**
|
|
32
|
-
* @param {
|
|
32
|
+
* @param {LogContext} [context] - Context to add to the logger.
|
|
33
33
|
* @param {Options} [options] - Options for the loggers output.
|
|
34
34
|
*/
|
|
35
|
-
constructor(context?:
|
|
36
|
-
/** @type {
|
|
37
|
-
replacers:
|
|
35
|
+
constructor(context?: LogContext, options?: Options);
|
|
36
|
+
/** @type {Replacer[]} */
|
|
37
|
+
replacers: Replacer[];
|
|
38
38
|
/**
|
|
39
39
|
* Set the channel in the new logger.
|
|
40
40
|
* @param {string} channel - The channel name.
|
|
@@ -44,48 +44,48 @@ export class Logger {
|
|
|
44
44
|
/**
|
|
45
45
|
* Creating a new logger with {context, ...this.context}.
|
|
46
46
|
* @overload
|
|
47
|
-
* @param {
|
|
47
|
+
* @param {LogContext} context - The new context.
|
|
48
48
|
* @returns {Logger}
|
|
49
49
|
*/
|
|
50
|
-
context(context:
|
|
50
|
+
context(context: LogContext): Logger;
|
|
51
51
|
/**
|
|
52
52
|
* Creating a new logger with {context, ...this.context}.
|
|
53
53
|
* @overload
|
|
54
|
-
* @param {
|
|
55
|
-
* @param {
|
|
54
|
+
* @param {LogContext} context - The new context.
|
|
55
|
+
* @param {LogLevel} level - The log level at which we allow this context.
|
|
56
56
|
* @returns {Logger}
|
|
57
57
|
*/
|
|
58
|
-
context(context:
|
|
58
|
+
context(context: LogContext, level: LogLevel): Logger;
|
|
59
59
|
/**
|
|
60
60
|
* Creating a new logger with {context, ...this.context}.
|
|
61
61
|
* @overload
|
|
62
|
-
* @param {
|
|
63
|
-
* @param {
|
|
62
|
+
* @param {LogLevel} level - The log level at which we allow this context.
|
|
63
|
+
* @param {LogContext} context - The new context.
|
|
64
64
|
* @returns {Logger}
|
|
65
65
|
*/
|
|
66
|
-
context(level:
|
|
66
|
+
context(level: LogLevel, context: LogContext): Logger;
|
|
67
67
|
/**
|
|
68
68
|
* Write a log with the level 'critical' - A crucial part of the application is not working.
|
|
69
69
|
*
|
|
70
70
|
* @param {Error | string} message - The message to log.
|
|
71
|
-
* @param {
|
|
71
|
+
* @param {LogContext} [context] - Additional context.
|
|
72
72
|
* @returns {void}
|
|
73
73
|
*/
|
|
74
|
-
critical(message: Error | string, context?:
|
|
74
|
+
critical(message: Error | string, context?: LogContext): void;
|
|
75
75
|
/**
|
|
76
76
|
* Write a log with the level 'debug' - Information that is unlikely to help in production.
|
|
77
77
|
* @param {Error | string} message - The message to log.
|
|
78
|
-
* @param {
|
|
78
|
+
* @param {LogContext} [context] - Additional context.
|
|
79
79
|
* @returns {void}
|
|
80
80
|
*/
|
|
81
|
-
debug(message: Error | string, context?:
|
|
81
|
+
debug(message: Error | string, context?: LogContext): void;
|
|
82
82
|
/**
|
|
83
83
|
* Write a log with the level 'error' - A non critical operation fails.
|
|
84
84
|
* @param {Error | string} message - The message to log.
|
|
85
|
-
* @param {
|
|
85
|
+
* @param {LogContext} [context] - Additional context.
|
|
86
86
|
* @returns {void}
|
|
87
87
|
*/
|
|
88
|
-
error(message: Error | string, context?:
|
|
88
|
+
error(message: Error | string, context?: LogContext): void;
|
|
89
89
|
/**
|
|
90
90
|
* Get a value from the context by key.
|
|
91
91
|
* @param {string} key - The key to get from the context.
|
|
@@ -95,17 +95,17 @@ export class Logger {
|
|
|
95
95
|
/**
|
|
96
96
|
* Write a log with the level 'info' - Information about successful operations.
|
|
97
97
|
* @param {Error | string} message - The message to log.
|
|
98
|
-
* @param {
|
|
98
|
+
* @param {LogContext} [context] - Additional context.
|
|
99
99
|
* @returns {void}
|
|
100
100
|
*/
|
|
101
|
-
info(message: Error | string, context?:
|
|
101
|
+
info(message: Error | string, context?: LogContext): void;
|
|
102
102
|
/**
|
|
103
103
|
* Write a log with the level 'notice' - Information about events that may be unusual.
|
|
104
104
|
* @param {Error | string} message - The message to log.
|
|
105
|
-
* @param {
|
|
105
|
+
* @param {LogContext} [context] - Additional context.
|
|
106
106
|
* @returns {void}
|
|
107
107
|
*/
|
|
108
|
-
notice(message: Error | string, context?:
|
|
108
|
+
notice(message: Error | string, context?: LogContext): void;
|
|
109
109
|
/**
|
|
110
110
|
* Set a value into a new context by key.
|
|
111
111
|
* @param {string} key - The key to set.
|
|
@@ -116,10 +116,10 @@ export class Logger {
|
|
|
116
116
|
/**
|
|
117
117
|
* Write a log with the level 'silly' - Information to help resolve complex logic issues.
|
|
118
118
|
* @param {Error | string} message - The message to log.
|
|
119
|
-
* @param {
|
|
119
|
+
* @param {LogContext} [context] - Additional context.
|
|
120
120
|
* @returns {void}
|
|
121
121
|
*/
|
|
122
|
-
silly(message: Error | string, context?:
|
|
122
|
+
silly(message: Error | string, context?: LogContext): void;
|
|
123
123
|
/**
|
|
124
124
|
* Set the trail in the new logger.
|
|
125
125
|
* @param {string} [trail] - The trail id.
|
|
@@ -129,10 +129,10 @@ export class Logger {
|
|
|
129
129
|
/**
|
|
130
130
|
* Write a log with the level 'warning' - An operation might fail in the future.
|
|
131
131
|
* @param {Error | string} message - The message to log.
|
|
132
|
-
* @param {
|
|
132
|
+
* @param {LogContext} [context] - Additional context.
|
|
133
133
|
* @returns {void}
|
|
134
134
|
*/
|
|
135
|
-
warning(message: Error | string, context?:
|
|
135
|
+
warning(message: Error | string, context?: LogContext): void;
|
|
136
136
|
/**
|
|
137
137
|
* @template [R=unknown]
|
|
138
138
|
* @param {() => R} callback - The function to wrap the context in.
|
|
@@ -141,12 +141,12 @@ export class Logger {
|
|
|
141
141
|
wrap<R = unknown>(callback: () => R): R;
|
|
142
142
|
/**
|
|
143
143
|
* Write a new log entry.
|
|
144
|
-
* @param {
|
|
144
|
+
* @param {LogLevel} level - The log level of the message to log.
|
|
145
145
|
* @param {Error | string} message - The message to log.
|
|
146
|
-
* @param {
|
|
146
|
+
* @param {LogContext} [context] - Context to add to the log (merged with this.context, and Logger.contexts).
|
|
147
147
|
* @returns {void}
|
|
148
148
|
*/
|
|
149
|
-
write(level:
|
|
149
|
+
write(level: LogLevel, message: Error | string, context?: LogContext): void;
|
|
150
150
|
#private;
|
|
151
151
|
}
|
|
152
152
|
export type Options = {
|
|
@@ -157,7 +157,7 @@ export type Options = {
|
|
|
157
157
|
/**
|
|
158
158
|
* - The replacers to use in the replacer.
|
|
159
159
|
*/
|
|
160
|
-
replacers?:
|
|
160
|
+
replacers?: Replacer[];
|
|
161
161
|
/**
|
|
162
162
|
* - The formatter function to use (eg console or json formatter).
|
|
163
163
|
*/
|
|
@@ -167,6 +167,9 @@ export type Options = {
|
|
|
167
167
|
*/
|
|
168
168
|
write?: typeof stdoutWrite;
|
|
169
169
|
};
|
|
170
|
+
import type { Replacer } from './replacer.js';
|
|
171
|
+
import type { LogContext } from './utilities/log-types.js';
|
|
172
|
+
import type { LogLevel } from './utilities/log-types.js';
|
|
170
173
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
171
174
|
import { replace } from './replacer.js';
|
|
172
175
|
import { formatter } from './formatter.js';
|
package/types/replacer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @template [T=
|
|
2
|
+
* @template [T=any]
|
|
3
3
|
* @template [O=unknown]
|
|
4
4
|
* @typedef {Object} Replacer
|
|
5
5
|
* @property {string} name - The name of the replacer.
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* @returns {unknown}
|
|
14
14
|
*/
|
|
15
15
|
export function replace(object: unknown, replacers?: Replacer[]): unknown;
|
|
16
|
-
export type Replacer<T =
|
|
16
|
+
export type Replacer<T = any, O = unknown> = {
|
|
17
17
|
/**
|
|
18
18
|
* - The name of the replacer.
|
|
19
19
|
*/
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
/** @type {
|
|
2
|
-
export const BufferReplacer:
|
|
1
|
+
/** @type {Replacer<Buffer>} */
|
|
2
|
+
export const BufferReplacer: Replacer<Buffer>;
|
|
3
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
/**
|
|
8
8
|
* @typedef {BaseError & { [k: string]: unknown }} NormalisedError
|
|
9
9
|
*/
|
|
10
|
-
/** @type {
|
|
11
|
-
export const ErrorReplacer:
|
|
10
|
+
/** @type {Replacer<Error, NormalisedError>} */
|
|
11
|
+
export const ErrorReplacer: Replacer<Error, NormalisedError>;
|
|
12
12
|
export type BaseError = {
|
|
13
13
|
/**
|
|
14
14
|
* - The error type or name.
|
|
@@ -26,3 +26,4 @@ export type BaseError = {
|
|
|
26
26
|
export type NormalisedError = BaseError & {
|
|
27
27
|
[k: string]: unknown;
|
|
28
28
|
};
|
|
29
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
/** @type {
|
|
2
|
-
export const HttpClientRequestReplacer:
|
|
1
|
+
/** @type {Replacer<ClientRequest>} */
|
|
2
|
+
export const HttpClientRequestReplacer: Replacer<ClientRequest>;
|
|
3
3
|
import { ClientRequest } from 'node:http';
|
|
4
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @type {
|
|
2
|
+
* @type {Replacer<
|
|
3
3
|
* IncomingMessage & {
|
|
4
4
|
* hostname?: string,
|
|
5
5
|
* originalUrl?: string,
|
|
6
|
-
* req?:
|
|
6
|
+
* req?: ClientRequest
|
|
7
7
|
* }
|
|
8
8
|
* >}
|
|
9
9
|
*/
|
|
10
|
-
export const HttpIncomingMessageReplacer:
|
|
10
|
+
export const HttpIncomingMessageReplacer: Replacer<IncomingMessage & {
|
|
11
11
|
hostname?: string;
|
|
12
12
|
originalUrl?: string;
|
|
13
|
-
req?:
|
|
13
|
+
req?: ClientRequest;
|
|
14
14
|
}>;
|
|
15
15
|
import { IncomingMessage } from 'node:http';
|
|
16
|
+
import type { ClientRequest } from 'node:http';
|
|
17
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
/** @type {
|
|
2
|
-
export const HttpServerResponseReplacer:
|
|
1
|
+
/** @type {Replacer<ServerResponse>} */
|
|
2
|
+
export const HttpServerResponseReplacer: Replacer<ServerResponse>;
|
|
3
3
|
import { ServerResponse } from 'node:http';
|
|
4
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
/** @type {
|
|
2
|
-
export const LongStringReplacer:
|
|
1
|
+
/** @type {Replacer<string>} */
|
|
2
|
+
export const LongStringReplacer: Replacer<string>;
|
|
3
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
/** @type {
|
|
2
|
-
export const NetSocketReplacer:
|
|
1
|
+
/** @type {Replacer<Socket>} */
|
|
2
|
+
export const NetSocketReplacer: Replacer<Socket>;
|
|
3
3
|
import { Socket } from 'node:net';
|
|
4
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
/** @type {
|
|
2
|
-
export const SecretDelete:
|
|
3
|
-
/** @type {
|
|
4
|
-
export const SecretObscure:
|
|
1
|
+
/** @type {Replacer} */
|
|
2
|
+
export const SecretDelete: Replacer;
|
|
3
|
+
/** @type {Replacer} */
|
|
4
|
+
export const SecretObscure: Replacer;
|
|
5
|
+
import type { Replacer } from '../replacer.js';
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Convert a given string to a LogLevel.
|
|
3
3
|
* @param {string} [input] - The string to convert.
|
|
4
4
|
* @param {string} [node] - The current NODE_ENV.
|
|
5
|
-
* @returns {
|
|
5
|
+
* @returns {LogLevel}
|
|
6
6
|
*/
|
|
7
|
-
export function parseLogLevel(input?: string, node?: string):
|
|
7
|
+
export function parseLogLevel(input?: string, node?: string): LogLevel;
|
|
8
|
+
import type { LogLevel } from './log-types.js';
|