UNPKG

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.

86 lines 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoggerFactory = void 0; /** * Presentation: LoggerFactory * Factory for creating Logger instances with dependency injection */ const Logger_1 = require("./Logger"); const CreateLogEntryUseCase_1 = require("../application/use-cases/CreateLogEntryUseCase"); const LogFilterService_1 = require("../infrastructure/services/LogFilterService"); const SanitizationService_1 = require("../infrastructure/services/SanitizationService"); const SamplingService_1 = require("../infrastructure/services/SamplingService"); const ContextService_1 = require("../infrastructure/services/ContextService"); const LokiLogRepository_1 = require("../infrastructure/repositories/LokiLogRepository"); const AxiosHttpClient_1 = require("../infrastructure/http/AxiosHttpClient"); const LokiFormatter_1 = require("../infrastructure/formatters/LokiFormatter"); const CircuitBreakerService_1 = require("../infrastructure/services/CircuitBreakerService"); class LoggerFactory { static create(config) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1; // Create sanitization service const sanitizationConfig = { sensitivePatterns: (_b = (_a = config.filter) === null || _a === void 0 ? void 0 : _a.sensitivePatterns) !== null && _b !== void 0 ? _b : [ /password/i, /token/i, /secret/i, /api[_-]?key/i, /authorization/i, /credit[_-]?card/i, /ssn/i, /cpf/i, ], }; const sanitizationService = new SanitizationService_1.SanitizationService(sanitizationConfig); // Create sampling service const samplingService = new SamplingService_1.SamplingService(); // Create filter service const filterConfig = { levels: (_d = (_c = config.filter) === null || _c === void 0 ? void 0 : _c.levels) !== null && _d !== void 0 ? _d : ['debug', 'info', 'warn', 'error', 'fatal'], patterns: (_e = config.filter) === null || _e === void 0 ? void 0 : _e.patterns, samplingRate: (_g = (_f = config.filter) === null || _f === void 0 ? void 0 : _f.samplingRate) !== null && _g !== void 0 ? _g : 1.0, maxMessageLength: (_j = (_h = config.filter) === null || _h === void 0 ? void 0 : _h.maxMessageLength) !== null && _j !== void 0 ? _j : 8192, sanitize: (_l = (_k = config.filter) === null || _k === void 0 ? void 0 : _k.sanitize) !== null && _l !== void 0 ? _l : true, }; const filterService = new LogFilterService_1.LogFilterService(filterConfig, sanitizationService, samplingService); // Create context service const contextConfig = { appName: config.appName, version: (_m = config.version) !== null && _m !== void 0 ? _m : '1.0.0', environment: (_o = config.environment) !== null && _o !== void 0 ? _o : 'production', labels: config.labels, dynamicLabels: config.dynamicLabels, }; const contextService = new ContextService_1.ContextService(contextConfig); // Create circuit breaker const circuitBreakerConfig = { enabled: (_q = (_p = config.circuitBreaker) === null || _p === void 0 ? void 0 : _p.enabled) !== null && _q !== void 0 ? _q : true, failureThreshold: (_s = (_r = config.circuitBreaker) === null || _r === void 0 ? void 0 : _r.failureThreshold) !== null && _s !== void 0 ? _s : 5, resetTimeout: (_u = (_t = config.circuitBreaker) === null || _t === void 0 ? void 0 : _t.resetTimeout) !== null && _u !== void 0 ? _u : 60000, halfOpenRequests: (_w = (_v = config.circuitBreaker) === null || _v === void 0 ? void 0 : _v.halfOpenRequests) !== null && _w !== void 0 ? _w : 3, }; const circuitBreaker = new CircuitBreakerService_1.CircuitBreakerService(circuitBreakerConfig); // Create HTTP client const httpClient = new AxiosHttpClient_1.AxiosHttpClient(); // Create formatter const formatter = new LokiFormatter_1.LokiFormatter(); // Create repository const repositoryConfig = { url: config.transport.url, tenantId: config.transport.tenantId, authToken: config.transport.authToken, timeout: (_x = config.transport.timeout) !== null && _x !== void 0 ? _x : 5000, maxRetries: (_y = config.transport.maxRetries) !== null && _y !== void 0 ? _y : 3, retryDelay: (_z = config.transport.retryDelay) !== null && _z !== void 0 ? _z : 1000, compression: (_0 = config.transport.compression) !== null && _0 !== void 0 ? _0 : true, compressionLevel: (_1 = config.transport.compressionLevel) !== null && _1 !== void 0 ? _1 : 6, }; const repository = new LokiLogRepository_1.LokiLogRepository(repositoryConfig, httpClient, formatter, circuitBreaker); // Create use case const createLogEntryUseCase = new CreateLogEntryUseCase_1.CreateLogEntryUseCase(repository, filterService, contextService); // Create logger return new Logger_1.Logger(createLogEntryUseCase, repository, contextService); } } exports.LoggerFactory = LoggerFactory; //# sourceMappingURL=LoggerFactory.js.map