UNPKG

@ogma/nestjs-module

Version:

A NestJS module for the Ogma logger

118 lines (117 loc) 5.29 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractInterceptorService = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const styler_1 = require("@ogma/styler"); let AbstractInterceptorService = class AbstractInterceptorService { constructor(reflector) { this.reflector = reflector; } /** * A method to transform the incoming execution context into metadata that the OgmaInterceptor will then log. * This method handles the success cases * @param data the response body that will be returned * @param context the execution context from Nest * @param startTime when the request started * @param options the options passed to the interceptor * @returns an object that represents what should be logged */ getSuccessContext(data, context, startTime, options) { const stringifiedData = data ? JSON.stringify(data) : ''; const dataLength = Buffer.from(stringifiedData).byteLength; return { callerAddress: this.getCallerIp(context), method: this.getMethod(context), callPoint: this.getCallPoint(context), responseTime: this.getResponseTime(startTime), contentLength: dataLength, protocol: this.getProtocol(context), status: this.getStatus(context, options.color && !options.json), meta: this.getMeta(context, data), }; } /** * A method to transform the incoming execution context into metadata that the OgmaInterceptor will then log. * This method handles the error cases * @param error the error that happened * @param context the execution context from Nest * @param startTime when the request started * @param options the options passed to the interceptor * @returns an object that represents what should be logged */ getErrorContext(error, context, startTime, options) { return { callerAddress: this.getCallerIp(context), method: this.getMethod(context), callPoint: this.getCallPoint(context), status: this.getStatus(context, options.color && !options.json, error), responseTime: this.getResponseTime(startTime), contentLength: Buffer.from(JSON.stringify(error.message)).byteLength, protocol: this.getProtocol(context), meta: this.getMeta(context, error), }; } /** * A helper method to get the status based on if the request was an error or success * @param _context the execution context * @param inColor if the status should be in color * @param error if it was an error * @returns a string representing the status */ getStatus(_context, inColor, error) { const status = error ? 500 : 200; return inColor ? this.wrapInColor(status) : status.toString(); } /** * A helper method to allow devs the ability to pass in extra metadata when it comes to the interceptor * @param _context The ArgumentsHost * @param _data the response body or the error being returned * @returns whatever metadata you want to add in on a second log line. This can be a string, an object, anything */ getMeta(_context, _data) { return; } getResponseTime(startTime) { return Date.now() - startTime; } wrapInColor(status) { let statusString; if (this.isBetween(status, 100, 300)) { statusString = styler_1.style.green.apply(status); } else if (this.isBetween(status, 300, 400)) { statusString = styler_1.style.cyan.apply(status); } else if (this.isBetween(status, 400, 500)) { statusString = styler_1.style.yellow.apply(status); } else if (this.isBetween(status, 500, 600)) { statusString = styler_1.style.red.apply(status); } else { statusString = styler_1.style.white.apply(status); } return statusString; } isBetween(comparator, bottom, top) { return comparator >= bottom && comparator < top; } getStartTime(_host) { return Date.now(); } }; exports.AbstractInterceptorService = AbstractInterceptorService; exports.AbstractInterceptorService = AbstractInterceptorService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [core_1.Reflector]) ], AbstractInterceptorService);