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.
128 lines • 4.79 kB
JavaScript
;
/**
* Node-RED Node Implementation
* Custom node for logs-interceptor
*/
module.exports = function (RED) {
'use strict';
function LogsInterceptorNode(config) {
RED.nodes.createNode(this, config);
const node = this;
let logger = null;
let initialized = false;
// Initialize logger
try {
// Try to load from dist (production) or src (development)
let logsInterceptor;
try {
logsInterceptor = require('../../../../dist/index');
}
catch {
logsInterceptor = require('../../index');
}
const { init } = logsInterceptor;
const loggerConfig = {
transport: {
url: config.url || process.env.LOGS_INTERCEPTOR_URL,
tenantId: config.tenantId || process.env.LOGS_INTERCEPTOR_TENANT_ID,
authToken: config.authToken || process.env.LOGS_INTERCEPTOR_AUTH_TOKEN,
},
appName: config.appName || 'node-red',
environment: config.environment || 'production',
version: config.version || '1.0.0',
interceptConsole: config.interceptConsole ?? false,
debug: config.debug ?? false,
};
logger = init(loggerConfig);
initialized = true;
node.status({ fill: 'green', shape: 'dot', text: 'connected' });
node.log('Logs interceptor initialized');
}
catch (error) {
node.error(`Failed to initialize logs interceptor: ${error.message}`);
node.status({ fill: 'red', shape: 'ring', text: 'error' });
initialized = false;
}
// Handle incoming messages
node.on('input', function (msg) {
if (!initialized || !logger) {
node.warn('Logger not initialized');
return;
}
try {
const payload = msg.payload;
const level = msg.level || 'info';
const message = typeof payload === 'string'
? payload
: JSON.stringify(payload);
const context = {
nodeId: node.id,
nodeName: node.name,
topic: msg.topic,
...(typeof payload === 'object' && payload !== null
? payload
: {}),
};
// Remove payload from context to avoid duplication
delete context.payload;
switch (level.toLowerCase()) {
case 'debug':
logger.debug(message, context);
break;
case 'info':
logger.info(message, context);
break;
case 'warn':
logger.warn(message, context);
break;
case 'error':
logger.error(message, context);
break;
case 'fatal':
logger.fatal(message, context);
break;
default:
logger.info(message, context);
}
// Pass message through
node.send(msg);
}
catch (error) {
node.error(`Error processing log: ${error.message}`);
}
});
// Handle node close
node.on('close', async function () {
if (logger && typeof logger.destroy === 'function') {
try {
await logger.destroy();
node.log('Logs interceptor destroyed');
}
catch (error) {
node.error(`Error destroying logger: ${error.message}`);
}
}
});
}
// Register the node
RED.nodes.registerType('logs-interceptor', LogsInterceptorNode);
// Register node configuration
RED.httpAdmin.get('/logs-interceptor/config', function (req, res) {
res.json({
name: 'logs-interceptor',
label: 'Logs Interceptor',
category: 'logging',
defaults: {
name: { value: '' },
url: { value: '' },
tenantId: { value: '' },
appName: { value: 'node-red' },
environment: { value: 'production' },
},
inputs: 1,
outputs: 1,
icon: 'file.png',
});
});
};
//# sourceMappingURL=logs-interceptor-node.js.map