UNPKG

logs-interceptor

Version:

High-performance, production-ready log interceptor for Node.js applications with Loki integration

50 lines (44 loc) 1.57 kB
/** * Preload script for automatic initialization of logs-interceptor * * Usage: * NODE_OPTIONS="--require logs-interceptor/preload" \ * LOGS_INTERCEPTOR_URL="http://localhost:3100/loki/api/v1/push" \ * LOGS_INTERCEPTOR_TENANT_ID="tenant1" \ * LOGS_INTERCEPTOR_APP_NAME="my-app" \ * node app.js */ const { init, loadConfigFromEnv, validateConfig } = require('./dist/index.js'); try { // Load configuration from environment const envConfig = loadConfigFromEnv(); // Only initialize if we have required config and it's not explicitly disabled if ( envConfig.transport?.url && envConfig.transport?.tenantId && envConfig.appName && process.env.LOGS_INTERCEPTOR_ENABLED !== 'false' ) { // Validate configuration const errors = validateConfig(envConfig); if (errors.length > 0) { console.error('[logs-interceptor:preload] Configuration errors:', errors); process.exit(1); } // Initialize with console interception enabled by default init({ ...envConfig, interceptConsole: true, debug: process.env.LOGS_INTERCEPTOR_DEBUG === 'true', }); console.log('[logs-interceptor:preload] Initialized successfully'); } else { console.log('[logs-interceptor:preload] Skipped - missing required configuration'); } } catch (error) { console.error('[logs-interceptor:preload] Initialization failed:', error.message); // Don't exit on error in preload - let the app continue if (process.env.LOGS_INTERCEPTOR_FAIL_ON_ERROR === 'true') { process.exit(1); } }