Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Add logging of request inputs (#69)
Browse files Browse the repository at this point in the history
* Add logging of request inputs

* Safe
  • Loading branch information
varney authored Jul 2, 2024
1 parent a591e9f commit 75353ec
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ const isHealthOrMetricRequest = require("./is-health-or-metric-request");
const requestMetrics = require("./request-metrics");
const statusShutdown = require("./status-middleware-when-shutting-down");

function hasData(obj) {
return obj && Object.keys(obj).length;
}

function accessLog(req, res, next) {
const time = new Date();

if (!isHealthOrMetricRequest(req)) {
const usefulStuff = [];
if (hasData(req.body)) usefulStuff.push(`body: ${JSON.stringify(req.body)}`);
if (hasData(req.query)) usefulStuff.push(`query: ${JSON.stringify(req.query)}`);
const inputs = usefulStuff.length ? ` with ${usefulStuff.join(", ")}` : "";
logger.info(`${req.method} received on ${req.url}${inputs}`);
}

function afterResponse() {
res.removeListener("finish", afterResponse);
res.removeListener("close", afterResponse);
Expand Down

0 comments on commit 75353ec

Please sign in to comment.