UNPKG

coffee-core

Version:

Coffee IT API core library

41 lines 2.09 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var LoggingInterceptor_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoggingInterceptor = void 0; const common_1 = require("@nestjs/common"); const custom_logger_1 = require("./custom-logger"); const rxjs_1 = require("rxjs"); let LoggingInterceptor = LoggingInterceptor_1 = class LoggingInterceptor { logger = new custom_logger_1.CustomLogger(LoggingInterceptor_1.name); excludedPaths = ['/health']; intercept(context, next) { const req = context.switchToHttp().getRequest(); if (this.excludedPaths.includes(req.path)) { return next.handle(); } if (!req.id) { req.id = this.generateRequestId(); } const userId = req.user?.userId ?? ''; this.logger.log(`[Request ${req.id}] ${req.method} ${req.url} by user ${userId} with body ${JSON.stringify(req.body, null, 2)}`); const startOfRequest = Date.now(); return next .handle() .pipe((0, rxjs_1.tap)(() => this.logger.log(`[Request ${req.id}] Completed after... ${Date.now() - startOfRequest}ms`))); } generateRequestId() { const randomString = Math.random().toString(36).substring(2, 8); return `${Date.now()}-${randomString}`; } }; exports.LoggingInterceptor = LoggingInterceptor; exports.LoggingInterceptor = LoggingInterceptor = LoggingInterceptor_1 = __decorate([ (0, common_1.Injectable)() ], LoggingInterceptor); //# sourceMappingURL=logging.interceptor.js.map