@hkdigital/lib-sveltekit 0.2.16 → 0.2.18

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.
@@ -64,13 +64,15 @@ export default class Logger extends EventEmitter {
64
64
  */
65
65
  context(namespace: string, additionalContext: any): Logger;
66
66
  /**
67
- * Log an event of type LogEvent,
68
- * e.g. an event that was created by another Logger instance and should be
67
+ * Log an event from an event emitter of type LogEvent
68
+ *
69
+ * E.g. an event that was created by another Logger instance and should be
69
70
  * forwarded to this logger.
70
71
  *
71
- * @param {import('./typedef.js').LogEvent} logEvent
72
+ * @param {string} eventName
73
+ * @param {import('./typedef.js').LogEventData} eventData
72
74
  */
73
- logEvent(logEvent: import("./typedef.js").LogEvent): boolean;
75
+ logFromEvent(eventName: string, eventData: import("./typedef.js").LogEventData): boolean;
74
76
  #private;
75
77
  }
76
78
  import { EventEmitter } from '../events';
@@ -144,21 +144,23 @@ export default class Logger extends EventEmitter {
144
144
  }
145
145
 
146
146
  /**
147
- * Log an event of type LogEvent,
148
- * e.g. an event that was created by another Logger instance and should be
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
149
150
  * forwarded to this logger.
150
151
  *
151
- * @param {import('./typedef.js').LogEvent} logEvent
152
+ * @param {string} eventName
153
+ * @param {import('./typedef.js').LogEventData} eventData
152
154
  */
