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.
171 lines • 4.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogLevelVO = exports.logger = exports.destroy = exports.isInitialized = exports.getLogger = exports.init = void 0;
/**
* Main Entry Point - Clean Architecture Implementation
* Public API for logs-interceptor
*/
const LogsInterceptorFactory_1 = require("./presentation/factory/LogsInterceptorFactory");
const application_1 = require("./application");
const utils_1 = require("./utils");
let globalInstance = null;
/**
* Initialize the logs interceptor with configuration
* Can be called multiple times - subsequent calls will update the configuration
*/
function init(userConfig = {}) {
// Load configuration from environment
const envConfig = (0, utils_1.loadConfigFromEnv)();
// Merge configurations
const mergedConfig = (0, utils_1.mergeConfigs)(userConfig, envConfig);
// Validate configuration
const errors = application_1.ConfigService.validate(mergedConfig);
if (errors.length > 0) {
throw new Error(`Configuration errors:\n${errors.join('\n')}`);
}
// Resolve configuration with defaults
const resolvedConfig = application_1.ConfigService.resolve(mergedConfig);
// Destroy existing instance if it exists
if (globalInstance) {
globalInstance.destroy().catch(() => {
// Ignore errors during cleanup
});
}
// Create new instance using factory
const { logger } = LogsInterceptorFactory_1.LogsInterceptorFactory.create(resolvedConfig);
globalInstance = logger;
return logger;
}
exports.init = init;
/**
* Get the global logger instance
* Throws an error if not initialized
*/
function getLogger() {
if (!globalInstance) {
throw new Error('LogsInterceptor not initialized. Call init() first.');
}
return globalInstance;
}
exports.getLogger = getLogger;
/**
* Check if the logger is initialized
*/
function isInitialized() {
return globalInstance !== null;
}
exports.isInitialized = isInitialized;
/**
* Destroy the global logger instance
*/
async function destroy() {
if (globalInstance) {
await globalInstance.destroy();
globalInstance = null;
}
}
exports.destroy = destroy;
/**
* Auto-initialize if environment variables are present
* This allows the logger to work automatically when loaded via NODE_OPTIONS
*/
function autoInit() {
// Only auto-initialize if we have the required environment variables
const envConfig = (0, utils_1.loadConfigFromEnv)();
if (envConfig.transport?.url &&
envConfig.transport?.tenantId &&
envConfig.appName &&
process.env.LOGS_INTERCEPTOR_ENABLED !== 'false') {
try {
init(envConfig);
console.log('[logs-interceptor] Auto-initialized from environment variables');
}
catch (error) {
console.error('[logs-interceptor] Auto-initialization failed:', error);
}
}
}
// Convenience exports for direct usage without initialization
exports.logger = {
/**
* Log a debug message
*/
debug: (message, context) => {
if (globalInstance) {
globalInstance.debug(message, context);
}
},
/**
* Log an info message
*/
info: (message, context) => {
if (globalInstance) {
globalInstance.info(message, context);
}
},
/**
* Log a warning message
*/
warn: (message, context) => {
if (globalInstance) {
globalInstance.warn(message, context);
}
},
/**
* Log an error message
*/
error: (message, context) => {
if (globalInstance) {
globalInstance.error(message, context);
}
},
/**
* Log a fatal message
*/
fatal: (message, context) => {
if (globalInstance) {
globalInstance.fatal(message, context);
}
},
/**
* Track an event
*/
trackEvent: (eventName, properties) => {
if (globalInstance) {
globalInstance.trackEvent(eventName, properties);
}
},
/**
* Force flush logs
*/
flush: async () => {
if (globalInstance) {
return globalInstance.flush();
}
},
/**
* Get metrics
*/
getMetrics: () => {
return globalInstance?.getMetrics();
},
/**
* Get health status
*/
getHealth: () => {
return globalInstance?.getHealth();
},
};
// Auto-initialize when module is loaded
autoInit();
// Default export for convenience
exports.default = {
init,
getLogger,
isInitialized,
destroy,
logger: exports.logger,
};
var LogLevel_1 = require("./domain/value-objects/LogLevel");
Object.defineProperty(exports, "LogLevelVO", { enumerable: true, get: function () { return LogLevel_1.LogLevelVO; } });
//# sourceMappingURL=index.js.map