UNPKG

@neoma/logging

Version:
88 lines 4.33 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 __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestLoggerInterceptor = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("@nestjs/common/constants"); const rxjs_1 = require("rxjs"); const symbols_1 = require("@lib/symbols"); /** * Interceptor that logs requests going into and coming out of route handlers. */ let RequestLoggerInterceptor = class RequestLoggerInterceptor { config; /** * Creates an instance of RequestLoggerInterceptor. * * @param config The logging configuration - the only property used is `logErrors` * which determines whether errors caught during request processing should be logged * automatically. */ constructor(config) { this.config = config; } /** * Intercepts the request and logs informtion about the route handler that the request * will be dispatched to and the result of processing once route handler has handled * the request. * * @param context Provides access to route handling information. * @param next used to continue processing the request. */ intercept(context, next) { const start = Date.now(); const controllerPath = Reflect.getMetadata(constants_1.PATH_METADATA, context.getClass()); const handlerPath = Reflect.getMetadata(constants_1.PATH_METADATA, context.getHandler()); const request = context.switchToHttp().getRequest(); request.logger.debug({ controller: { name: context.getClass().name, path: controllerPath }, handler: { name: context.getHandler().name, path: handlerPath }, }, "Processing an incoming request and dispatching it to a route handler."); return next.handle().pipe((0, rxjs_1.tap)({ next: () => { const response = context.switchToHttp().getResponse(); request.logger.debug({ controller: { name: context.getClass().name, path: controllerPath, }, handler: { name: context.getHandler().name, path: handlerPath }, res: response, duration: `${Date.now() - start}ms`, }, "Processed an incoming request that was successfully handled by a route handler."); }, error: (error) => { if (this.config.logErrors) { const duration = Date.now() - start; request.logger.error("Error processing an incoming request in the route handler.", { controller: { name: context.getClass().name, path: controllerPath, }, handler: { name: context.getHandler().name, path: handlerPath }, duration: `${duration}ms`, err: error, }); } }, })); } }; exports.RequestLoggerInterceptor = RequestLoggerInterceptor; exports.RequestLoggerInterceptor = RequestLoggerInterceptor = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(symbols_1.LOGGING_MODULE_OPTIONS)), __metadata("design:paramtypes", [Object]) ], RequestLoggerInterceptor); //# sourceMappingURL=request-logger.interceptor.js.map