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
feat: Request Logging Customization #4955
base: main
Are you sure you want to change the base?
Changes from all commits
e0c7c7b
735cdab
05a46bd
defc572
0a304c4
d46f565
f466e0d
09e17ed
89bf726
29d621f
fb761f2
5c1021d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ declare namespace fastify { | |
requestIdLogLabel?: string; | ||
jsonShorthand?: boolean; | ||
genReqId?: (req: RawRequestDefaultExpression<RawServer>) => string, | ||
createRequestLogMessage?: (req: RawRequestDefaultExpression<RawServer>) => string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it receives the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok no problem, I will change it to FastifyRequest and have it return a string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it FastifyRequest or the RawRequest? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's how I set the prototype: createRequestLogMessage?: (ChildLoggerOptions: ChildLoggerOptions, FastifyRequest) => string Please let me know if you guys have any alternative suggestions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it should be |
||
trustProxy?: boolean | string | string[] | number | TrustProxyFunction, | ||
querystringParser?: (str: string) => { [key: string]: unknown }, | ||
/** | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -114,6 +114,7 @@ function fastify (options) { | |||||
const requestIdLogLabel = options.requestIdLogLabel || 'reqId' | ||||||
const bodyLimit = options.bodyLimit || defaultInitOptions.bodyLimit | ||||||
const disableRequestLogging = options.disableRequestLogging || false | ||||||
const createRequestLogMessage = options.createRequestLogMessage | ||||||
|
||||||
const ajvOptions = Object.assign({ | ||||||
customOptions: {}, | ||||||
|
@@ -729,9 +730,12 @@ function fastify (options) { | |||||
const reply = new Reply(res, request, childLogger) | ||||||
|
||||||
if (disableRequestLogging === false) { | ||||||
childLogger.info({ req: request }, 'incoming request') | ||||||
if (createRequestLogMessage) { | ||||||
childLogger.info(createRequestLogMessage(req)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the user here can set only the 1st parameter of I would think another way to handle it like:
so it is up to the user choose to log info or warning (i used to change the log level based on the request headers) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok perfect! I will make sure to modify the parameters so that createRequestLogMessage will accept childLogger as a parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, that's a good one. But then I'd like to know if the initial signature is the best for the use case you describe.
Suggested change
If something more complex is required, I believe the current documentation for |
||||||
} else { | ||||||
childLogger.info({ req: request }, 'incoming request') | ||||||
} | ||||||
Comment on lines
+733
to
+737
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of executing an if condition in the hot path execution of the request, we could just set a default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok sounds good. So instead of having: if (createRequestLogMessage) {
childLogger.info(createRequestLogMessage(req))
} else {
childLogger.info({ req: request }, 'incoming request')
} Should I just replace that code with: createRequestLogMessage(childLogger, req)? Please let me know if I am incorrect about this. Thank you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the goal is just to create the message, what about: Or we are trying to cover something else there? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kdrewes I was thinking on: childLogger.info.call(childLogger, createRequestLogMessage(req)) where the default Note the returned array and the @metcoder95 that usage misses the multiple pino format parameter (the array) |
||||||
} | ||||||
|
||||||
return frameworkErrors(new FST_ERR_BAD_URL(path), request, reply) | ||||||
} | ||||||
const body = `{"error":"Bad Request","code":"FST_ERR_BAD_URL","message":"'${path}' is not a valid url component","statusCode":400}` | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should describe its value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely my apologies for not including that earlier. Is there a particular area within docs/Reference/Server.md where I should include it or does anywhere suffice? Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
search for the docs of disableRequestLogging and put them next to it?