logs-interceptor
Version:
High-performance, production-ready log interceptor for Node.js applications with Loki integration. Built with Clean Architecture principles. Supports Node.js, Browser, and Node-RED.
43 lines • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LokiFormatter = void 0;
class LokiFormatter {
format(entries) {
var _a;
const streamMap = new Map();
for (const entry of entries) {
const streamKey = JSON.stringify((_a = entry.labels) !== null && _a !== void 0 ? _a : {});
const timestamp = String(Date.parse(entry.timestamp) * 1000000); // nanoseconds
const logData = {
id: entry.id,
level: entry.level,
message: entry.message,
context: entry.context,
};
if (entry.traceId && entry.traceId !== 'undefined') {
logData.traceId = entry.traceId;
}
if (entry.spanId && entry.spanId !== 'undefined') {
logData.spanId = entry.spanId;
}
if (entry.requestId && entry.requestId !== 'undefined') {
logData.requestId = entry.requestId;
}
if (entry.metadata) {
logData.metadata = entry.metadata;
}
const logLine = JSON.stringify(logData);
if (!streamMap.has(streamKey)) {
streamMap.set(streamKey, []);
}
streamMap.get(streamKey).push([timestamp, logLine]);
}
const streams = Array.from(streamMap.entries()).map(([streamKey, values]) => ({
stream: JSON.parse(streamKey),
values: values.sort((a, b) => a[0].localeCompare(b[0])),
}));
return { streams };
}
}
exports.LokiFormatter = LokiFormatter;
//# sourceMappingURL=LokiFormatter.js.map