All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.dist.src.config.middleware.request-logging-middleware.js Maven / Gradle / Ivy

The newest version!
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestLoggingMiddleware = void 0;
const __1 = require("../..");
class LoggingMiddlewareRequestHandler {
    constructor(logger, requestId) {
        this.logger = logger;
        this.requestId = requestId;
    }
    onRequestMetadata(metadata) {
        this.logger.info(JSON.stringify({
            event: 'onRequestMetadata',
            requestId: this.requestId,
            ...metadata.toJsonObject(),
        }));
        return Promise.resolve(metadata);
    }
    onRequestBody(request) {
        this.logger.info(JSON.stringify({
            event: 'onRequestBody',
            requestId: this.requestId,
            requestSize: request.messageLength(),
            ...request.toLogFormat(),
        }));
        return Promise.resolve(request);
    }
    onResponseMetadata(metadata) {
        this.logger.info(JSON.stringify({
            event: 'onResponseMetadata',
            requestId: this.requestId,
            ...metadata.toJsonObject(),
        }));
        return Promise.resolve(metadata);
    }
    onResponseBody(response) {
        var _a;
        this.logger.info(JSON.stringify({
            event: 'onResponseBody',
            requestId: this.requestId,
            ...(_a = response === null || response === void 0 ? void 0 : response._grpcMessage) === null || _a === void 0 ? void 0 : _a.toObject(),
        }));
        return Promise.resolve(response);
    }
    onResponseStatus(status) {
        this.logger.info(JSON.stringify({
            event: 'onResponseStatus',
            requestId: this.requestId,
            status: status.code(),
        }));
        return Promise.resolve(status);
    }
}
/**
 * This middleware implements per-request logging which can be used for
 * debugging.
 *
 * WARNING: enabling this middleware may have minor performance implications,
 * so enable with caution.
 *
 * In order for this middleware to produce output you will need to have
 * set up your {Configuration} with a {MomentoLoggerFactory} instance that
 * is configured to log at DEBUG level or lower.  See `advanced.ts` in the
 * examples directory for an example of how to set up your {Configuration} to
 * enable this middleware.
 */
class RequestLoggingMiddleware {
    constructor(loggerFactory = new __1.DefaultMomentoLoggerFactory()) {
        this.logger = loggerFactory.getLogger(this);
        this.nextRequestId = 0;
    }
    onNewRequest() {
        this.nextRequestId++;
        return new LoggingMiddlewareRequestHandler(this.logger, this.nextRequestId.toString());
    }
}
exports.RequestLoggingMiddleware = RequestLoggingMiddleware;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVxdWVzdC1sb2dnaW5nLW1pZGRsZXdhcmUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvY29uZmlnL21pZGRsZXdhcmUvcmVxdWVzdC1sb2dnaW5nLW1pZGRsZXdhcmUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBT0EsNkJBSWU7QUFFZixNQUFNLCtCQUErQjtJQUduQyxZQUFZLE1BQXFCLEVBQUUsU0FBaUI7UUFDbEQsSUFBSSxDQUFDLE1BQU0sR0FBRyxNQUFNLENBQUM7UUFDckIsSUFBSSxDQUFDLFNBQVMsR0FBRyxTQUFTLENBQUM7SUFDN0IsQ0FBQztJQUVELGlCQUFpQixDQUFDLFFBQTRCO1FBQzVDLElBQUksQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUNkLElBQUksQ0FBQyxTQUFTLENBQUM7WUFDYixLQUFLLEVBQUUsbUJBQW1CO1lBQzFCLFNBQVMsRUFBRSxJQUFJLENBQUMsU0FBUztZQUN6QixHQUFHLFFBQVEsQ0FBQyxZQUFZLEVBQUU7U0FDM0IsQ0FBQyxDQUNILENBQUM7UUFDRixPQUFPLE9BQU8sQ0FBQyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUM7SUFDbkMsQ0FBQztJQUNELGFBQWEsQ0FBQyxPQUEwQjtRQUN0QyxJQUFJLENBQUMsTUFBTSxDQUFDLElBQUksQ0FDZCxJQUFJLENBQUMsU0FBUyxDQUFDO1lBQ2IsS0FBSyxFQUFFLGVBQWU7WUFDdEIsU0FBUyxFQUFFLElBQUksQ0FBQyxTQUFTO1lBQ3pCLFdBQVcsRUFBRSxPQUFPLENBQUMsYUFBYSxFQUFFO1lBQ3BDLEdBQUcsT0FBT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