Skip to content

Commit

Permalink
chore: diminue le niveau d'erreur des 404
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ST0R committed Oct 17, 2023
1 parent 5911c6f commit 4fccbeb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions server/src/http/middlewares/logMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { omitBy } from "lodash-es";
import { logger } from "#src/common/logger.js";

function errorLevel(req, res, error) {
const statusCode = res.statusCode;

if (statusCode === 404) {
return "warn";
}

if (error || (statusCode >= 400 && statusCode < 600)) {
return "error";
}
return "info";
}

export function logMiddleware() {
return (req, res, next) => {
const relativeUrl = (req.baseUrl || "") + (req.url || "");
Expand Down Expand Up @@ -33,7 +46,7 @@ export function logMiddleware() {
body: withoutSensibleFields(req.body),
},
response: {
statusCode,
status: statusCode,
headers: res.getHeaders(),
},
...(!error
Expand All @@ -47,9 +60,8 @@ export function logMiddleware() {
}),
};

const level = error || (statusCode >= 400 && statusCode < 600) ? "error" : "info";

logger[level](data, `Http Request ${level === "error" ? "KO" : "OK"}`);
const level = errorLevel(req, res, error);
logger[level](data, `Http Request ${level !== "info" ? "KO" : "OK"}`);
} finally {
res.removeListener("finish", log);
res.removeListener("close", log);
Expand Down

0 comments on commit 4fccbeb

Please sign in to comment.