Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] logging response body #283

Open
kundkingan opened this issue Mar 29, 2023 · 3 comments
Open

[QUESTION] logging response body #283

kundkingan opened this issue Mar 29, 2023 · 3 comments

Comments

@kundkingan
Copy link

Is it possible to log response body with the request complete log?

Currently in the res serializers, it's not possible to access the returning body to add it to the final log.

@maxckelly
Copy link

@kundkingan did you figure this out?

@DmitriyMolochkov
Copy link

You can catch the context using a property with a getter

const middleware = (req, res, next) => {
  const logger = pinoHttp({
      serializers: {
        res(res) {
          res.body = res.raw.locals.body;
  
          return res;
        },
    },
  })

  logger(
    req,
    Object.defineProperty(res, 'locals', {
      get() {
        return { body: res.body };
      },
      enumerable: true,
      configurable: true,
    }),
  )

  next();
};

@kundkingan
Copy link
Author

@kundkingan did you figure this out?

Haven't tried it out yet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants