logs-interceptor-node14
Version:
High-performance, production-ready log interceptor for Node.js 14 applications with Loki integration
181 lines • 5.55 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.destroy = exports.isInitialized = exports.getLogger = exports.init = exports.LogsInterceptor = void 0;
const logger_1 = require("./logger");
const utils_1 = require("./utils");
var logger_2 = require("./logger");
Object.defineProperty(exports, "LogsInterceptor", { enumerable: true, get: function () { return logger_2.LogsInterceptor; } });
__exportStar(require("./types"), exports);
__exportStar(require("./utils"), exports);
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 finalConfig = (0, utils_1.mergeConfigs)(userConfig, envConfig);
// Validate configuration
const errors = (0, utils_1.validateConfig)(finalConfig);
if (errors.length > 0) {
throw new Error(`Configuration errors:\n${errors.join('\n')}`);
}
// Destroy existing instance if it exists
if (globalInstance) {
globalInstance.destroy().catch(() => {
// Ignore errors during cleanup
});
}
// Create new instance
globalInstance = new logger_1.LogsInterceptor(finalConfig);
return globalInstance;
}
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() {
var _a, _b;
// Only auto-initialize if we have the required environment variables
const envConfig = (0, utils_1.loadConfigFromEnv)();
if (((_a = envConfig.transport) === null || _a === void 0 ? void 0 : _a.url) &&
((_b = envConfig.transport) === null || _b === void 0 ? void 0 : _b.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 === null || globalInstance === void 0 ? void 0 : globalInstance.getMetrics();
},
/**
* Get health status
*/
getHealth: () => {
return globalInstance === null || globalInstance === void 0 ? void 0 : globalInstance.getHealth();
},
};
// Auto-initialize when module is loaded
autoInit();
// Default export for convenience
exports.default = {
init,
getLogger,
isInitialized,
destroy,
logger: exports.logger,
LogsInterceptor: logger_1.LogsInterceptor,
};
//# sourceMappingURL=index.js.map