UNPKG

@tsed/common

Version:
119 lines 3.96 kB
"use strict"; var PlatformLogMiddleware_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlatformLogMiddleware = void 0; const tslib_1 = require("tslib"); const di_1 = require("@tsed/di"); const middleware_1 = require("../../mvc/decorators/class/middleware"); const context_1 = require("../decorators/context"); /** * @middleware * @platform */ let PlatformLogMiddleware = PlatformLogMiddleware_1 = class PlatformLogMiddleware { // tslint:disable-next-line: no-unused-variable constructor(injector) { this.settings = injector.settings.logger || {}; this.settings.requestFields = this.settings.requestFields || PlatformLogMiddleware_1.DEFAULT_FIELDS; if (this.settings.level !== "off") { this.$onResponse = this.onLogEnd.bind(this); } } /** * Handle the request. */ use(ctx) { this.configureRequest(ctx); this.onLogStart(ctx); } /** * The separate onLogStart() function will allow developer to overwrite the initial request log. * @param ctx */ onLogStart(ctx) { const { debug, logRequest, logStart } = this.settings; if (logStart !== false) { if (debug) { ctx.logger.debug({ event: "request.start" }); } else if (logRequest) { ctx.logger.info({ event: "request.start" }); } } } /** * Called when the `$onResponse` is called by Ts.ED (through Express.end). */ onLogEnd(ctx) { const { debug, logRequest, logEnd } = this.settings; if (logEnd !== false) { if (debug) { ctx.logger.debug({ event: "request.end", status: ctx.response.statusCode, data: ctx.data }); } else if (logRequest) { ctx.logger.info({ event: "request.end", status: ctx.response.statusCode }); } } ctx.logger.flush(); } /** * Attach all information that will be necessary to log the request. Attach a new `request.log` object. */ configureRequest(ctx) { ctx.logger.minimalRequestPicker = (obj) => ({ ...this.minimalRequestPicker(ctx), ...obj }); ctx.logger.completeRequestPicker = (obj) => ({ ...this.requestToObject(ctx), ...obj }); } /** * Return complete request info. * @returns {Object} * @param ctx */ requestToObject(ctx) { const { request } = ctx; return { method: request.method, url: request.url, headers: request.headers, body: request.body, query: request.query, params: request.params }; } /** * Return a filtered request from global configuration. * @returns {Object} * @param ctx */ minimalRequestPicker(ctx) { const { requestFields } = this.settings; const info = this.requestToObject(ctx); return requestFields.reduce((acc, key) => { acc[key] = info[key]; return acc; }, {}); } }; PlatformLogMiddleware.DEFAULT_FIELDS = ["reqId", "method", "url", "duration"]; tslib_1.__decorate([ tslib_1.__param(0, context_1.Context()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [Object]), tslib_1.__metadata("design:returntype", void 0) ], PlatformLogMiddleware.prototype, "use", null); PlatformLogMiddleware = PlatformLogMiddleware_1 = tslib_1.__decorate([ middleware_1.Middleware(), tslib_1.__metadata("design:paramtypes", [di_1.InjectorService]) ], PlatformLogMiddleware); exports.PlatformLogMiddleware = PlatformLogMiddleware; //# sourceMappingURL=PlatformLogMiddleware.js.map