voltlog-io 1.0.0
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.
Potentially problematic release.
This version of voltlog-io might be problematic. Click here for more details.
- package/CODE_OF_CONDUCT.md +41 -0
- package/CONTRIBUTING.md +112 -0
- package/LICENSE +21 -0
- package/README.md +372 -0
- package/dist/index.d.mts +707 -0
- package/dist/index.d.ts +707 -0
- package/dist/index.js +782 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +738 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,782 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
LogLevel: () => LogLevel,
|
|
24
|
+
LogLevelNameMap: () => LogLevelNameMap,
|
|
25
|
+
LogLevelValueMap: () => LogLevelValueMap,
|
|
26
|
+
alertMiddleware: () => alertMiddleware,
|
|
27
|
+
batchTransport: () => batchTransport,
|
|
28
|
+
consoleTransport: () => consoleTransport,
|
|
29
|
+
createLogger: () => createLogger,
|
|
30
|
+
createMiddleware: () => createMiddleware,
|
|
31
|
+
jsonStreamTransport: () => jsonStreamTransport,
|
|
32
|
+
ocppMiddleware: () => ocppMiddleware,
|
|
33
|
+
prettyTransport: () => prettyTransport,
|
|
34
|
+
redactionMiddleware: () => redactionMiddleware,
|
|
35
|
+
redisTransport: () => redisTransport,
|
|
36
|
+
resolveLevel: () => resolveLevel,
|
|
37
|
+
samplingMiddleware: () => samplingMiddleware,
|
|
38
|
+
shouldIncludeStack: () => shouldIncludeStack,
|
|
39
|
+
shouldLog: () => shouldLog,
|
|
40
|
+
webhookTransport: () => webhookTransport
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(src_exports);
|
|
43
|
+
|
|
44
|
+
// src/core/logger.ts
|
|
45
|
+
var import_cuid2 = require("@paralleldrive/cuid2");
|
|
46
|
+
|
|
47
|
+
// src/core/types.ts
|
|
48
|
+
var LogLevel = {
|
|
49
|
+
TRACE: 10,
|
|
50
|
+
DEBUG: 20,
|
|
51
|
+
INFO: 30,
|
|
52
|
+
WARN: 40,
|
|
53
|
+
ERROR: 50,
|
|
54
|
+
FATAL: 60,
|
|
55
|
+
SILENT: Infinity
|
|
56
|
+
};
|
|
57
|
+
var LogLevelNameMap = Object.fromEntries(
|
|
58
|
+
Object.entries(LogLevel).map(([k, v]) => [k.toLowerCase(), v])
|
|
59
|
+
);
|
|
60
|
+
var LogLevelValueMap = Object.fromEntries(
|
|
61
|
+
Object.entries(LogLevel).filter(([, v]) => Number.isFinite(v)).map(([k, v]) => [v, k])
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
// src/core/levels.ts
|
|
65
|
+
function resolveLevel(level) {
|
|
66
|
+
const n = LogLevelNameMap[level.toLowerCase()];
|
|
67
|
+
return n !== void 0 ? n : LogLevel.INFO;
|
|
68
|
+
}
|
|
69
|
+
function shouldLog(entryLevel, filterLevel) {
|
|
70
|
+
return entryLevel >= filterLevel;
|
|
71
|
+
}
|
|
72
|
+
function shouldIncludeStack(entryLevel, includeStack) {
|
|
73
|
+
if (typeof includeStack === "boolean") return includeStack;
|
|
74
|
+
return entryLevel >= resolveLevel(includeStack);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/core/pipeline.ts
|
|
78
|
+
function composeMiddleware(middleware, final) {
|
|
79
|
+
if (middleware.length === 0) return final;
|
|
80
|
+
return (entry) => {
|
|
81
|
+
let index = 0;
|
|
82
|
+
const next = (e) => {
|
|
83
|
+
if (index < middleware.length) {
|
|
84
|
+
const mw = middleware[index++];
|
|
85
|
+
mw(e, next);
|
|
86
|
+
} else {
|
|
87
|
+
final(e);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
next(entry);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function fanOutToTransformers(entry, transformers, loggerLevel) {
|
|
94
|
+
for (const t of transformers) {
|
|
95
|
+
const tLevel = t.level ? resolveLevel(t.level) : loggerLevel;
|
|
96
|
+
if (!shouldLog(entry.level, tLevel)) continue;
|
|
97
|
+
try {
|
|
98
|
+
const result = t.transform(entry);
|
|
99
|
+
if (result && typeof result.catch === "function") {
|
|
100
|
+
result.catch(() => {
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/core/logger.ts
|
|
109
|
+
var LoggerImpl = class {
|
|
110
|
+
_level;
|
|
111
|
+
_transports;
|
|
112
|
+
_middlewareList;
|
|
113
|
+
_pipeline;
|
|
114
|
+
_context;
|
|
115
|
+
_includeStack;
|
|
116
|
+
_timestampFn;
|
|
117
|
+
constructor(options = {}) {
|
|
118
|
+
this._level = resolveLevel(options.level ?? "INFO");
|
|
119
|
+
this._transports = [...options.transports ?? []];
|
|
120
|
+
this._middlewareList = [...options.middleware ?? []];
|
|
121
|
+
this._context = options.context ? { ...options.context } : {};
|
|
122
|
+
this._includeStack = options.includeStack ?? "ERROR";
|
|
123
|
+
this._timestampFn = options.timestamp ?? Date.now;
|
|
124
|
+
this._pipeline = this._buildPipeline();
|
|
125
|
+
}
|
|
126
|
+
// ─── Log Methods ────────────────────────────────────────────
|
|
127
|
+
trace(message, meta) {
|
|
128
|
+
this._log(10, "TRACE", message, meta);
|
|
129
|
+
}
|
|
130
|
+
debug(message, meta) {
|
|
131
|
+
this._log(20, "DEBUG", message, meta);
|
|
132
|
+
}
|
|
133
|
+
info(message, meta) {
|
|
134
|
+
this._log(30, "INFO", message, meta);
|
|
135
|
+
}
|
|
136
|
+
warn(message, meta) {
|
|
137
|
+
this._log(40, "WARN", message, meta);
|
|
138
|
+
}
|
|
139
|
+
error(message, metaOrError, error) {
|
|
140
|
+
if (metaOrError instanceof Error) {
|
|
141
|
+
this._log(50, "ERROR", message, void 0, metaOrError);
|
|
142
|
+
} else {
|
|
143
|
+
this._log(50, "ERROR", message, metaOrError, error);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
fatal(message, metaOrError, error) {
|
|
147
|
+
if (metaOrError instanceof Error) {
|
|
148
|
+
this._log(60, "FATAL", message, void 0, metaOrError);
|
|
149
|
+
} else {
|
|
150
|
+
this._log(60, "FATAL", message, metaOrError, error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// ─── Child Logger ───────────────────────────────────────────
|
|
154
|
+
child(context) {
|
|
155
|
+
return new ChildLoggerImpl(this, { ...this._context, ...context });
|
|
156
|
+
}
|
|
157
|
+
// ─── Dynamic Configuration ─────────────────────────────────
|
|
158
|
+
addTransformer(transformer) {
|
|
159
|
+
this._transports.push(transformer);
|
|
160
|
+
}
|
|
161
|
+
removeTransformer(name) {
|
|
162
|
+
this._transports = this._transports.filter((t) => t.name !== name);
|
|
163
|
+
}
|
|
164
|
+
addMiddleware(middleware) {
|
|
165
|
+
this._middlewareList.push(middleware);
|
|
166
|
+
this._pipeline = this._buildPipeline();
|
|
167
|
+
}
|
|
168
|
+
// ─── Lifecycle ──────────────────────────────────────────────
|
|
169
|
+
async flush() {
|
|
170
|
+
await Promise.all(this._transports.map((t) => t.flush?.()).filter(Boolean));
|
|
171
|
+
}
|
|
172
|
+
async close() {
|
|
173
|
+
await this.flush();
|
|
174
|
+
await Promise.all(this._transports.map((t) => t.close?.()).filter(Boolean));
|
|
175
|
+
}
|
|
176
|
+
// ─── Internal ───────────────────────────────────────────────
|
|
177
|
+
/** @internal */
|
|
178
|
+
_log(level, levelName, message, meta, error) {
|
|
179
|
+
this._logWithContext(level, levelName, message, this._context, meta, error);
|
|
180
|
+
}
|
|
181
|
+
/** @internal — used by child loggers to inject bound context */
|
|
182
|
+
_logWithContext(level, levelName, message, context, meta, error) {
|
|
183
|
+
if (!shouldLog(level, this._level)) return;
|
|
184
|
+
const entry = {
|
|
185
|
+
id: (0, import_cuid2.createId)(),
|
|
186
|
+
level,
|
|
187
|
+
levelName,
|
|
188
|
+
message,
|
|
189
|
+
timestamp: this._timestampFn(),
|
|
190
|
+
meta: meta ?? {},
|
|
191
|
+
context: Object.keys(context).length > 0 ? context : void 0
|
|
192
|
+
};
|
|
193
|
+
if (error) {
|
|
194
|
+
const logError = {
|
|
195
|
+
message: error.message,
|
|
196
|
+
name: error.name,
|
|
197
|
+
code: error.code
|
|
198
|
+
};
|
|
199
|
+
if (shouldIncludeStack(level, this._includeStack)) {
|
|
200
|
+
logError.stack = error.stack;
|
|
201
|
+
}
|
|
202
|
+
entry.error = logError;
|
|
203
|
+
}
|
|
204
|
+
this._pipeline(entry);
|
|
205
|
+
}
|
|
206
|
+
_buildPipeline() {
|
|
207
|
+
return composeMiddleware(this._middlewareList, (entry) => {
|
|
208
|
+
fanOutToTransformers(entry, this._transports, this._level);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
var ChildLoggerImpl = class _ChildLoggerImpl {
|
|
213
|
+
constructor(_parent, _context) {
|
|
214
|
+
this._parent = _parent;
|
|
215
|
+
this._context = _context;
|
|
216
|
+
}
|
|
217
|
+
trace(message, meta) {
|
|
218
|
+
this._parent._logWithContext(10, "TRACE", message, this._context, meta);
|
|
219
|
+
}
|
|
220
|
+
debug(message, meta) {
|
|
221
|
+
this._parent._logWithContext(20, "DEBUG", message, this._context, meta);
|
|
222
|
+
}
|
|
223
|
+
info(message, meta) {
|
|
224
|
+
this._parent._logWithContext(30, "INFO", message, this._context, meta);
|
|
225
|
+
}
|
|
226
|
+
warn(message, meta) {
|
|
227
|
+
this._parent._logWithContext(40, "WARN", message, this._context, meta);
|
|
228
|
+
}
|
|
229
|
+
error(message, metaOrError, error) {
|
|
230
|
+
if (metaOrError instanceof Error) {
|
|
231
|
+
this._parent._logWithContext(
|
|
232
|
+
50,
|
|
233
|
+
"ERROR",
|
|
234
|
+
message,
|
|
235
|
+
this._context,
|
|
236
|
+
void 0,
|
|
237
|
+
metaOrError
|
|
238
|
+
);
|
|
239
|
+
} else {
|
|
240
|
+
this._parent._logWithContext(
|
|
241
|
+
50,
|
|
242
|
+
"ERROR",
|
|
243
|
+
message,
|
|
244
|
+
this._context,
|
|
245
|
+
metaOrError,
|
|
246
|
+
error
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
fatal(message, metaOrError, error) {
|
|
251
|
+
if (metaOrError instanceof Error) {
|
|
252
|
+
this._parent._logWithContext(
|
|
253
|
+
60,
|
|
254
|
+
"FATAL",
|
|
255
|
+
message,
|
|
256
|
+
this._context,
|
|
257
|
+
void 0,
|
|
258
|
+
metaOrError
|
|
259
|
+
);
|
|
260
|
+
} else {
|
|
261
|
+
this._parent._logWithContext(
|
|
262
|
+
60,
|
|
263
|
+
"FATAL",
|
|
264
|
+
message,
|
|
265
|
+
this._context,
|
|
266
|
+
metaOrError,
|
|
267
|
+
error
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
child(context) {
|
|
272
|
+
return new _ChildLoggerImpl(this._parent, {
|
|
273
|
+
...this._context,
|
|
274
|
+
...context
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
addTransformer(transformer) {
|
|
278
|
+
this._parent.addTransformer(transformer);
|
|
279
|
+
}
|
|
280
|
+
removeTransformer(name) {
|
|
281
|
+
this._parent.removeTransformer(name);
|
|
282
|
+
}
|
|
283
|
+
addMiddleware(middleware) {
|
|
284
|
+
this._parent.addMiddleware(middleware);
|
|
285
|
+
}
|
|
286
|
+
flush() {
|
|
287
|
+
return this._parent.flush();
|
|
288
|
+
}
|
|
289
|
+
close() {
|
|
290
|
+
return this._parent.close();
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
function createLogger(options) {
|
|
294
|
+
return new LoggerImpl(options);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/middleware/redaction.ts
|
|
298
|
+
var DEFAULT_REDACT_VALUE = "[REDACTED]";
|
|
299
|
+
function redactionMiddleware(options) {
|
|
300
|
+
const paths = new Set(options.paths.map((p) => p.toLowerCase()));
|
|
301
|
+
const replacement = options.replacement ?? DEFAULT_REDACT_VALUE;
|
|
302
|
+
const deep = options.deep ?? true;
|
|
303
|
+
function redactObject(obj) {
|
|
304
|
+
const result = {};
|
|
305
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
306
|
+
if (paths.has(key.toLowerCase())) {
|
|
307
|
+
result[key] = replacement;
|
|
308
|
+
} else if (deep && value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
309
|
+
result[key] = redactObject(value);
|
|
310
|
+
} else {
|
|
311
|
+
result[key] = value;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return result;
|
|
315
|
+
}
|
|
316
|
+
return (entry, next) => {
|
|
317
|
+
const redacted = { ...entry };
|
|
318
|
+
if (entry.meta && typeof entry.meta === "object") {
|
|
319
|
+
redacted.meta = redactObject(
|
|
320
|
+
entry.meta
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
if (entry.context && typeof entry.context === "object") {
|
|
324
|
+
redacted.context = redactObject(entry.context);
|
|
325
|
+
}
|
|
326
|
+
next(redacted);
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// src/middleware/sampling.ts
|
|
331
|
+
function samplingMiddleware(options = {}) {
|
|
332
|
+
const keyFn = options.keyFn ?? ((entry) => entry.message);
|
|
333
|
+
const maxPerWindow = options.maxPerWindow ?? 100;
|
|
334
|
+
const windowMs = options.windowMs ?? 6e4;
|
|
335
|
+
const sampleRate = options.sampleRate ?? 1;
|
|
336
|
+
const priorityLevel = options.priorityLevel ?? 40;
|
|
337
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
338
|
+
return (entry, next) => {
|
|
339
|
+
if (entry.level >= priorityLevel) {
|
|
340
|
+
return next(entry);
|
|
341
|
+
}
|
|
342
|
+
if (sampleRate < 1 && Math.random() > sampleRate) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const key = keyFn(entry);
|
|
346
|
+
const now = entry.timestamp;
|
|
347
|
+
let bucket = buckets.get(key);
|
|
348
|
+
if (!bucket || now - bucket.windowStart >= windowMs) {
|
|
349
|
+
bucket = { count: 0, windowStart: now };
|
|
350
|
+
buckets.set(key, bucket);
|
|
351
|
+
}
|
|
352
|
+
if (bucket.count < maxPerWindow) {
|
|
353
|
+
bucket.count++;
|
|
354
|
+
next(entry);
|
|
355
|
+
}
|
|
356
|
+
if (buckets.size > 2e3) {
|
|
357
|
+
const expireBefore = now - windowMs * 2;
|
|
358
|
+
for (const [k, b] of buckets) {
|
|
359
|
+
if (b.windowStart < expireBefore) {
|
|
360
|
+
buckets.delete(k);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/middleware/ocpp.ts
|
|
368
|
+
function ocppMiddleware(options = {}) {
|
|
369
|
+
const autoPayloadSize = options.autoPayloadSize ?? true;
|
|
370
|
+
const propagateCorrelationId = options.propagateCorrelationId ?? true;
|
|
371
|
+
return (entry, next) => {
|
|
372
|
+
const enriched = { ...entry, meta: { ...entry.meta } };
|
|
373
|
+
if (autoPayloadSize && enriched.meta.payloadSize === void 0 && enriched.meta.action) {
|
|
374
|
+
try {
|
|
375
|
+
enriched.meta.payloadSize = JSON.stringify(enriched.meta).length;
|
|
376
|
+
} catch {
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (propagateCorrelationId && enriched.meta.correlationId && !enriched.correlationId) {
|
|
380
|
+
enriched.correlationId = enriched.meta.correlationId;
|
|
381
|
+
}
|
|
382
|
+
next(enriched);
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/middleware/alert.ts
|
|
387
|
+
function alertMiddleware(rules) {
|
|
388
|
+
const states = /* @__PURE__ */ new Map();
|
|
389
|
+
for (const rule of rules) {
|
|
390
|
+
states.set(rule.name, { entries: [], lastFired: -Infinity });
|
|
391
|
+
}
|
|
392
|
+
return (entry, next) => {
|
|
393
|
+
const now = entry.timestamp;
|
|
394
|
+
for (const rule of rules) {
|
|
395
|
+
if (!rule.when(entry)) continue;
|
|
396
|
+
const state = states.get(rule.name);
|
|
397
|
+
const windowMs = rule.windowMs ?? Infinity;
|
|
398
|
+
const threshold = rule.threshold ?? 1;
|
|
399
|
+
const cooldownMs = rule.cooldownMs ?? 0;
|
|
400
|
+
if (Number.isFinite(windowMs)) {
|
|
401
|
+
state.entries = state.entries.filter(
|
|
402
|
+
(e) => now - e.timestamp < windowMs
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
state.entries.push(entry);
|
|
406
|
+
if (state.entries.length >= threshold && now - state.lastFired >= cooldownMs) {
|
|
407
|
+
const alertEntries = [...state.entries];
|
|
408
|
+
state.entries = [];
|
|
409
|
+
state.lastFired = now;
|
|
410
|
+
try {
|
|
411
|
+
const result = rule.onAlert(alertEntries);
|
|
412
|
+
if (result && typeof result.catch === "function") {
|
|
413
|
+
result.catch(() => {
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
} catch {
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
next(entry);
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/middleware/create-middleware.ts
|
|
425
|
+
function createMiddleware(fn) {
|
|
426
|
+
return fn;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/transformers/console.ts
|
|
430
|
+
function consoleTransport(options = {}) {
|
|
431
|
+
const useConsoleLevels = options.useConsoleLevels ?? true;
|
|
432
|
+
const formatter = options.formatter ?? ((entry) => JSON.stringify(entry));
|
|
433
|
+
return {
|
|
434
|
+
name: "console",
|
|
435
|
+
level: options.level,
|
|
436
|
+
transform(entry) {
|
|
437
|
+
const output = formatter(entry);
|
|
438
|
+
if (!useConsoleLevels) {
|
|
439
|
+
console.log(output);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (entry.level >= LogLevel.FATAL) {
|
|
443
|
+
console.error(output);
|
|
444
|
+
} else if (entry.level >= LogLevel.ERROR) {
|
|
445
|
+
console.error(output);
|
|
446
|
+
} else if (entry.level >= LogLevel.WARN) {
|
|
447
|
+
console.warn(output);
|
|
448
|
+
} else if (entry.level >= LogLevel.INFO) {
|
|
449
|
+
console.info(output);
|
|
450
|
+
} else if (entry.level >= LogLevel.DEBUG) {
|
|
451
|
+
console.debug(output);
|
|
452
|
+
} else {
|
|
453
|
+
console.log(output);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/transformers/pretty.ts
|
|
460
|
+
var RESET = "\x1B[0m";
|
|
461
|
+
var DIM = "\x1B[2m";
|
|
462
|
+
var BOLD = "\x1B[1m";
|
|
463
|
+
var COLORS = {
|
|
464
|
+
TRACE: "\x1B[90m",
|
|
465
|
+
// gray
|
|
466
|
+
DEBUG: "\x1B[36m",
|
|
467
|
+
// cyan
|
|
468
|
+
INFO: "\x1B[32m",
|
|
469
|
+
// green
|
|
470
|
+
WARN: "\x1B[33m",
|
|
471
|
+
// yellow
|
|
472
|
+
ERROR: "\x1B[31m",
|
|
473
|
+
// red
|
|
474
|
+
FATAL: "\x1B[35;1m"
|
|
475
|
+
// bold magenta
|
|
476
|
+
};
|
|
477
|
+
var ICONS = {
|
|
478
|
+
TRACE: "\u{1F50D}",
|
|
479
|
+
DEBUG: "\u{1F41B}",
|
|
480
|
+
INFO: "\u2139",
|
|
481
|
+
WARN: "\u26A0",
|
|
482
|
+
ERROR: "\u2716",
|
|
483
|
+
FATAL: "\u{1F480}"
|
|
484
|
+
};
|
|
485
|
+
var EXCHANGE_ICONS = {
|
|
486
|
+
CALL: "\u26A1",
|
|
487
|
+
CALLRESULT: "\u2714",
|
|
488
|
+
CALLERROR: "\u{1F6A8}"
|
|
489
|
+
};
|
|
490
|
+
var DIRECTION_ARROWS = {
|
|
491
|
+
IN: "\u2192",
|
|
492
|
+
OUT: "\u2190"
|
|
493
|
+
};
|
|
494
|
+
function prettyTransport(options = {}) {
|
|
495
|
+
const showTimestamps = options.timestamps ?? true;
|
|
496
|
+
const useColors = options.colors ?? true;
|
|
497
|
+
function colorize(text, color) {
|
|
498
|
+
return useColors ? `${color}${text}${RESET}` : text;
|
|
499
|
+
}
|
|
500
|
+
function formatExchange(entry) {
|
|
501
|
+
const meta = entry.meta;
|
|
502
|
+
if (!meta || !meta.action || !meta.messageType) return null;
|
|
503
|
+
const icon = EXCHANGE_ICONS[meta.messageType] ?? "\u2022";
|
|
504
|
+
const arrow = DIRECTION_ARROWS[meta.direction ?? "IN"] ?? "\u2192";
|
|
505
|
+
const cpId = meta.chargePointId ?? "unknown";
|
|
506
|
+
const action = meta.action;
|
|
507
|
+
const msgType = meta.messageType;
|
|
508
|
+
const dir = meta.direction ?? "";
|
|
509
|
+
let line = `${icon} ${colorize(cpId, BOLD)} ${arrow} ${colorize(action, BOLD)} [${dir}] ${colorize(msgType, DIM)}`;
|
|
510
|
+
if (meta.status || meta.latencyMs !== void 0) {
|
|
511
|
+
const statusIcon = meta.messageType === "CALLERROR" ? "\u274C" : "\u2714";
|
|
512
|
+
const status = meta.status ?? "";
|
|
513
|
+
const latency = meta.latencyMs !== void 0 ? `(${meta.latencyMs}ms)` : "";
|
|
514
|
+
line += `
|
|
515
|
+
${statusIcon} ${status} ${colorize(latency, DIM)}`;
|
|
516
|
+
}
|
|
517
|
+
return line;
|
|
518
|
+
}
|
|
519
|
+
function formatStandard(entry) {
|
|
520
|
+
const icon = ICONS[entry.levelName] ?? "\u2022";
|
|
521
|
+
const levelColor = COLORS[entry.levelName] ?? "";
|
|
522
|
+
const level = colorize(entry.levelName.padEnd(5), levelColor);
|
|
523
|
+
const ts = showTimestamps ? colorize(new Date(entry.timestamp).toISOString(), DIM) + " " : "";
|
|
524
|
+
let line = `${icon} ${ts}${level} ${entry.message}`;
|
|
525
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
526
|
+
line += ` ${colorize(JSON.stringify(entry.context), DIM)}`;
|
|
527
|
+
}
|
|
528
|
+
if (entry.meta && Object.keys(entry.meta).length > 0) {
|
|
529
|
+
line += ` ${colorize(JSON.stringify(entry.meta), DIM)}`;
|
|
530
|
+
}
|
|
531
|
+
if (entry.error) {
|
|
532
|
+
line += `
|
|
533
|
+
${colorize(`${entry.error.name ?? "Error"}: ${entry.error.message}`, COLORS["ERROR"] ?? "")}`;
|
|
534
|
+
if (entry.error.stack) {
|
|
535
|
+
line += `
|
|
536
|
+
${colorize(entry.error.stack, DIM)}`;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return line;
|
|
540
|
+
}
|
|
541
|
+
return {
|
|
542
|
+
name: "pretty",
|
|
543
|
+
level: options.level,
|
|
544
|
+
transform(entry) {
|
|
545
|
+
const exchangeOutput = formatExchange(entry);
|
|
546
|
+
if (exchangeOutput) {
|
|
547
|
+
console.log(exchangeOutput);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
const output = formatStandard(entry);
|
|
551
|
+
if (entry.level >= LogLevel.ERROR) {
|
|
552
|
+
console.error(output);
|
|
553
|
+
} else if (entry.level >= LogLevel.WARN) {
|
|
554
|
+
console.warn(output);
|
|
555
|
+
} else {
|
|
556
|
+
console.log(output);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// src/transformers/json-stream.ts
|
|
563
|
+
function jsonStreamTransport(options) {
|
|
564
|
+
const stream = options.stream;
|
|
565
|
+
const serialize = options.serializer ?? ((entry) => JSON.stringify(entry) + "\n");
|
|
566
|
+
return {
|
|
567
|
+
name: "json-stream",
|
|
568
|
+
level: options.level,
|
|
569
|
+
transform(entry) {
|
|
570
|
+
const data = serialize(entry);
|
|
571
|
+
stream.write(data);
|
|
572
|
+
},
|
|
573
|
+
close() {
|
|
574
|
+
return new Promise((resolve) => {
|
|
575
|
+
if ("end" in stream && typeof stream.end === "function") {
|
|
576
|
+
stream.end(() => resolve());
|
|
577
|
+
} else {
|
|
578
|
+
resolve();
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// src/transformers/webhook.ts
|
|
586
|
+
function webhookTransport(options) {
|
|
587
|
+
const {
|
|
588
|
+
url,
|
|
589
|
+
method = "POST",
|
|
590
|
+
headers = {},
|
|
591
|
+
batchSize = 1,
|
|
592
|
+
flushIntervalMs = 5e3,
|
|
593
|
+
retry = false,
|
|
594
|
+
maxRetries = 3
|
|
595
|
+
} = options;
|
|
596
|
+
const serialize = options.serializer ?? ((entries) => JSON.stringify({
|
|
597
|
+
entries,
|
|
598
|
+
count: entries.length,
|
|
599
|
+
timestamp: Date.now()
|
|
600
|
+
}));
|
|
601
|
+
let buffer = [];
|
|
602
|
+
let flushTimer = null;
|
|
603
|
+
async function sendBatch(entries, attempt = 0) {
|
|
604
|
+
try {
|
|
605
|
+
const response = await fetch(url, {
|
|
606
|
+
method,
|
|
607
|
+
headers: {
|
|
608
|
+
"Content-Type": "application/json",
|
|
609
|
+
...headers
|
|
610
|
+
},
|
|
611
|
+
body: serialize(entries)
|
|
612
|
+
});
|
|
613
|
+
if (!response.ok && retry && attempt < maxRetries) {
|
|
614
|
+
const delay = Math.min(1e3 * Math.pow(2, attempt), 3e4);
|
|
615
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
616
|
+
return sendBatch(entries, attempt + 1);
|
|
617
|
+
}
|
|
618
|
+
} catch {
|
|
619
|
+
if (retry && attempt < maxRetries) {
|
|
620
|
+
const delay = Math.min(1e3 * Math.pow(2, attempt), 3e4);
|
|
621
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
622
|
+
return sendBatch(entries, attempt + 1);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function scheduleFlush() {
|
|
627
|
+
if (flushTimer) return;
|
|
628
|
+
flushTimer = setTimeout(() => {
|
|
629
|
+
flushTimer = null;
|
|
630
|
+
doFlush();
|
|
631
|
+
}, flushIntervalMs);
|
|
632
|
+
}
|
|
633
|
+
function doFlush() {
|
|
634
|
+
if (buffer.length === 0) return;
|
|
635
|
+
const batch = buffer;
|
|
636
|
+
buffer = [];
|
|
637
|
+
sendBatch(batch).catch(() => {
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
return {
|
|
641
|
+
name: "webhook",
|
|
642
|
+
level: options.level,
|
|
643
|
+
transform(entry) {
|
|
644
|
+
buffer.push(entry);
|
|
645
|
+
if (buffer.length >= batchSize) {
|
|
646
|
+
doFlush();
|
|
647
|
+
} else {
|
|
648
|
+
scheduleFlush();
|
|
649
|
+
}
|
|
650
|
+
},
|
|
651
|
+
async flush() {
|
|
652
|
+
if (flushTimer) {
|
|
653
|
+
clearTimeout(flushTimer);
|
|
654
|
+
flushTimer = null;
|
|
655
|
+
}
|
|
656
|
+
if (buffer.length > 0) {
|
|
657
|
+
const batch = buffer;
|
|
658
|
+
buffer = [];
|
|
659
|
+
await sendBatch(batch);
|
|
660
|
+
}
|
|
661
|
+
},
|
|
662
|
+
async close() {
|
|
663
|
+
await this.flush();
|
|
664
|
+
}
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// src/transformers/batch.ts
|
|
669
|
+
function batchTransport(inner, options = {}) {
|
|
670
|
+
const batchSize = options.batchSize ?? 100;
|
|
671
|
+
const flushIntervalMs = options.flushIntervalMs ?? 5e3;
|
|
672
|
+
let buffer = [];
|
|
673
|
+
let flushTimer = null;
|
|
674
|
+
function scheduleFlush() {
|
|
675
|
+
if (flushTimer) return;
|
|
676
|
+
flushTimer = setTimeout(() => {
|
|
677
|
+
flushTimer = null;
|
|
678
|
+
doFlush();
|
|
679
|
+
}, flushIntervalMs);
|
|
680
|
+
}
|
|
681
|
+
function doFlush() {
|
|
682
|
+
if (buffer.length === 0) return;
|
|
683
|
+
const batch = buffer;
|
|
684
|
+
buffer = [];
|
|
685
|
+
for (const entry of batch) {
|
|
686
|
+
try {
|
|
687
|
+
const result = inner.transform(entry);
|
|
688
|
+
if (result && typeof result.catch === "function") {
|
|
689
|
+
result.catch(() => {
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
} catch {
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return {
|
|
697
|
+
name: `batch(${inner.name})`,
|
|
698
|
+
level: inner.level,
|
|
699
|
+
transform(entry) {
|
|
700
|
+
buffer.push(entry);
|
|
701
|
+
if (buffer.length >= batchSize) {
|
|
702
|
+
doFlush();
|
|
703
|
+
} else {
|
|
704
|
+
scheduleFlush();
|
|
705
|
+
}
|
|
706
|
+
},
|
|
707
|
+
async flush() {
|
|
708
|
+
if (flushTimer) {
|
|
709
|
+
clearTimeout(flushTimer);
|
|
710
|
+
flushTimer = null;
|
|
711
|
+
}
|
|
712
|
+
doFlush();
|
|
713
|
+
await inner.flush?.();
|
|
714
|
+
},
|
|
715
|
+
async close() {
|
|
716
|
+
await this.flush();
|
|
717
|
+
await inner.close?.();
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// src/transformers/redis.ts
|
|
723
|
+
function redisTransport(options) {
|
|
724
|
+
const { client, streamKey = "logs", maxLen, level } = options;
|
|
725
|
+
const fieldMapper = options.fieldMapper ?? defaultFieldMapper;
|
|
726
|
+
function defaultFieldMapper(entry) {
|
|
727
|
+
return {
|
|
728
|
+
id: entry.id,
|
|
729
|
+
level: String(entry.level),
|
|
730
|
+
levelName: entry.levelName,
|
|
731
|
+
message: entry.message,
|
|
732
|
+
timestamp: String(entry.timestamp),
|
|
733
|
+
data: JSON.stringify({
|
|
734
|
+
meta: entry.meta,
|
|
735
|
+
context: entry.context,
|
|
736
|
+
correlationId: entry.correlationId,
|
|
737
|
+
error: entry.error
|
|
738
|
+
})
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
return {
|
|
742
|
+
name: "redis",
|
|
743
|
+
level,
|
|
744
|
+
transform(entry) {
|
|
745
|
+
const fields = fieldMapper(entry);
|
|
746
|
+
const args = [streamKey];
|
|
747
|
+
if (maxLen) {
|
|
748
|
+
args.push("MAXLEN", "~", maxLen);
|
|
749
|
+
}
|
|
750
|
+
args.push("*");
|
|
751
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
752
|
+
args.push(key, value);
|
|
753
|
+
}
|
|
754
|
+
client.xadd(...args).catch(() => {
|
|
755
|
+
});
|
|
756
|
+
},
|
|
757
|
+
async close() {
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
762
|
+
0 && (module.exports = {
|
|
763
|
+
LogLevel,
|
|
764
|
+
LogLevelNameMap,
|
|
765
|
+
LogLevelValueMap,
|
|
766
|
+
alertMiddleware,
|
|
767
|
+
batchTransport,
|
|
768
|
+
consoleTransport,
|
|
769
|
+
createLogger,
|
|
770
|
+
createMiddleware,
|
|
771
|
+
jsonStreamTransport,
|
|
772
|
+
ocppMiddleware,
|
|
773
|
+
prettyTransport,
|
|
774
|
+
redactionMiddleware,
|
|
775
|
+
redisTransport,
|
|
776
|
+
resolveLevel,
|
|
777
|
+
samplingMiddleware,
|
|
778
|
+
shouldIncludeStack,
|
|
779
|
+
shouldLog,
|
|
780
|
+
webhookTransport
|
|
781
|
+
});
|
|
782
|
+
//# sourceMappingURL=index.js.map
|