@hkdigital/lib-sveltekit 0.2.16 → 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.
@@ -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,7 @@ export class ServiceBase extends EventEmitter {
413
413
  this.state = newState;
414
414
 
415
415
  this.emit('stateChanged', {
416
- service: this.name,
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
- service: this.name,
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
- service: this.name,
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
- this.emit('service:stateChanged', { ...data, service: name });
554
+ const eventName = 'service:stateChanged';
555
+ this.emit(eventName, { ...data, eventName, source: name });
555
556
  });
556
557
 
557
558
  instance.on('healthChanged', (data) => {
558
- this.emit('service:healthChanged', { ...data, service: name });
559
+ const eventName = 'service:healthChanged';
560
+ this.emit(eventName, { ...data, eventName, source: name });
559
561
  });
560
562
 
561
563
  instance.on('error', (data) => {
562
- this.emit('service:error', { ...data, service: name });
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', (logEvent) => {
567
- this.emit('service:log', { ...logEvent, service: name });
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] - 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.17",
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';