153
- logEvent( logEvent ) {
154
- const level = logEvent.level;
155
+ logFromEvent( eventName, eventData ) {
156
+ const level = eventData.level;
155
157
 
156
158
  // Check if this log level should be filtered
157
159
  if (LEVELS[level] < LEVELS[this.level]) {
158
160
  return false; // Below threshold, don't emit
159
161
  }
160
162
 
161
- this.#logEvent( logEvent );
163
+ this.#logEvent( { ...eventData, eventName });
162
164
  }
163
165
 
164
166
  /**
@@ -179,7 +181,7 @@ export default class Logger extends EventEmitter {
179
181
 
180
182
  const logEvent = {
181
183
  timestamp,
182
- service: this.name,
184
+ source: this.name,
183
185
  level,
184
186
  message,
185
187
  context: this.#hasContext ? this.#defaultContext : null,
@@ -1,14 +1,14 @@
1
1
  declare const _default: {};
2
2
  export default _default;
3
- export type LogEvent = {
3
+ export type LogEventData = {
4
4
  /**
5
5
  * - When the log event was created
6
6
  */
7
7
  timestamp: Date;
8
8
  /**
9
- * - Name of the service/component that logged this event
9
+ * - Name of the source where the event came from
10
10
  */
11
- service: string;
11
+ source: string;
12
12
  /**
13
13
  * - Log level (DEBUG, INFO, WARN, ERROR)
14
14
  */
@@ -18,7 +18,7 @@ export type LogEvent = {
18
18
  */
19
19
  message: string;
20
20
  /**
21
- * - Default context data from the logger (null if no context)
21
+ * Default context data from the logger (null if no context)
22
22
  */
23
23
  context: any | null;
24
24
  /**
@@ -26,3 +26,9 @@ export type LogEvent = {
26
26
  */
27
27
  details?: any;
28
28
  };
29
+ /**
30
+ * eventName - Original event name if log came from an event (optional)
31
+ */
32
+ export type LogEvent = LogEventData & {
33
+ eventName?: string;
34
+ };
@@ -1,12 +1,17 @@
1
-
2
1
  /**
3
- * @typedef {Object} LogEvent
2
+ * @typedef {Object} LogEventData
4
3
  * @property {Date} timestamp - When the log event was created
5
- * @property {string} service - Name of the service/component that logged this event
4
+ * @property {string} source - Name of the source where the event came from
6
5
  * @property {string} level - Log level (DEBUG, INFO, WARN, ERROR)
7
6
  * @property {string} message - The log message
8
- * @property {Object|null} context - Default context data from the logger (null if no context)
7
+ * @property {Object|null} context
8
+ * Default context data from the logger (null if no context)
9
9
  * @property {*} [details] - Additional details provided with the log (optional)
10
10
  */
11
11
 
12
+ /**
13
+ * @typedef {LogEventData & { eventName?: string }} LogEvent
14
+ * eventName - Original event name if log came from an event (optional)
15
+ */
16
+
12
17
  export default {};
@@ -413,7 +413,6 @@ export class ServiceBase extends EventEmitter {
413
413
  this.state = newState;
414
414
 
415
415
  this.emit('stateChanged', {
416
- service: this.name,
417
416
  oldState,
418
417
  newState
419
418
  });
@@ -431,8 +430,8 @@ export class ServiceBase extends EventEmitter {
431
430
 
432
431
  if (wasHealthy !== healthy) {
433
432
  this.emit('healthChanged', {
434
- service: this.name,
435
- healthy
433
+ healthy,
434
+ wasHealthy
436
435
  });
437
436
  }
438
437
  }
@@ -455,7 +454,6 @@ export class ServiceBase extends EventEmitter {
455
454
  });
456
455
 
457
456
  this.emit('error', {
458
- service: this.name,
459
457
  operation,
460
458
  error
461
459
  });
@@ -551,20 +551,21 @@ export class ServiceManager extends EventEmitter {
551
551
  _attachServiceEvents(name, instance) {
552
552
  // Forward service events
553
553
  instance.on('stateChanged', (data) => {
554
- this.emit('service:stateChanged', { ...data, service: name });
554
+ this.emit('service:stateChanged', { service: name, data });
555
555
  });
556
556
 
557
557
  instance.on('healthChanged', (data) => {
558
- this.emit('service:healthChanged', { ...data, service: name });
558
+ this.emit('service:healthChanged', { service: name, data });
559
559
  });
560
560
 
561
561
  instance.on('error', (data) => {
562
- this.emit('service:error', { ...data, service: name });
562
+ this.emit('service:error', { service: name, data });
563
563
  });
564
564
 
565
565
  // Forward log events
566
+
566
567
  instance.logger.on('log', (logEvent) => {
567
- this.emit('service:log', { ...logEvent, service: name });
568
+ this.emit('service:log', logEvent);
568
569
  });
569
570
  }
570
571
 
@@ -62,12 +62,9 @@ export type HealthStatus = {
62
62
  */
63
63
  key?: any;
64
64
  };
65
- /**
66
- * Event emitted when service state changes
67
- */
68
65
  export type StateChangeEvent = {
69
66
  /**
70
- * - Service name
67
+ * - Service name (added by ServiceManager)
71
68
  */
72
69
  service: string;
73
70
  /**
@@ -79,18 +76,19 @@ export type StateChangeEvent = {
79
76
  */
80
77
  newState: string;
81
78
  };
82
- /**
83
- * Event emitted when service health changes
84
- */
85
79
  export type HealthChangeEvent = {
86
80
  /**
87
- * - Service name
81
+ * - Service name (added by ServiceManager)
88
82
  */
89
83
  service: string;
90
84
  /**
91
- * - New health status
85
+ * - Current health status
92
86
  */
93
87
  healthy: boolean;
88
+ /**
89
+ * - Previous health status
90
+ */
91
+ wasHealthy?: boolean;
94
92
  };
95
93
  /**
96
94
  * Event emitted when service encounters an error
@@ -81,20 +81,17 @@
81
81
  */
82
82
 
83
83
  /**
84
- * Event emitted when service state changes
85
- *
86
84
  * @typedef {Object} StateChangeEvent
87
- * @property {string} service - Service name
85
+ * @property {string} service - Service name (added by ServiceManager)
88
86
  * @property {string} oldState - Previous state
89
87
  * @property {string} newState - New state
90
88
  */
91
89
 
92
90
  /**
93
- * Event emitted when service health changes
94
- *
95
91
  * @typedef {Object} HealthChangeEvent
96
- * @property {string} service - Service name
97
- * @property {boolean} healthy - New health status
92
+ * @property {string} service - Service name (added by ServiceManager)
93
+ * @property {boolean} healthy - Current health status
94
+ * @property {boolean} [wasHealthy] - Previous health status
98
95
  */
99
96
 
100
97
  /**
@@ -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] - Additional context data to include with all logs
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] - Additional context data to include with all logs
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, service, timestamp } = logEvent;
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[${service}]`;
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, service, timestamp } = logEvent;
37
+ const { level, message, details, source, timestamp } = logEvent;
38
38
 
39
39
  const logData = {
40
- service,
40
+ source,
41
41
  timestamp,
42
42
  ...(details && { details })
43
43
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.2.16",
3
+ "version": "0.2.18",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"
@@ -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';