diff --git a/docs/Reference/Server.md b/docs/Reference/Server.md index bbc5092834..9dae268d51 100644 --- a/docs/Reference/Server.md +++ b/docs/Reference/Server.md @@ -25,6 +25,7 @@ describes the properties available in that options object. - [`onConstructorPoisoning`](#onconstructorpoisoning) - [`logger`](#logger) - [`disableRequestLogging`](#disablerequestlogging) + - [`createRequestLogMessage`](#createRequestLogMessage) - [`serverFactory`](#serverfactory) - [`jsonShorthand`](#jsonshorthand) - [`caseSensitive`](#casesensitive) @@ -1911,6 +1912,7 @@ The properties that can currently be exposed are: explicitly passed) - ignoreTrailingSlash - disableRequestLogging +- createRequestLogMessage - maxParamLength - onProtoPoisoning - onConstructorPoisoning diff --git a/fastify.d.ts b/fastify.d.ts index 65c36d78cf..433fb1bc6a 100644 --- a/fastify.d.ts +++ b/fastify.d.ts @@ -111,6 +111,7 @@ declare namespace fastify { requestIdLogLabel?: string; jsonShorthand?: boolean; genReqId?: (req: RawRequestDefaultExpression) => string, + createRequestLogMessage?: (req: RawRequestDefaultExpression) => string, trustProxy?: boolean | string | string[] | number | TrustProxyFunction, querystringParser?: (str: string) => { [key: string]: unknown }, /** diff --git a/fastify.js b/fastify.js index 4efb4c2816..a95890a9b9 100644 --- a/fastify.js +++ b/fastify.js @@ -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)) + } else { + childLogger.info({ req: request }, 'incoming request') + } } - 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}`