@hkdigital/lib-core 0.4.32 → 0.4.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/logging/internal/adapters/console.js +5 -5
- package/dist/logging/internal/adapters/pino.js +2 -1
- package/dist/logging/internal/factories/client.js +2 -2
- package/dist/logging/internal/factories/server.js +2 -2
- package/dist/logging/internal/logger/Logger.d.ts +8 -7
- package/dist/logging/internal/logger/Logger.js +9 -6
- package/dist/services/README.md +2 -2
- package/dist/services/service-manager/ServiceManager.d.ts +16 -0
- package/dist/services/service-manager/ServiceManager.js +48 -9
- package/dist/services/service-manager/constants.d.ts +6 -2
- package/dist/services/service-manager/constants.js +10 -3
- package/dist/util/sveltekit/env/README.md +1 -2
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { dev } from '$app/environment';
|
|
2
|
-
import { LEVELS } from '../../levels.js';
|
|
2
|
+
import { LEVELS, DEBUG, INFO, WARN, ERROR } from '../../levels.js';
|
|
3
3
|
import {
|
|
4
4
|
findRelevantFrameIndex,
|
|
5
5
|
detectErrorMeta,
|
|
@@ -164,13 +164,13 @@ export class ConsoleAdapter {
|
|
|
164
164
|
*/
|
|
165
165
|
#getConsoleMethod(level) {
|
|
166
166
|
switch (level) {
|
|
167
|
-
case
|
|
167
|
+
case DEBUG:
|
|
168
168
|
return 'debug';
|
|
169
|
-
case
|
|
169
|
+
case INFO:
|
|
170
170
|
return 'info';
|
|
171
|
-
case
|
|
171
|
+
case WARN:
|
|
172
172
|
return 'warn';
|
|
173
|
-
case
|
|
173
|
+
case ERROR:
|
|
174
174
|
case 'fatal':
|
|
175
175
|
return 'error';
|
|
176
176
|
default:
|
|
@@ -5,6 +5,7 @@ import pino from 'pino';
|
|
|
5
5
|
import { dev } from '$app/environment';
|
|
6
6
|
|
|
7
7
|
import { HkPromise } from '../../../generic/promises.js';
|
|
8
|
+
import { ERROR } from '../../levels.js';
|
|
8
9
|
|
|
9
10
|
import {
|
|
10
11
|
detectErrorMeta,
|
|
@@ -240,7 +241,7 @@ export class PinoAdapter {
|
|
|
240
241
|
|
|
241
242
|
while (this.#messageQueue.length > 0) {
|
|
242
243
|
const queuedLog = this.#messageQueue.shift();
|
|
243
|
-
console[queuedLog.level ===
|
|
244
|
+
console[queuedLog.level === ERROR ? 'error' : 'log'](
|
|
244
245
|
`[${queuedLog.level.toUpperCase()}] ${queuedLog.message}`,
|
|
245
246
|
queuedLog.data
|
|
246
247
|
);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger } from '../logger/index.js';
|
|
2
2
|
import { ConsoleAdapter } from '../adapters/console.js';
|
|
3
|
-
import { INFO } from '../../levels.js';
|
|
3
|
+
import { INFO, LOG } from '../../levels.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a client-side logger with console adapter
|
|
@@ -15,7 +15,7 @@ export function createClientLogger(name, level = INFO, consoleOptions = {}) {
|
|
|
15
15
|
const adapter = new ConsoleAdapter({ ...consoleOptions, level });
|
|
16
16
|
|
|
17
17
|
// Connect adapter to logger events
|
|
18
|
-
logger.on(
|
|
18
|
+
logger.on(LOG, (logEvent) => adapter.handleLog(logEvent));
|
|
19
19
|
|
|
20
20
|
return logger;
|
|
21
21
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger } from '../logger/index.js';
|
|
2
2
|
import { PinoAdapter } from '../adapters/pino.js';
|
|
3
|
-
import { INFO } from '../../levels.js';
|
|
3
|
+
import { INFO, LOG } from '../../levels.js';
|
|
4
4
|
// import { expectNoSSRContext } from '../../../util/ssr/index.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -26,7 +26,7 @@ export function createServerLogger(name, level = INFO, pinoOptions = {}) {
|
|
|
26
26
|
// determination to fail
|
|
27
27
|
// -> Stop and start the dev server
|
|
28
28
|
//
|
|
29
|
-
logger.on(
|
|
29
|
+
logger.on(LOG, (logEvent) => adapter.handleLog(logEvent));
|
|
30
30
|
|
|
31
31
|
return logger;
|
|
32
32
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** @typedef {import('../../typedef.js').LogLevel} LogLevel */
|
|
1
2
|
/**
|
|
2
3
|
* Logger class for consistent logging
|
|
3
4
|
* @extends EventEmitter
|
|
@@ -7,19 +8,19 @@ export default class Logger extends EventEmitter {
|
|
|
7
8
|
* Create a new Logger instance
|
|
8
9
|
*
|
|
9
10
|
* @param {string} name - Name of the service/component for this logger
|
|
10
|
-
* @param {
|
|
11
|
+
* @param {LogLevel} [defaultLevel=INFO] - Initial log level threshold
|
|
11
12
|
* @param {Object} [context={}] - Default context data for all logs
|
|
12
13
|
*/
|
|
13
|
-
constructor(name: string, defaultLevel?:
|
|
14
|
+
constructor(name: string, defaultLevel?: LogLevel, context?: Object);
|
|
14
15
|
name: string;
|
|
15
|
-
level:
|
|
16
|
+
level: import("../../typedef.js").LogLevel;
|
|
16
17
|
/**
|
|
17
18
|
* Set the minimum log level threshold
|
|
18
19
|
*
|
|
19
|
-
* @param {
|
|
20
|
+
* @param {LogLevel} level - New log level (DEBUG, INFO, WARN, ERROR or NONE)
|
|
20
21
|
* @returns {boolean} True if level was valid and set, false otherwise
|
|
21
22
|
*/
|
|
22
|
-
setLevel(level:
|
|
23
|
+
setLevel(level: LogLevel): boolean;
|
|
23
24
|
/**
|
|
24
25
|
* Log a debug message
|
|
25
26
|
*
|
|
@@ -77,11 +78,11 @@ export default class Logger extends EventEmitter {
|
|
|
77
78
|
* E.g. an event that was created by another Logger instance and should be
|
|
78
79
|
* forwarded to this logger.
|
|
79
80
|
*
|
|
80
|
-
* @param {string} eventName
|
|
81
81
|
* @param {import('../../typedef.js').LogEventData} eventData
|
|
82
82
|
*/
|
|
83
|
-
logFromEvent(
|
|
83
|
+
logFromEvent(eventData: import("../../typedef.js").LogEventData): false | undefined;
|
|
84
84
|
#private;
|
|
85
85
|
}
|
|
86
86
|
export type ErrorDetails = import("../../../generic/typedef.js").ErrorDetails;
|
|
87
|
+
export type LogLevel = import("../../typedef.js").LogLevel;
|
|
87
88
|
import { EventEmitter } from '../../../generic/events.js';
|
|
@@ -59,6 +59,8 @@ import { exportNotNullish } from '../../../util/object.js';
|
|
|
59
59
|
import * as is from '../../../util/is.js';
|
|
60
60
|
import { HttpError } from '../../../network/errors.js';
|
|
61
61
|
|
|
62
|
+
/** @typedef {import('../../typedef.js').LogLevel} LogLevel */
|
|
63
|
+
|
|
62
64
|
/**
|
|
63
65
|
* Logger class for consistent logging
|
|
64
66
|
* @extends EventEmitter
|
|
@@ -71,7 +73,7 @@ export default class Logger extends EventEmitter {
|
|
|
71
73
|
* Create a new Logger instance
|
|
72
74
|
*
|
|
73
75
|
* @param {string} name - Name of the service/component for this logger
|
|
74
|
-
* @param {
|
|
76
|
+
* @param {LogLevel} [defaultLevel=INFO] - Initial log level threshold
|
|
75
77
|
* @param {Object} [context={}] - Default context data for all logs
|
|
76
78
|
*/
|
|
77
79
|
constructor(name, defaultLevel = INFO, context = {}) {
|
|
@@ -86,7 +88,7 @@ export default class Logger extends EventEmitter {
|
|
|
86
88
|
/**
|
|
87
89
|
* Set the minimum log level threshold
|
|
88
90
|
*
|
|
89
|
-
* @param {
|
|
91
|
+
* @param {LogLevel} level - New log level (DEBUG, INFO, WARN, ERROR or NONE)
|
|
90
92
|
* @returns {boolean} True if level was valid and set, false otherwise
|
|
91
93
|
*/
|
|
92
94
|
setLevel(level) {
|
|
@@ -239,10 +241,9 @@ export default class Logger extends EventEmitter {
|
|
|
239
241
|
* E.g. an event that was created by another Logger instance and should be
|
|
240
242
|
* forwarded to this logger.
|
|
241
243
|
*
|
|
242
|
-
* @param {string} eventName
|
|
243
244
|
* @param {import('../../typedef.js').LogEventData} eventData
|
|
244
245
|
*/
|
|
245
|
-
logFromEvent(
|
|
246
|
+
logFromEvent(eventData) {
|
|
246
247
|
const level = eventData.level;
|
|
247
248
|
|
|
248
249
|
// Check if this log level should be filtered
|
|
@@ -250,13 +251,13 @@ export default class Logger extends EventEmitter {
|
|
|
250
251
|
return false; // Below threshold, don't emit
|
|
251
252
|
}
|
|
252
253
|
|
|
253
|
-
this.#logEvent(
|
|
254
|
+
this.#logEvent(eventData);
|
|
254
255
|
}
|
|
255
256
|
|
|
256
257
|
/**
|
|
257
258
|
* Internal logging method
|
|
258
259
|
*
|
|
259
|
-
* @param {
|
|
260
|
+
* @param {LogLevel} level - Log level
|
|
260
261
|
* @param {string} message - Log message
|
|
261
262
|
* @param {*} [details] - Additional details to include in the log
|
|
262
263
|
* @returns {boolean} True if the log was emitted, false if filtered
|
|
@@ -355,8 +356,10 @@ export default class Logger extends EventEmitter {
|
|
|
355
356
|
if (reason instanceof Error) {
|
|
356
357
|
return reason;
|
|
357
358
|
}
|
|
359
|
+
// @ts-ignore
|
|
358
360
|
else if ( is.object(reason) && reason?.message ) {
|
|
359
361
|
// reason is an object with message property
|
|
362
|
+
// @ts-ignore
|
|
360
363
|
return new DetailedError(reason?.message, reason);
|
|
361
364
|
}
|
|
362
365
|
else if ( typeof reason === "string" ) {
|
package/dist/services/README.md
CHANGED
|
@@ -302,8 +302,8 @@ const manager = new ServiceManager();
|
|
|
302
302
|
const logger = createServerLogger('SystemLogger');
|
|
303
303
|
|
|
304
304
|
// Listen to all log events and forward them to the logger
|
|
305
|
-
const unsubscribe = manager.
|
|
306
|
-
logger.logFromEvent(
|
|
305
|
+
const unsubscribe = manager.onLogEvent((logEvent) => {
|
|
306
|
+
logger.logFromEvent(logEvent);
|
|
307
307
|
});
|
|
308
308
|
|
|
309
309
|
// Register services
|
|
@@ -140,6 +140,22 @@ export class ServiceManager extends EventEmitter {
|
|
|
140
140
|
* @returns {Function} Unsubscribe function
|
|
141
141
|
*/
|
|
142
142
|
onServiceLogEvent(listener: (logEvent: LogEvent) => void): Function;
|
|
143
|
+
/**
|
|
144
|
+
* Listen to log messages emitted by the ServiceManager itself
|
|
145
|
+
*
|
|
146
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
147
|
+
*
|
|
148
|
+
* @returns {Function} Unsubscribe function
|
|
149
|
+
*/
|
|
150
|
+
onManagerLogEvent(listener: (logEvent: LogEvent) => void): Function;
|
|
151
|
+
/**
|
|
152
|
+
* Listen to all log messages (both manager and services)
|
|
153
|
+
*
|
|
154
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
155
|
+
*
|
|
156
|
+
* @returns {Function} Unsubscribe function
|
|
157
|
+
*/
|
|
158
|
+
onLogEvent(listener: (logEvent: LogEvent) => void): Function;
|
|
143
159
|
/**
|
|
144
160
|
* Set log level for the ServiceManager itself
|
|
145
161
|
*
|
|
@@ -66,7 +66,16 @@
|
|
|
66
66
|
import { EventEmitter } from '../../generic/events.js';
|
|
67
67
|
import { Logger, DEBUG, INFO } from '../../logging/index.js';
|
|
68
68
|
|
|
69
|
-
import {
|
|
69
|
+
import {
|
|
70
|
+
SERVICE_LOG,
|
|
71
|
+
STATE_CHANGED,
|
|
72
|
+
HEALTH_CHANGED,
|
|
73
|
+
ERROR,
|
|
74
|
+
LOG,
|
|
75
|
+
SERVICE_STATE_CHANGED,
|
|
76
|
+
SERVICE_HEALTH_CHANGED,
|
|
77
|
+
SERVICE_ERROR
|
|
78
|
+
} from './constants.js';
|
|
70
79
|
import { parseServiceLogLevels } from './util.js';
|
|
71
80
|
|
|
72
81
|
import {
|
|
@@ -503,6 +512,36 @@ export class ServiceManager extends EventEmitter {
|
|
|
503
512
|
return this.on(SERVICE_LOG, listener);
|
|
504
513
|
}
|
|
505
514
|
|
|
515
|
+
/**
|
|
516
|
+
* Listen to log messages emitted by the ServiceManager itself
|
|
517
|
+
*
|
|
518
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
519
|
+
*
|
|
520
|
+
* @returns {Function} Unsubscribe function
|
|
521
|
+
*/
|
|
522
|
+
onManagerLogEvent(listener) {
|
|
523
|
+
return this.logger.on(LOG, listener);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Listen to all log messages (both manager and services)
|
|
528
|
+
*
|
|
529
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
530
|
+
*
|
|
531
|
+
* @returns {Function} Unsubscribe function
|
|
532
|
+
*/
|
|
533
|
+
onLogEvent(listener) {
|
|
534
|
+
// Listen to both service and manager logs
|
|
535
|
+
const unsubscribeService = this.onServiceLogEvent(listener);
|
|
536
|
+
const unsubscribeManager = this.onManagerLogEvent(listener);
|
|
537
|
+
|
|
538
|
+
// Return combined unsubscribe function
|
|
539
|
+
return () => {
|
|
540
|
+
unsubscribeService();
|
|
541
|
+
unsubscribeManager();
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
506
545
|
/**
|
|
507
546
|
* Set log level for the ServiceManager itself
|
|
508
547
|
*
|
|
@@ -587,21 +626,21 @@ export class ServiceManager extends EventEmitter {
|
|
|
587
626
|
*/
|
|
588
627
|
_attachServiceEvents(name, instance) {
|
|
589
628
|
// Forward service events
|
|
590
|
-
instance.on(
|
|
591
|
-
this.emit(
|
|
629
|
+
instance.on(STATE_CHANGED, (/** @type {StateChangeEvent} */ data) => {
|
|
630
|
+
this.emit(SERVICE_STATE_CHANGED, { service: name, data });
|
|
592
631
|
});
|
|
593
632
|
|
|
594
|
-
instance.on(
|
|
595
|
-
this.emit(
|
|
633
|
+
instance.on(HEALTH_CHANGED, (/** @type {HealthChangeEvent} */ data) => {
|
|
634
|
+
this.emit(SERVICE_HEALTH_CHANGED, { service: name, data });
|
|
596
635
|
});
|
|
597
636
|
|
|
598
|
-
instance.on(
|
|
599
|
-
this.emit(
|
|
637
|
+
instance.on(ERROR, (/** @type {ServiceErrorEvent} */ data) => {
|
|
638
|
+
this.emit(SERVICE_ERROR, { service: name, data });
|
|
600
639
|
});
|
|
601
640
|
|
|
602
641
|
// Forward log events
|
|
603
|
-
instance.logger.on(
|
|
604
|
-
this.emit(
|
|
642
|
+
instance.logger.on(LOG, (/** @type {LogEvent} */ logEvent) => {
|
|
643
|
+
this.emit(SERVICE_LOG, logEvent);
|
|
605
644
|
});
|
|
606
645
|
}
|
|
607
646
|
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
export const
|
|
2
|
-
export const
|
|
1
|
+
export const STATE_CHANGED: "stateChanged";
|
|
2
|
+
export const HEALTH_CHANGED: "healthChanged";
|
|
3
|
+
export const ERROR: "error";
|
|
4
|
+
export const LOG: "log";
|
|
5
|
+
export const SERVICE_STATE_CHANGED: "service:stateChanged";
|
|
6
|
+
export const SERVICE_HEALTH_CHANGED: "service:healthChanged";
|
|
3
7
|
export const SERVICE_ERROR: "service:error";
|
|
4
8
|
export const SERVICE_LOG: "service:log";
|
|
5
9
|
export const ANY_LOG_LEVEL: "*";
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
//
|
|
2
|
-
export const
|
|
3
|
-
export const
|
|
1
|
+
// Service event names (what services emit)
|
|
2
|
+
export const STATE_CHANGED = 'stateChanged';
|
|
3
|
+
export const HEALTH_CHANGED = 'healthChanged';
|
|
4
|
+
export const ERROR = 'error';
|
|
5
|
+
export const LOG = 'log';
|
|
6
|
+
|
|
7
|
+
// Manager event names (what ServiceManager emits)
|
|
8
|
+
export const SERVICE_STATE_CHANGED = 'service:stateChanged';
|
|
9
|
+
export const SERVICE_HEALTH_CHANGED = 'service:healthChanged';
|
|
4
10
|
export const SERVICE_ERROR = 'service:error';
|
|
5
11
|
export const SERVICE_LOG = 'service:log';
|
|
6
12
|
|
|
13
|
+
|
|
7
14
|
export const ANY_LOG_LEVEL = '*';
|
|
8
15
|
export const ANY_SERVICE_NAME = '*';
|
|
@@ -339,7 +339,6 @@ console.log(features);
|
|
|
339
339
|
- `getAllEnv(options)` - Get combined environment variables (server only)
|
|
340
340
|
- `getPublicEnvByPrefix(prefix, options)` - Get public variables by prefix
|
|
341
341
|
- `getPrivateEnvByPrefix(prefix, options)` - Get private variables by prefix
|
|
342
|
-
- `getAllEnvByPrefix(prefix, options)` - Get combined variables by prefix
|
|
343
342
|
|
|
344
343
|
### Raw Access
|
|
345
344
|
|
|
@@ -421,4 +420,4 @@ interface DatabaseConfig {
|
|
|
421
420
|
}
|
|
422
421
|
|
|
423
422
|
const { database }: { database: DatabaseConfig } = getPrivateEnv();
|
|
424
|
-
```
|
|
423
|
+
```
|