@mcesystems/logging-g4 1.0.70 → 1.0.72

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/Example.md ADDED
@@ -0,0 +1,44 @@
1
+ # Logging examples
2
+
3
+ This document describes the examples in `src/example/` and how to run them.
4
+
5
+ ## sendLog.ts
6
+
7
+ **Purpose:** Send a single log event using the default batch schedule.
8
+
9
+ **Setup:** Set in `.env` or environment:
10
+
11
+ - `CREDENTIALS_PATH` — path to credentials file
12
+ - `LOGGING_API_URL` — logging API base URL
13
+ - `AUTH_API_URL` — auth API URL
14
+
15
+ Optional for this example: `TIME_BETWEEN_LOG`, `MAX_OFFLINE_TIME`, `CACHED_EVENTS_PATH` (defaults: 10 min, 1 hour; `CACHED_EVENTS_PATH` must be set).
16
+
17
+ **Run (from package root):**
18
+
19
+ ```bash
20
+ pnpm exec tsx src/example/sendLog.ts
21
+ ```
22
+
23
+ **Behavior:** Creates a `LogClient`, calls `logEvent()` once with a test event. Returns `"delayed"`; the event is queued and sent at the next cron boundary.
24
+
25
+ ## sendBatchAfterOneMinute.ts
26
+
27
+ **Purpose:** Queue a batch of log events with a 15-second gap between each, and show the time when the batch is really sent (at the next 1-minute boundary).
28
+
29
+ **Setup:** Same as sendLog. The example sets `TIME_BETWEEN_LOG=60000` (1 minute) and `CACHED_EVENTS_PATH` to a temp file if not set.
30
+
31
+ **Run (from package root):**
32
+
33
+ ```bash
34
+ pnpm exec tsx src/example/sendBatchAfterOneMinute.ts
35
+ ```
36
+
37
+ **Behavior:**
38
+
39
+ 1. Logs the scheduled send time (next 1-minute boundary).
40
+ 2. Queues three events: "First event", "Second event", "Third event", with 15 seconds between each. Each log line includes the queue time.
41
+ 3. Waits until after the boundary so the cron sends the batch.
42
+ 4. Logs "Batch was sent at: <time>" with the boundary time.
43
+
44
+ Use this to verify batching and cron-style send times.
package/README.md CHANGED
@@ -1,23 +1,71 @@
1
1
  # @mcesystems/logging-g4
2
2
 
3
- Node-only logging client that sends events to the MCE logging service.
3
+ ## 1. Description
4
4
 
5
- ## Install
5
+ **What it does:** Node-only logging client that sends events to the MCE logging service. Events are queued and sent in batches on a cron-style schedule (e.g. every 10 minutes at fixed clock boundaries). Failed sends (401, network errors) are persisted to disk and retried on the next run. If the service stays offline longer than `MAX_OFFLINE_TIME`, the client throws.
6
6
 
7
- ```
7
+ **When it's used:** Use this package when your application needs to send structured log events to the MCE logging service with batching, persistence, and offline handling—for example device management tools or services that must not lose logs when the API is temporarily unavailable.
8
+
9
+ ## 2. Install
10
+
11
+ ```bash
8
12
  pnpm add @mcesystems/logging-g4
