Skip to content

feat: implement conditional request logging#5732

Merged
kibertoad merged 16 commits intomainfrom
feat/conditional-request-logging
Jan 13, 2026
Merged

feat: implement conditional request logging#5732
kibertoad merged 16 commits intomainfrom
feat/conditional-request-logging

Conversation

@kibertoad
Copy link
Member

@kibertoad kibertoad commented Oct 6, 2024

Use-case for this is following: some endpoints generate excessive amount of irrelevant noise (e. g. healthcheck) and are not useful in logs, so it would be great to be able to be able to resolve variable for logging their requests or not conditionally. And the big problem here is that these endpoints (healthchecks, metrics etc) frequently come from 3rd party plugins which register their routes themselves, therefore preventing extra configuration on a route level.

Note: Documentation linting failing does not seem to be related to changes in this PR.

Checklist

@kibertoad kibertoad requested a review from a team October 6, 2024 13:43
Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Why not?

const Fastify = require('./')
const fastify = Fastify({ logger: true })

fastify.get('/', {
  handler: () => { return 'foo' },
  logLevel: 'silent',
})

fastify.inject('/')

@kibertoad
Copy link
Member Author

@Eomm That is an option in some cases, but it has very limited granularity, and sometimes you may not even have necessary level of control, e. g. if route is registered by a third-party plugin that you are using. Also you may want to log some domain business logic, but not the "incoming request".

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

I believe it can be useful to apply custom logic to decide wether or not trigger logs accordingly to specific filters; but I'm wondering if this does not overlaps with route-level (e.g. the ones that @Eomm pointed out).

Also think that maybe this should be allowed to be set on a request basis, in case implementers want to fully disable logging for a registered route or apply custom logic to only that route.

@kibertoad
Copy link
Member Author

@metcoder95 @climba03003 I've adjusted the code. Are we good with the approach now, can I proceed with TS types and documentation?

…onditional-request-logging

# Conflicts:
#	lib/config-validator.js
#	lib/route.js
#	test/logger/logging.test.js
…' into feat/conditional-request-logging

# Conflicts:
#	lib/route.js
#	test/logger/logging.test.js
@github-actions github-actions bot added documentation Improvements or additions to documentation typescript TypeScript related labels Jan 6, 2026
@kibertoad kibertoad marked this pull request as ready for review January 6, 2026 22:54
@kibertoad kibertoad requested a review from Eomm January 6, 2026 22:55
@Eomm
Copy link
Member

Eomm commented Jan 7, 2026

Oh, this is a very old PR I forgot

My stands here can be resumed like:

#6417 (comment)

We need a better API because I think this the 4th PR related to "customize the custom logger"
it is not sustainable to add flags/options to change it, we need something like the schemaController where you can totally remove ajv with a custom implementation.

Here, we don't want a logController because it is already doable to change it via the loggerInstance, we need a frameworkLogs:{} where the user can inject whatever logic it wants.

This makes everybody happy:

  • no more stale PRs (this one is 2yo)
  • get what you want right now without a new flag
  • if a user does not use the feature, it does not pay the ifs penalty

@kibertoad
Copy link
Member Author

@Eomm I see this PR as complimentary to #6417. Ideally this one lands first to extend the surface that such customizable layer would allow customizing (currently it is not aiming to do so at all), and I agree that ultimately it needs to live there, but it's unclear when and if we will migrate to that mechanism fully.

Amount of ifs here is very small, I don't think there is any meaningful perf penalty. And long-term it would be removed together with the rest of reqLogging configuration.

Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

I do not have strong opinion on either go with a progressive or all in approach (flag vs fully fledged at once).

The downsides on both ways roots mostly on the amount of work to consolidate it eventually if we seek the fully fledged one to be the one we want to be the final goal.

The PR lgtm (just issues on linting for MD).

@kibertoad
Copy link
Member Author

@metcoder95 Thank you! Should I fix doc lint issues? They did not originate in this PR, but fairly easy to fix.

@metcoder95
Copy link
Member

Just for the linter check to go green

@kibertoad
Copy link
Member Author

@metcoder95 done!

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

@mcollina
Copy link
Member

@Eomm agreed. Should fastify in v6 ship without pino?

@kibertoad kibertoad merged commit 1b3381e into main Jan 13, 2026
37 checks passed
@kibertoad kibertoad deleted the feat/conditional-request-logging branch January 13, 2026 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants