Skip to content

Commit

Permalink
fix(api): convert _id to string in logs as to not loose data
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-telescoop committed Nov 13, 2023
1 parent bbfcf18 commit 68c9dd8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/api/src/middlewares/LogMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import winston from "winston";
import expressWinston from "express-winston";
import { client } from "../shared/MongoConnection";

import "winston-mongodb";
import { ObjectId } from "mongodb";
import { client } from "../shared/MongoConnection";

const LOGGER_SECRET_FIELDS = ["password", "token"];

Expand Down Expand Up @@ -49,6 +49,11 @@ export const expressLogger = () =>
requestFilter: (req, propName) => {
if (propName === "body" && typeof req[propName] === "object" && req[propName])
recursiveFilter(req[propName]);

// @ts-expect-error strange express-winston types
// we convert _id into string as a workaround to winston-mongodb bug that serializes them to {}
if (propName === "user" && req[propName]?._id) req[propName]._id = req[propName]._id.toString();

return LOGGER_SECRET_FIELDS.includes(propName) ? "**********" : req[propName];
},
responseFilter: (req, propName) => {
Expand Down

0 comments on commit 68c9dd8

Please sign in to comment.