9
13
  ```
10
14
 
11
- ## Usage (Node only)
15
+ **Requirements:** Node.js 18+. This package uses Node APIs (`fs`, `os`, `path`) and is not compatible with browsers or other non-Node runtimes.
16
+
17
+ ## 3. Resources
18
+
19
+ No binary resources are required. You need a credentials file (see Setup) and network access to the auth and logging APIs.
20
+
21
+ ## 4. Examples
22
+
23
+ **Single event**
24
+
25
+ ```bash
26
+ pnpm exec tsx src/example/sendLog.ts
27
+ ```
28
+
29
+ Uses `CREDENTIALS_PATH`, `LOGGING_API_URL`, `AUTH_API_URL` from `.env` or environment. Sends one event; returns `"delayed"` (event queued for next scheduled send).
12
30
 
13
- This package uses Node APIs (`fs`, `os`, `path`) and environment variables. It is not
14
- compatible with browsers or other non-Node runtimes.
31
+ **Batch after 1 minute (with 15s gap between events)**
15
32
 
33
+ ```bash
34
+ pnpm exec tsx src/example/sendBatchAfterOneMinute.ts
16
35
  ```
36
+
37
+ Queues three events with a 15-second gap between each; shows the time when the batch is sent at the next 1-minute boundary. Sets `TIME_BETWEEN_LOG=60000` and `CACHED_EVENTS_PATH` to a temp file if not set.
38
+
39
+ For step-by-step explanations and scenarios, see [Example.md](./Example.md).
40
+
41
+ ## 5. API
42
+
43
+ ### LogClient
44
+
45
+ **Constructor**
46
+
47
+ - **Input:** none
48
+ - **Output:** new `LogClient` instance. Call `logEvent()` after creation; `init()` runs on first `logEvent()` if needed.
49
+
50
+ **logEvent(event)**
51
+
52
+ - **Input:** `event` — object with `event_severity`, `event_type`, `event_info`, `context` (and optional fields). Omit `event_id`, `event_time`, `framework_id`, `event_source`; they are set by the client.
53
+ - **Output:** `Promise<LogSendResult>` — always `"delayed"` (event queued for next scheduled send). No immediate send. The outcomes `"success"` and `"MAX_OFFLINE_TIME passed"` occur inside the cron job: when `MAX_OFFLINE_TIME` is exceeded, `sendBatch()` throws so the process receives the exception.
54
+
55
+ ### LogSendResult
56
+
57
+ Type: `"success" | "MAX_OFFLINE_TIME passed" | "delayed"`. Exported for typing; `logEvent()` returns `"delayed"` only.
58
+
59
+ ## 6. Flow
60
+
61
+ Example flow: create client, queue several events, let the cron send the batch at the next boundary.
62
+
63
+ ```typescript
17
64
  import { LogSeverity } from "@mcesystems/logging-interfaces/lib/sdk";
18
65
  import { LogClient } from "@mcesystems/logging-g4";
19
66
 
20
67
  const logClient = new LogClient();
68
+
21
69
  await logClient.logEvent({
22
70
  event_severity: LogSeverity.Info,
23
71
  event_type: "device_connected",
@@ -26,8 +74,33 @@ await logClient.logEvent({
26
74
  source: "device-manager",
27
75
  timestamp: new Date().toISOString(),
28
76
  },
29
- context: {
30
- deviceId: "abc123",
31
- },
77
+ context: { deviceId: "abc123" },
32
78
  });
79
+ // result is "delayed" — batch will be sent at next TIME_BETWEEN_LOG boundary
80
+
81
+ await logClient.logEvent({
82
+ event_severity: LogSeverity.Info,
83
+ event_type: "device_disconnected",
84
+ event_info: { message: "Device disconnected", timestamp: new Date().toISOString() },
85
+ context: { deviceId: "abc123" },
86
+ });
87
+ // both events are queued; cron sends the batch at the next boundary
33
88
  ```
89
+
90
+ ## 7. Setup
91
+
92
+ **Environment variables**
93
+
94
+ - **TIME_BETWEEN_LOG** (ms): Interval between batch send attempts. Sends run at fixed clock boundaries (e.g. 10 min → 13:50, 14:00, 14:10). Default: 10 minutes.
95
+ - **MAX_OFFLINE_TIME** (ms): After this many ms since the first send failure, the client throws. If the batch was successfully persisted and still within this window, no throw. Default: 1 hour.
96
+ - **CACHED_EVENTS_PATH**: Path to a single JSON file for pending (delayed) events. **Required.** Delayed batches are written here on send failure and loaded on the next run; file is deleted after a successful send.
97
+ - **CREDENTIALS_PATH**: Path to credentials file used by `@mcesystems/auth-g4`. Required for auth.
98
+ - **LOGGING_API_URL**: Logging API base URL. Required.
99
+ - **AUTH_API_URL**: Auth API URL for token. Required.
100
+
101
+ **Resources location:** None. Credentials file path is given by `CREDENTIALS_PATH`. Pending events file path is given by `CACHED_EVENTS_PATH`.
102
+
103
+ ## 8. TODO
104
+
105
+ - [ ] Optional: Expand Example.md with more scenarios (e.g. offline handling, MAX_OFFLINE_TIME).
106
+ - [ ] Optional: Token refresh on 401 before persisting and retrying.