grassrootsgrocery / admin-portal

GNU Affero General Public License v3.0
11 stars 6 forks source link

Add username to standard server logging #137

Open mattsahn opened 1 year ago

mattsahn commented 1 year ago

Our current logs show timestamp, type of request, and URL details, but do not show which logged-in user made the request. Our intention is to move towards using actual users for each user rather than a single, shared admin user. At that point, it's important for logs to show who each user is that is interacting with the server. This issue is to add username to the standard log output for all requests. Format could be this:

[2023-09-08T21:42:48.173Z] INFO: (grassrootsadmin) PATCH /api/volunteers/confirm/recDiz3Sk4kBOnrP6

Existing logs:

[2023-09-08T21:42:49.139Z] INFO: GET /api/volunteers/?scheduledSlotsIds=recFFagozjumfhj3n,recAjTcvDQ22i6or1,recIPHzNWVPfBm5vW,rec9RG9jewloGivId,recTTkQwxRryriE7p,recJmhW5nC0mV3rEp,reccNh35DGvy3SCx2,recF4YkKjP1PBV4PY,recbnpZ9519hcVNH2,recijrJrU3kqsq7cC,rec8VL3ppUcLNI7Tm,rectXTQgh1zgziAR7,rec4IciFRgvJAcqnY,recnWqvTtwB96nyeg,recA0yyrebmoPaZOZ,rec0mfgd3WU2Skav0,recoIEipbWwPB5ijK,recCQUmkjrmHHgIbD,recPKXbm3P4bFpSkC,recpkOFzV6FQGXZQY,rec0m1QcmFWxn3uy7,recyabRowFFwOjpi8,recSDKnE1QpspKaBo,recTI4rQxRoKswjxb,recyzaXVuqWVEpDtz,recIANwKbzyJvi1Zq,recuj15wekLL0bZO3,rec0Rd15VfNSqA0Ok,rec7AvaLlgAAy7Dct,recO2A3rpa3mdfI7B,recRxwqpusCJFF9fq,recrlEJX0709JadR5,recyesgX7SS8VdN9i,rec6aM1pE7c7RPzEX,recE8Rnbghqp9BwBF,recquH2LVpASSZiYK,recfu7qMjIYkiTmR6,rec2bB46z47hwtWbd,recKNx5Yr9KOD8P3k,recdw4NoEZIznh5jw,recZvusTbeMxQnhl6,rec13iaR6qAlElAVM,rec9UZidA5rXi4ASW,recN02i9uvoSAZEfS,recFPlCphG1Il5Kku,recHuKOZq07mS5M6b,recIJWEtImCfT5M6Q,recStlmxDHPoqoYfb,recIyjZFaFWZI6VqI,recrPCLbJYdWgRs3q,recE5PKstpVXMWXMP,recggQdjwg1xFuxvs,recrI0iYbPq2QZvd5,recLfhaARAIxD3G2V,recDiz3Sk4kBOnrP6

[2023-09-08T21:48:11.180Z] INFO: GET /api/special-groups

[2023-09-08T21:48:11.586Z] INFO: GET /api/events/view-event-special-groups/?
6mp commented 1 year ago
export const protect = asyncHandler(async (req, res, next) => {
  const user = await checkTokenAndExtractUser(req, res);
  res.locals.user = user;
  logger.info(`(${user.fields.Username}) ${req.method} ${req.originalUrl}`);
  next();
});

Adding this snippet to the auth middleware allows for logging to be done easily on all the api calls. The only issue is other logs within the functions dont have the username so I am finding a fix for that.