voltlog-io 1.0.1 â 1.0.3
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/LICENSE +21 -21
- package/README.md +316 -65
- package/dist/index.d.mts +374 -5
- package/dist/index.d.ts +374 -5
- package/dist/index.js +708 -247
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +592 -55
- package/dist/index.mjs.map +1 -1
- package/package.json +75 -78
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Rohit Tiwari
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Rohit Tiwari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,65 +1,316 @@
|
|
|
1
|
-
# VoltLog
|
|
2
|
-
|
|
3
|
-
#### Structured logger for real-time infrastructure
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/voltlog-io)
|
|
6
|
-
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
###
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
1
|
+
# VoltLog
|
|
2
|
+
|
|
3
|
+
#### Structured logger for real-time infrastructure â zero dependencies, type-safe, OCPP-aware
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/voltlog-io)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://www.npmjs.com/package/voltlog-io)
|
|
8
|
+
|
|
9
|
+
**VoltLog** is a modern, lightweight, and type-safe structured logger designed for high-throughput, real-time systems like IoT platforms, WebSocket servers (OCPP), and microservices.
|
|
10
|
+
|
|
11
|
+
## ð Full Documentation
|
|
12
|
+
|
|
13
|
+
For detailed guides, API reference, and advanced usage:
|
|
14
|
+
|
|
15
|
+
### [ð https://ocpp-ws-io.rohittiwari.me/docs/voltlog-io](https://ocpp-ws-io.rohittiwari.me/docs/voltlog-io)
|
|
16
|
+
|
|
17
|
+
## ðĶ Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install voltlog-io
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## ð Quick Start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createLogger, prettyTransport, consoleTransport } from "voltlog-io";
|
|
27
|
+
|
|
28
|
+
// Development â pretty colored output
|
|
29
|
+
const logger = createLogger({
|
|
30
|
+
level: "DEBUG",
|
|
31
|
+
transports: [prettyTransport()],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Production â structured JSON
|
|
35
|
+
const logger = createLogger({
|
|
36
|
+
level: "INFO",
|
|
37
|
+
transports: [consoleTransport()],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
logger.info("Server started", { port: 3000, env: "production" });
|
|
41
|
+
logger.error("Connection failed", new Error("ETIMEDOUT"));
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## ð Features
|
|
45
|
+
|
|
46
|
+
### Core Logging
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// All log levels
|
|
50
|
+
logger.trace("Entering function");
|
|
51
|
+
logger.debug("Parsed payload", { bytes: 1024 });
|
|
52
|
+
logger.info("User authenticated", { userId: "u-42" });
|
|
53
|
+
logger.warn("Rate limit approaching", { rate: 95 });
|
|
54
|
+
logger.error("Query failed", { query: "SELECT *" }, new Error("timeout"));
|
|
55
|
+
logger.fatal("Unrecoverable", new Error("OOM"));
|
|
56
|
+
|
|
57
|
+
// Runtime level control
|
|
58
|
+
logger.setLevel("WARN"); // change at runtime
|
|
59
|
+
logger.getLevel(); // â "WARN"
|
|
60
|
+
logger.isLevelEnabled("DEBUG"); // â false (useful to guard expensive computation)
|
|
61
|
+
|
|
62
|
+
// Timer helper â auto-logs duration
|
|
63
|
+
const timer = logger.startTimer();
|
|
64
|
+
await doExpensiveWork();
|
|
65
|
+
timer.done("Work completed", { items: 100 });
|
|
66
|
+
// â INFO Work completed { durationMs: 342, items: 100 }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Child Loggers
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const cpLogger = logger.child({ chargePointId: "CP-101" });
|
|
73
|
+
cpLogger.info("Connected");
|
|
74
|
+
// â context: { chargePointId: "CP-101" }
|
|
75
|
+
|
|
76
|
+
const sessionLogger = cpLogger.child({ sessionId: "sess-xyz" });
|
|
77
|
+
sessionLogger.info("Charging started");
|
|
78
|
+
// â context: { chargePointId: "CP-101", sessionId: "sess-xyz" }
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Error Cause Chain (ES2022)
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const rootCause = new Error("ECONNREFUSED 10.0.0.5:5432");
|
|
85
|
+
const dbError = new Error("DB connection failed", { cause: rootCause });
|
|
86
|
+
const appError = new Error("Request failed", { cause: dbError });
|
|
87
|
+
|
|
88
|
+
logger.error("Handler crashed", appError);
|
|
89
|
+
// â error.cause.cause.message = "ECONNREFUSED 10.0.0.5:5432" â root cause preserved!
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Custom ID Generator
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// Default: crypto.randomUUID() (fast, native)
|
|
96
|
+
const logger = createLogger({ transports: [...] });
|
|
97
|
+
|
|
98
|
+
// Custom generator
|
|
99
|
+
const logger = createLogger({
|
|
100
|
+
idGenerator: () => `log-${Date.now()}-${Math.random().toString(36).slice(2)}`,
|
|
101
|
+
transports: [...],
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Disable for max performance
|
|
105
|
+
const logger = createLogger({ idGenerator: false, transports: [...] });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## ð Transports (14 Built-in)
|
|
111
|
+
|
|
112
|
+
### Console & Pretty
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { consoleTransport, prettyTransport } from "voltlog-io";
|
|
116
|
+
|
|
117
|
+
consoleTransport(); // JSON to stdout
|
|
118
|
+
prettyTransport({ colors: true, timestamps: true }); // colored dev output
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### File (Daily + Size Rotation)
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { fileTransport } from "voltlog-io";
|
|
125
|
+
|
|
126
|
+
fileTransport({
|
|
127
|
+
dir: "./logs",
|
|
128
|
+
filename: "app-%DATE%.log", // daily rotation
|
|
129
|
+
maxSize: 10_000_000, // 10MB size rotation
|
|
130
|
+
});
|
|
131
|
+
// â logs/app-2026-02-28.log â logs/app-2026-02-28.1.log (at 10MB)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Ring Buffer (In-Memory Diagnostics)
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { ringBufferTransport } from "voltlog-io";
|
|
138
|
+
|
|
139
|
+
const ring = ringBufferTransport({ maxSize: 500 });
|
|
140
|
+
|
|
141
|
+
// Query buffered logs
|
|
142
|
+
ring.getEntries({ level: "ERROR" });
|
|
143
|
+
ring.getEntries({ since: Date.now() - 300_000, limit: 20 });
|
|
144
|
+
ring.clear();
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Webhook (with Batching & Retry)
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { webhookTransport, batchTransport } from "voltlog-io";
|
|
151
|
+
|
|
152
|
+
batchTransport(
|
|
153
|
+
webhookTransport({
|
|
154
|
+
url: "https://api.example.com/logs",
|
|
155
|
+
headers: { Authorization: "Bearer token" },
|
|
156
|
+
retry: true,
|
|
157
|
+
maxRetries: 3,
|
|
158
|
+
}),
|
|
159
|
+
{ batchSize: 50, flushIntervalMs: 5000 },
|
|
160
|
+
);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### OpenTelemetry (SigNoz, Jaeger, Grafana)
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { otelTransport, otelTraceMiddleware } from "voltlog-io";
|
|
167
|
+
|
|
168
|
+
const logger = createLogger({
|
|
169
|
+
middleware: [otelTraceMiddleware()], // auto-picks traceId/spanId
|
|
170
|
+
transports: [
|
|
171
|
+
otelTransport({
|
|
172
|
+
endpoint: "https://ingest.signoz.io",
|
|
173
|
+
headers: { "signoz-access-token": "YOUR_TOKEN" },
|
|
174
|
+
serviceName: "my-app",
|
|
175
|
+
resource: { "deployment.environment": "production" },
|
|
176
|
+
}),
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
// Every log auto-includes traceId + spanId from active OTel spans
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Loki (Grafana)
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { lokiTransport } from "voltlog-io";
|
|
186
|
+
|
|
187
|
+
lokiTransport({
|
|
188
|
+
host: "http://loki:3100",
|
|
189
|
+
labels: { app: "my-service", env: "prod" },
|
|
190
|
+
dynamicLabels: (entry) => ({ level: entry.levelName }),
|
|
191
|
+
includeMetadata: true, // context, error, correlationId
|
|
192
|
+
retry: true,
|
|
193
|
+
maxRetries: 3,
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Other Transports
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import {
|
|
201
|
+
datadogTransport, // Datadog Logs API
|
|
202
|
+
sentryTransport, // Sentry (errors + breadcrumbs)
|
|
203
|
+
slackTransport, // Slack webhook
|
|
204
|
+
discordTransport, // Discord webhook
|
|
205
|
+
redisTransport, // Redis Streams
|
|
206
|
+
jsonStreamTransport, // Node.js WritableStream
|
|
207
|
+
browserJsonStreamTransport, // Browser WritableStream
|
|
208
|
+
} from "voltlog-io";
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## ð§Đ Middleware (13 Built-in)
|
|
214
|
+
|
|
215
|
+
### Redaction
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import { redactionMiddleware } from "voltlog-io";
|
|
219
|
+
|
|
220
|
+
redactionMiddleware({
|
|
221
|
+
paths: ["password", "idToken", "authorization"],
|
|
222
|
+
deep: true, // search nested objects
|
|
223
|
+
replacement: "[REDACTED]",
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### AsyncLocalStorage Context
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import { asyncContextMiddleware } from "voltlog-io";
|
|
231
|
+
|
|
232
|
+
const asyncCtx = asyncContextMiddleware();
|
|
233
|
+
|
|
234
|
+
const logger = createLogger({
|
|
235
|
+
middleware: [asyncCtx.middleware],
|
|
236
|
+
transports: [prettyTransport()],
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// Set context once â propagates across all async boundaries
|
|
240
|
+
app.use((req, res, next) => {
|
|
241
|
+
asyncCtx.runInContext({ requestId: req.id, userId: req.user?.id }, next);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Anywhere downstream â no child() needed
|
|
245
|
+
logger.info("Processing order");
|
|
246
|
+
// â auto-includes { requestId, userId }
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Sampling & Rate Limiting
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { samplingMiddleware } from "voltlog-io";
|
|
253
|
+
|
|
254
|
+
samplingMiddleware({
|
|
255
|
+
maxPerWindow: 10, // max 10 logs per window
|
|
256
|
+
windowMs: 60_000, // per minute
|
|
257
|
+
priorityLevel: 40, // WARN+ always passes
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Other Middleware
|
|
262
|
+
|
|
263
|
+
```ts
|
|
264
|
+
import {
|
|
265
|
+
correlationIdMiddleware, // auto-generate/propagate correlation IDs
|
|
266
|
+
alertMiddleware, // trigger alerts on error spikes
|
|
267
|
+
deduplicationMiddleware, // suppress repeated logs
|
|
268
|
+
heapUsageMiddleware, // attach memory stats
|
|
269
|
+
ipMiddleware, // extract client IP
|
|
270
|
+
userAgentMiddleware, // extract User-Agent
|
|
271
|
+
levelOverrideMiddleware, // dynamic level via headers
|
|
272
|
+
ocppMiddleware, // OCPP protocol enrichment
|
|
273
|
+
otelTraceMiddleware, // OpenTelemetry trace context
|
|
274
|
+
createHttpLogger, // HTTP request/response logging
|
|
275
|
+
} from "voltlog-io";
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## ⥠Performance
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
// Max performance mode â zero overhead for filtered logs
|
|
284
|
+
const logger = createLogger({
|
|
285
|
+
level: "WARN",
|
|
286
|
+
idGenerator: false, // no ID generation
|
|
287
|
+
transports: [consoleTransport()],
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// These cost ~0.02Ξs each (100x faster than Pino for filtered logs)
|
|
291
|
+
logger.trace("free");
|
|
292
|
+
logger.debug("free");
|
|
293
|
+
logger.info("free");
|
|
294
|
+
|
|
295
|
+
// Only these execute
|
|
296
|
+
logger.warn("logged");
|
|
297
|
+
logger.error("logged");
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## ðĄïļ Graceful Shutdown
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
process.on("SIGTERM", async () => {
|
|
304
|
+
await logger.flush(); // ensure all buffered logs are sent
|
|
305
|
+
await logger.close(); // release resources (file handles, connections)
|
|
306
|
+
process.exit(0);
|
|
307
|
+
});
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## ðĪ Contributing
|
|
311
|
+
|
|
312
|
+
Contributions are welcome! Please see our [Contributing Guide](CONTRIBUTING.md).
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
[MIT](LICENSE) ÂĐ [Rohit Tiwari](https://github.com/rohittiwari-dev)
|