@hkdigital/lib-sveltekit 0.2.15 → 0.2.17
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/classes/logging/Logger.d.ts +7 -7
- package/dist/classes/logging/Logger.js +40 -7
- package/dist/classes/logging/typedef.d.ts +34 -0
- package/dist/classes/logging/typedef.js +17 -0
- package/dist/classes/services/ServiceBase.js +3 -3
- package/dist/classes/services/ServiceManager.js +9 -5
- package/dist/classes/svelte/network-loader/NetworkLoader.svelte.js +1 -1
- package/dist/logging/adapters/console.d.ts +2 -1
- package/dist/logging/adapters/console.js +4 -3
- package/dist/logging/adapters/pino.js +2 -2
- package/dist/logging/factories/client.d.ts +2 -2
- package/dist/logging/factories/client.js +3 -3
- package/dist/logging/factories/server.d.ts +2 -2
- package/dist/logging/factories/server.js +3 -3
- package/dist/logging/index.js +1 -0
- package/dist/util/http/http-request.js +6 -6
- package/package.json +1 -1
- package/dist/classes/services/_old/ServiceBase.d.ts +0 -153
- package/dist/classes/services/_old/ServiceBase.js +0 -409
- package/dist/classes/services/_old/ServiceManager.d.ts +0 -350
- package/dist/classes/services/_old/ServiceManager.js +0 -1114
- package/dist/classes/services/_old/constants.d.ts +0 -11
- package/dist/classes/services/_old/constants.js +0 -12
- package/dist/classes/services/_old/index.d.ts +0 -3
- package/dist/classes/services/_old/index.js +0 -5
@@ -64,15 +64,15 @@ export default class Logger extends EventEmitter {
|
|
64
64
|
*/
|
65
65
|
context(namespace: string, additionalContext: any): Logger;
|
66
66
|
/**
|
67
|
-
*
|
67
|
+
* Log an event from an event emitter of type LogEvent
|
68
68
|
*
|
69
|
-
*
|
70
|
-
*
|
71
|
-
*
|
72
|
-
* @
|
73
|
-
* @
|
69
|
+
* E.g. an event that was created by another Logger instance and should be
|
70
|
+
* forwarded to this logger.
|
71
|
+
*
|
72
|
+
* @param {string} eventName
|
73
|
+
* @param {import('./typedef.js').LogEventData} eventData
|
74
74
|
*/
|
75
|
-
|
75
|
+
logFromEvent(eventName: string, eventData: import("./typedef.js").LogEventData): boolean;
|
76
76
|
#private;
|
77
77
|
}
|
78
78
|
import { EventEmitter } from '../events';
|
@@ -84,7 +84,7 @@ export default class Logger extends EventEmitter {
|
|
84
84
|
* @returns {boolean} True if the log was emitted
|
85
85
|
*/
|
86
86
|
debug(message, details) {
|
87
|
-
return this
|
87
|
+
return this.#log(DEBUG, message, details);
|
88
88
|
}
|
89
89
|
|
90
90
|
/**
|
@@ -95,7 +95,7 @@ export default class Logger extends EventEmitter {
|
|
95
95
|
* @returns {boolean} True if the log was emitted
|
96
96
|
*/
|
97
97
|
info(message, details) {
|
98
|
-
return this
|
98
|
+
return this.#log(INFO, message, details);
|
99
99
|
}
|
100
100
|
|
101
101
|
/**
|
@@ -106,7 +106,7 @@ export default class Logger extends EventEmitter {
|
|
106
106
|
* @returns {boolean} True if the log was emitted
|
107
107
|
*/
|
108
108
|
warn(message, details) {
|
109
|
-
return this
|
109
|
+
return this.#log(WARN, message, details);
|
110
110
|
}
|
111
111
|
|
112
112
|
/**
|
@@ -117,7 +117,7 @@ export default class Logger extends EventEmitter {
|
|
117
117
|
* @returns {boolean} True if the log was emitted
|
118
118
|
*/
|
119
119
|
error(message, details) {
|
120
|
-
return this
|
120
|
+
return this.#log(ERROR, message, details);
|
121
121
|
}
|
122
122
|
|
123
123
|
/**
|
@@ -143,6 +143,26 @@ export default class Logger extends EventEmitter {
|
|
143
143
|
return new Logger(this.name, this.level, mergedContext);
|
144
144
|
}
|
145
145
|
|
146
|
+
/**
|
147
|
+
* Log an event from an event emitter of type LogEvent
|
148
|
+
*
|
149
|
+
* E.g. an event that was created by another Logger instance and should be
|
150
|
+
* forwarded to this logger.
|
151
|
+
*
|
152
|
+
* @param {string} eventName
|
153
|
+
* @param {import('./typedef.js').LogEventData} eventData
|
154
|
+
*/
|
155
|
+
logFromEvent( eventName, eventData ) {
|
156
|
+
const level = eventData.level;
|
157
|
+
|
158
|
+
// Check if this log level should be filtered
|
159
|
+
if (LEVELS[level] < LEVELS[this.level]) {
|
160
|
+
return false; // Below threshold, don't emit
|
161
|
+
}
|
162
|
+
|
163
|
+
this.#logEvent( { ...eventData, eventName });
|
164
|
+
}
|
165
|
+
|
146
166
|
/**
|
147
167
|
* Internal logging method
|
148
168
|
*
|
@@ -150,18 +170,18 @@ export default class Logger extends EventEmitter {
|
|
150
170
|
* @param {string} message - Log message
|
151
171
|
* @param {*} [details] - Additional details to include in the log
|
152
172
|
* @returns {boolean} True if the log was emitted, false if filtered
|
153
|
-
* @private
|
154
173
|
*/
|
155
|
-
|
174
|
+
#log(level, message, details) {
|
156
175
|
// Check if this log level should be filtered
|
157
176
|
if (LEVELS[level] < LEVELS[this.level]) {
|
158
177
|
return false; // Below threshold, don't emit
|
159
178
|
}
|
160
179
|
|
161
180
|
const timestamp = new Date();
|
181
|
+
|
162
182
|
const logEvent = {
|
163
183
|
timestamp,
|
164
|
-
|
184
|
+
source: this.name,
|
165
185
|
level,
|
166
186
|
message,
|
167
187
|
context: this.#hasContext ? this.#defaultContext : null,
|
@@ -174,4 +194,17 @@ export default class Logger extends EventEmitter {
|
|
174
194
|
|
175
195
|
return true;
|
176
196
|
}
|
197
|
+
|
198
|
+
/**
|
199
|
+
* Internal event loggin method
|
200
|
+
*
|
201
|
+
* @param {import('./typedef.js').LogEvent} logEvent
|
202
|
+
*/
|
203
|
+
#logEvent(logEvent) {
|
204
|
+
// Emit as both specific level event and generic 'log' event
|
205
|
+
this.emit(logEvent.level, logEvent);
|
206
|
+
this.emit('log', logEvent);
|
207
|
+
|
208
|
+
return true;
|
209
|
+
}
|
177
210
|
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
declare const _default: {};
|
2
|
+
export default _default;
|
3
|
+
export type LogEventData = {
|
4
|
+
/**
|
5
|
+
* - When the log event was created
|
6
|
+
*/
|
7
|
+
timestamp: Date;
|
8
|
+
/**
|
9
|
+
* - Name of the source where the event came from
|
10
|
+
*/
|
11
|
+
source: string;
|
12
|
+
/**
|
13
|
+
* - Log level (DEBUG, INFO, WARN, ERROR)
|
14
|
+
*/
|
15
|
+
level: string;
|
16
|
+
/**
|
17
|
+
* - The log message
|
18
|
+
*/
|
19
|
+
message: string;
|
20
|
+
/**
|
21
|
+
* Default context data from the logger (null if no context)
|
22
|
+
*/
|
23
|
+
context: any | null;
|
24
|
+
/**
|
25
|
+
* - Additional details provided with the log (optional)
|
26
|
+
*/
|
27
|
+
details?: any;
|
28
|
+
};
|
29
|
+
/**
|
30
|
+
* eventName - Original event name if log came from an event (optional)
|
31
|
+
*/
|
32
|
+
export type LogEvent = LogEventData & {
|
33
|
+
eventName?: string;
|
34
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/**
|
2
|
+
* @typedef {Object} LogEventData
|
3
|
+
* @property {Date} timestamp - When the log event was created
|
4
|
+
* @property {string} source - Name of the source where the event came from
|
5
|
+
* @property {string} level - Log level (DEBUG, INFO, WARN, ERROR)
|
6
|
+
* @property {string} message - The log message
|
7
|
+
* @property {Object|null} context
|
8
|
+
* Default context data from the logger (null if no context)
|
9
|
+
* @property {*} [details] - Additional details provided with the log (optional)
|
10
|
+
*/
|
11
|
+
|
12
|
+
/**
|
13
|
+
* @typedef {LogEventData & { eventName?: string }} LogEvent
|
14
|
+
* eventName - Original event name if log came from an event (optional)
|
15
|
+
*/
|
16
|
+
|
17
|
+
export default {};
|
@@ -413,7 +413,7 @@ export class ServiceBase extends EventEmitter {
|
|
413
413
|
this.state = newState;
|
414
414
|
|
415
415
|
this.emit('stateChanged', {
|
416
|
-
|
416
|
+
source: this.name,
|
417
417
|
oldState,
|
418
418
|
newState
|
419
419
|
});
|
@@ -431,7 +431,7 @@ export class ServiceBase extends EventEmitter {
|
|
431
431
|
|
432
432
|
if (wasHealthy !== healthy) {
|
433
433
|
this.emit('healthChanged', {
|
434
|
-
|
434
|
+
source: this.name,
|
435
435
|
healthy
|
436
436
|
});
|
437
437
|
}
|
@@ -455,7 +455,7 @@ export class ServiceBase extends EventEmitter {
|
|
455
455
|
});
|
456
456
|
|
457
457
|
this.emit('error', {
|
458
|
-
|
458
|
+
source: this.name,
|
459
459
|
operation,
|
460
460
|
error
|
461
461
|
});
|
@@ -551,20 +551,24 @@ export class ServiceManager extends EventEmitter {
|
|
551
551
|
_attachServiceEvents(name, instance) {
|
552
552
|
// Forward service events
|
553
553
|
instance.on('stateChanged', (data) => {
|
554
|
-
|
554
|
+
const eventName = 'service:stateChanged';
|
555
|
+
this.emit(eventName, { ...data, eventName, source: name });
|
555
556
|
});
|
556
557
|
|
557
558
|
instance.on('healthChanged', (data) => {
|
558
|
-
|
559
|
+
const eventName = 'service:healthChanged';
|
560
|
+
this.emit(eventName, { ...data, eventName, source: name });
|
559
561
|
});
|
560
562
|
|
561
563
|
instance.on('error', (data) => {
|
562
|
-
|
564
|
+
const eventName = 'service:error';
|
565
|
+
this.emit(eventName, { ...data, eventName, source: name });
|
563
566
|
});
|
564
567
|
|
565
568
|
// Forward log events
|
566
|
-
instance.logger.on('log', (
|
567
|
-
|
569
|
+
instance.logger.on('log', (data) => {
|
570
|
+
const eventName = 'service:log';
|
571
|
+
this.emit(eventName, { ...data, eventName, source: name });
|
568
572
|
});
|
569
573
|
}
|
570
574
|
|
@@ -7,7 +7,8 @@ export class ConsoleAdapter {
|
|
7
7
|
*
|
8
8
|
* @param {Object} [options] - Browser configuration options
|
9
9
|
* @param {string} [options.level] - Minimum log level
|
10
|
-
* @param {Object} [options.context]
|
10
|
+
* @param {Object} [options.context]
|
11
|
+
* Additional context data to include with all logs
|
11
12
|
*/
|
12
13
|
constructor(options?: {
|
13
14
|
level?: string;
|
@@ -9,7 +9,8 @@ export class ConsoleAdapter {
|
|
9
9
|
*
|
10
10
|
* @param {Object} [options] - Browser configuration options
|
11
11
|
* @param {string} [options.level] - Minimum log level
|
12
|
-
* @param {Object} [options.context]
|
12
|
+
* @param {Object} [options.context]
|
13
|
+
* Additional context data to include with all logs
|
13
14
|
*/
|
14
15
|
constructor(options = {}) {
|
15
16
|
this.level = options.level || 'info';
|
@@ -23,7 +24,7 @@ export class ConsoleAdapter {
|
|
23
24
|
*/
|
24
25
|
handleLog(logEvent) {
|
25
26
|
// eslint-disable-next-line no-unused-vars
|
26
|
-
const { level, message, details,
|
27
|
+
const { level, message, details, source, timestamp } = logEvent;
|
27
28
|
|
28
29
|
// Filter by level
|
29
30
|
if (LEVELS[level] < LEVELS[this.level]) {
|
@@ -32,7 +33,7 @@ export class ConsoleAdapter {
|
|
32
33
|
|
33
34
|
// Use browser console styling
|
34
35
|
const styles = this._getStyles(level);
|
35
|
-
const prefix = `%c[${
|
36
|
+
const prefix = `%c[${source}]`;
|
36
37
|
|
37
38
|
// Merge context with details
|
38
39
|
const logData = details
|
@@ -34,10 +34,10 @@ export class PinoAdapter {
|
|
34
34
|
* @param {Object} logEvent - Log event from Logger
|
35
35
|
*/
|
36
36
|
handleLog(logEvent) {
|
37
|
-
const { level, message, details,
|
37
|
+
const { level, message, details, source, timestamp } = logEvent;
|
38
38
|
|
39
39
|
const logData = {
|
40
|
-
|
40
|
+
source,
|
41
41
|
timestamp,
|
42
42
|
...(details && { details })
|
43
43
|
};
|
@@ -1,10 +1,10 @@
|
|
1
1
|
/**
|
2
2
|
* Create a client-side logger with console adapter
|
3
3
|
*
|
4
|
-
* @param {string}
|
4
|
+
* @param {string} name
|
5
5
|
* @param {string} [level=INFO] - Initial log level
|
6
6
|
* @param {Object} [consoleOptions] - Additional console options
|
7
7
|
* @returns {Logger} Configured logger instance
|
8
8
|
*/
|
9
|
-
export function createClientLogger(
|
9
|
+
export function createClientLogger(name: string, level?: string, consoleOptions?: any): Logger;
|
10
10
|
import { Logger } from '../../classes/logging';
|
@@ -5,13 +5,13 @@ import { INFO } from '../constants.js';
|
|
5
5
|
/**
|
6
6
|
* Create a client-side logger with console adapter
|
7
7
|
*
|
8
|
-
* @param {string}
|
8
|
+
* @param {string} name
|
9
9
|
* @param {string} [level=INFO] - Initial log level
|
10
10
|
* @param {Object} [consoleOptions] - Additional console options
|
11
11
|
* @returns {Logger} Configured logger instance
|
12
12
|
*/
|
13
|
-
export function createClientLogger(
|
14
|
-
const logger = new Logger(
|
13
|
+
export function createClientLogger(name, level = INFO, consoleOptions = {}) {
|
14
|
+
const logger = new Logger(name, level);
|
15
15
|
const adapter = new ConsoleAdapter({ ...consoleOptions, level });
|
16
16
|
|
17
17
|
// Connect adapter to logger events
|
@@ -1,10 +1,10 @@
|
|
1
1
|
/**
|
2
2
|
* Create a server-side logger with pino adapter
|
3
3
|
*
|
4
|
-
* @param {string}
|
4
|
+
* @param {string} name
|
5
5
|
* @param {string} [level=INFO] - Initial log level
|
6
6
|
* @param {Object} [pinoOptions] - Additional pino options
|
7
7
|
* @returns {Logger} Configured logger instance
|
8
8
|
*/
|
9
|
-
export function createServerLogger(
|
9
|
+
export function createServerLogger(name: string, level?: string, pinoOptions?: any): Logger;
|
10
10
|
import { Logger } from '../../classes/logging';
|
@@ -6,13 +6,13 @@ import { INFO } from '../constants.js';
|
|
6
6
|
/**
|
7
7
|
* Create a server-side logger with pino adapter
|
8
8
|
*
|
9
|
-
* @param {string}
|
9
|
+
* @param {string} name
|
10
10
|
* @param {string} [level=INFO] - Initial log level
|
11
11
|
* @param {Object} [pinoOptions] - Additional pino options
|
12
12
|
* @returns {Logger} Configured logger instance
|
13
13
|
*/
|
14
|
-
export function createServerLogger(
|
15
|
-
const logger = new Logger(
|
14
|
+
export function createServerLogger(name, level = INFO, pinoOptions = {}) {
|
15
|
+
const logger = new Logger(name, level);
|
16
16
|
const adapter = new PinoAdapter(pinoOptions);
|
17
17
|
|
18
18
|
// Connect adapter to logger events
|
package/dist/logging/index.js
CHANGED
@@ -356,12 +356,12 @@ export async function httpRequest(options) {
|
|
356
356
|
}
|
357
357
|
|
358
358
|
// Create stale info
|
359
|
-
const staleInfo = {
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
};
|
359
|
+
// const staleInfo = {
|
360
|
+
// isStale: false,
|
361
|
+
// fresh: null,
|
362
|
+
// timestamp: Date.now(),
|
363
|
+
// expires
|
364
|
+
// };
|
365
365
|
|
366
366
|
// Store response in cache
|
367
367
|
const cacheKeyParams = { url, ...headers };
|
package/package.json
CHANGED
@@ -1,153 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Base class for all services
|
3
|
-
*/
|
4
|
-
export default class ServiceBase {
|
5
|
-
/**
|
6
|
-
* Create a new service
|
7
|
-
*
|
8
|
-
* @param {string} name - Service name
|
9
|
-
* @param {Object} [options] - Service options
|
10
|
-
* @param {string} [options.logLevel=INFO] - Initial log level
|
11
|
-
*/
|
12
|
-
constructor(name: string, options?: {
|
13
|
-
logLevel?: string;
|
14
|
-
});
|
15
|
-
/**
|
16
|
-
* Service name
|
17
|
-
* @type {string}
|
18
|
-
*/
|
19
|
-
name: string;
|
20
|
-
/**
|
21
|
-
* Event emitter for service events
|
22
|
-
* @type {EventEmitter}
|
23
|
-
*/
|
24
|
-
events: EventEmitter;
|
25
|
-
/**
|
26
|
-
* Current service state
|
27
|
-
* @type {string}
|
28
|
-
*/
|
29
|
-
state: string;
|
30
|
-
/**
|
31
|
-
* Last error that occurred
|
32
|
-
* @type {Error|null}
|
33
|
-
*/
|
34
|
-
error: Error | null;
|
35
|
-
/**
|
36
|
-
* Last stable state before error
|
37
|
-
* @type {string|null}
|
38
|
-
* @private
|
39
|
-
*/
|
40
|
-
private _preErrorState;
|
41
|
-
/**
|
42
|
-
* Service logger
|
43
|
-
* @type {Logger}
|
44
|
-
*/
|
45
|
-
logger: Logger;
|
46
|
-
/**
|
47
|
-
* Set the service log level
|
48
|
-
*
|
49
|
-
* @param {string} level - New log level
|
50
|
-
* @returns {boolean} True if level was set, false if invalid
|
51
|
-
*/
|
52
|
-
setLogLevel(level: string): boolean;
|
53
|
-
/**
|
54
|
-
* Initialize the service
|
55
|
-
*
|
56
|
-
* @param {Object} [config] - Service configuration
|
57
|
-
* @returns {Promise<boolean>} True if initialized successfully
|
58
|
-
*/
|
59
|
-
initialize(config?: any): Promise<boolean>;
|
60
|
-
/**
|
61
|
-
* Start the service
|
62
|
-
*
|
63
|
-
* @returns {Promise<boolean>} True if started successfully
|
64
|
-
*/
|
65
|
-
start(): Promise<boolean>;
|
66
|
-
/**
|
67
|
-
* Stop the service
|
68
|
-
*
|
69
|
-
* @returns {Promise<boolean>} True if stopped successfully
|
70
|
-
*/
|
71
|
-
stop(): Promise<boolean>;
|
72
|
-
/**
|
73
|
-
* Recover the service
|
74
|
-
*
|
75
|
-
* @returns {Promise<boolean>} True if stopped successfully
|
76
|
-
*/
|
77
|
-
recover(): Promise<boolean>;
|
78
|
-
/**
|
79
|
-
* Destroy the service
|
80
|
-
*
|
81
|
-
* @returns {Promise<boolean>} True if destroyed successfully
|
82
|
-
*/
|
83
|
-
destroy(): Promise<boolean>;
|
84
|
-
/**
|
85
|
-
* Add an event listener
|
86
|
-
*
|
87
|
-
* @param {string} eventName - Event name
|
88
|
-
* @param {Function} handler - Event handler
|
89
|
-
* @returns {Function} Unsubscribe function
|
90
|
-
*/
|
91
|
-
on(eventName: string, handler: Function): Function;
|
92
|
-
/**
|
93
|
-
* Emit an event
|
94
|
-
*
|
95
|
-
* @param {string} eventName - Event name
|
96
|
-
* @param {*} data - Event data
|
97
|
-
* @returns {boolean} True if event had listeners
|
98
|
-
*/
|
99
|
-
emit(eventName: string, data: any): boolean;
|
100
|
-
/**
|
101
|
-
* Initialize the service (to be overridden)
|
102
|
-
*
|
103
|
-
* @protected
|
104
|
-
* @param {Object} config - Service configuration
|
105
|
-
* @returns {Promise<void>}
|
106
|
-
*/
|
107
|
-
protected _init(config: any): Promise<void>;
|
108
|
-
/**
|
109
|
-
* Start the service (to be overridden)
|
110
|
-
*
|
111
|
-
* @protected
|
112
|
-
* @returns {Promise<void>}
|
113
|
-
*/
|
114
|
-
protected _start(): Promise<void>;
|
115
|
-
/**
|
116
|
-
* Stop the service (to be overridden)
|
117
|
-
*
|
118
|
-
* @protected
|
119
|
-
* @returns {Promise<void>}
|
120
|
-
*/
|
121
|
-
protected _stop(): Promise<void>;
|
122
|
-
/**
|
123
|
-
* Destroy the service (to be overridden)
|
124
|
-
*
|
125
|
-
* @protected
|
126
|
-
* @returns {Promise<void>}
|
127
|
-
*/
|
128
|
-
protected _destroy(): Promise<void>;
|
129
|
-
/**
|
130
|
-
* Recover the service from an error (to be overridden)
|
131
|
-
*
|
132
|
-
* @protected
|
133
|
-
* @returns {Promise<void>}
|
134
|
-
*/
|
135
|
-
protected _recover(): Promise<void>;
|
136
|
-
/**
|
137
|
-
* Set the service state
|
138
|
-
*
|
139
|
-
* @private
|
140
|
-
* @param {string} state - New state
|
141
|
-
*/
|
142
|
-
private _setState;
|
143
|
-
/**
|
144
|
-
* Set an error state
|
145
|
-
*
|
146
|
-
* @private
|
147
|
-
* @param {string} operation - Operation that failed
|
148
|
-
* @param {Error} error - Error that occurred
|
149
|
-
*/
|
150
|
-
private _setError;
|
151
|
-
}
|
152
|
-
import { EventEmitter } from '../../events';
|
153
|
-
import { Logger } from '../../logging';
|