Skip to content

Commit

Permalink
feat: adding response body to the access log
Browse files Browse the repository at this point in the history
  • Loading branch information
artursudnik committed Oct 17, 2023
1 parent 7067bc9 commit a7e482b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions apps/vc-api/src/middlewares/http-logger.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ export class HttpLoggerMiddleware implements NestMiddleware {

let finished = false;

const oldWrite = res.write;
const oldEnd = res.end;
const chunks: Buffer[] = [];
let responseBody: string;

res.write = (...args: unknown[]): boolean => {
chunks.push(args[0] as Buffer);
return oldWrite.apply(res, args);
};

res.end = (...args: unknown[]) => {
const chunk = args[0] as Buffer;

if (chunk) {
chunks.push(chunk);
}

responseBody = Buffer.concat(chunks).toString();
return oldEnd.apply(res, args);
};

res.on('finish', () => {
const message = `${res.statusCode} ${res.statusMessage} | ${req.ip} | [${method}] ${url} - ${
Date.now() - requestStarted
Expand All @@ -30,6 +51,10 @@ export class HttpLoggerMiddleware implements NestMiddleware {
if (req.body) {
this.logger.debug(`request body: ${JSON.stringify(req.body)}`);
}

if (responseBody) {
this.logger.debug(`response body: ${responseBody}`);
}
});

res.on('close', () => {
Expand Down

0 comments on commit a7e482b

Please sign in to comment.