Skip to content

Conversation

@jsumners
Copy link
Member

@jsumners jsumners commented May 4, 2019

This addresses issue #1624. Instead of attempting to pass along a limited set of information such that it can be logged at the end of the request, which would lead to a never ending "I need this info there" path, this PR adds an option to disable the initial and final request logs. The result is you can attach your own onRequest and onResponse hooks to log the information you need and not get double log messages due to the enforced logging in Fastify.

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

@jsumners jsumners requested a review from a team May 4, 2019 17:53
@SerayaEryn
Copy link
Contributor

This is a nice solution to the problem of duplicate log messages. 👍

I wonder if it would be good to have one option that disables all built-in logs.
There are (at least) two more locations where something is being logged:

logger.debug({ err }, 'client error')

this.log.info('Server listening at ' + address)

@jsumners
Copy link
Member Author

jsumners commented May 5, 2019

I don't see a reason to disable the other logs. The other logs are a key feature of Fastify: integrated logging. The catch with the logs at the start and end of the request is that these logs are usually parsed tools to determine usage and performance metrics, e.g. "how many people are using my routes and with what parameters" (incoming request) and "how long are my route taking to respond with which status codes" (outgoing response). Different people have different requirements as to what information they need to accomplish those tasks. So it's difficult to provide general log statements in these situations. The proposed PR allows the user to determine if what exists is good enough for them or to provide their own implementation if necessary.

That being said, feel free to submit a PR that implements whatever functionality you need.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, awesome

@jsumners jsumners merged commit 073f747 into master May 5, 2019
@jsumners jsumners deleted the diable-req-logging branch May 5, 2019 16:37
@delvedor delvedor added the semver-minor Issue or PR that should land as semver minor label May 6, 2019
@jsumners
Copy link
Member Author

@delvedor @mcollina what's the next release window for this?

@mcollina
Copy link
Member

Likely next week.

})

fastify.addHook('onResponse', (req, reply, next) => {
req.log.info({ url: req.req.originalUrl, statusCode: res.res.statusCode }, 'request completed')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.res.statusCode should be reply.res.statusCode and req.log.info should be reply.log.info in next line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A PR to fix it is welcome.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsumners Added you as reviewer Here

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver-minor Issue or PR that should land as semver minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants