morgan-body icon indicating copy to clipboard operation
morgan-body copied to clipboard

it is possible to get informations from request and add to the log?

Open instplanet opened this issue 4 years ago • 5 comments

I want add some informations that are passed thought the request object in log, this is possible?

instplanet avatar Jun 03 '20 11:06 instplanet

@instplanet thank you for reaching out, do you mind being a bit more specific? It's not something that's part of the request or response body, correct?

Mind giving an example you'd like to work that doesn't now?

sirrodgepodge avatar Jun 04 '20 01:06 sirrodgepodge

Let me explain better, i have a middleware that add some informations to my request like the following:

req.metadata = {
        syncRegister: false,
        collectionNamespaces: {
          tenantCollection: credentials.mongoCredentials.dbName,
          managerCollection: credentials.appConfiguraton.dbName,
          pdvCollection: credentials.appConfiguraton.pdv_configuration.mongo_connection.database
        },
        accountId: decoded.accountId,
        unitId: decoded.unitId,
        requestOriginalUrl: req.originalUrl,
        currentDate: getCurrentDate(),
        requestId: req.id
 }

this informations is very important because, with it, I can identify who is sending the request and would like to add it to log.

The problem is that this information is not passed on at the customer's request and I cannot return that information in the return request because it contains confidential information

let me know if I was not very clear with the explanation and thanks for your attention and help

instplanet avatar Jun 04 '20 19:06 instplanet

got it, so basically you have a middleware before morgan-body which adds to the req object and you'd like to be able to log that...

btw, side note, just in case you didn't notice already, req.id is actually already part of the logs.

okay I can imagine adding as an option an array of "additional custom properties on the request object to log" and logging in a way similar to request/response body. I would log these in order after the "request body" and before the "response body", sound good?

sirrodgepodge avatar Jun 06 '20 18:06 sirrodgepodge

This sounds pretty good, will help me and others to customize log Thanks again for your attention and help

instplanet avatar Jun 08 '20 17:06 instplanet

unfortunately I don't have time to work on this right now to the extent it deserved (this project is used quite a bit and needs to be tested) :(, I would gladly review a PR

sirrodgepodge avatar Jul 19 '20 00:07 sirrodgepodge