Skip to content

Commit 80b3cf1

Browse files
committed
chore: Update package version to 1.0.5 and fastify dependency to 4.27.0
add: config.app.disableLogRequestHeaders option.
1 parent 6c147c4 commit 80b3cf1

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ By default, the server will add the CORS headers to the response. This behavior
8686

8787
By default, the server will log the request and response to console. This behavior can be disabled by setting the `fastify.disableRequestLogging` to `true`, or by setting the `fastify.logger`. All the settings under the `fastify` key will be passed to the `fastify` construct function.
8888

89-
### Log request body
89+
### Log request body and headers
9090

91-
By default, the server will log the request body. This behavior can be disabled by setting the `app.disableLogRequestBody` to `true`.
91+
By default, the server will log the request body and headers. These behaviors can be disabled by setting the `app.disableLogRequestBody` or `app.disableLogRequestHeaders` to `true`.
9292

9393
### Send request id header
9494

@@ -165,6 +165,7 @@ app:
165165
globalAppVariable: app
166166
disableCors: false
167167
disableLogRequestBody: false
168+
disableLogRequestHeaders: false
168169
disableSendRequestIdHeader: false
169170
disableApiErrorHandler: false
170171
internalServerErrorCode: 200

index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = (
1717
* @param {Object} config.app - Application configuration
1818
* @param {Boolean} config.app.disableCors - Whether to disable the cors plugin, default false
1919
* @param {Boolean} config.app.disableLogRequestBody - Whether to disable logging of request body, default false
20+
* @param {Boolean} config.app.disableLogRequestHeaders - Whether to disable logging of request headers, default false
2021
* @param {Boolean} config.app.disableSendRequestIdHeader - Whether to disable sending Request ID in response headers, default false
2122
* @param {Boolean} config.app.disableApiErrorHandler - Whether to disable the ApiErrorHandler, default false
2223
* @param {Boolean} config.app.disableLogApiError - Whether to disable logging of ApiError, default false
@@ -71,20 +72,27 @@ module.exports = (
7172
}
7273

7374
/************************************
74-
* Log request body
75+
* Log request body and headers
7576
************************************/
76-
if (!config.app.disableLogRequestBody) {
77+
if (!config.app.disableLogRequestBody || !config.app.disableLogRequestHeaders) {
7778
app.addHook('preHandler', (req, res, done) => {
78-
let clone = null;
79-
if (req.body && req.headers['content-length'] > 1000) {
80-
clone = JSON.parse(JSON.stringify(req.body));
81-
for (const key in clone) {
82-
if (clone[key] && clone[key].length > 100) {
83-
clone[key] = clone[key].slice(0, 100) + '...';
79+
const logContent = {url: req.url}
80+
if (!config.app.disableLogRequestBody) {
81+
let clone = null;
82+
if (req.body && req.headers['content-length'] > 1000) {
83+
clone = JSON.parse(JSON.stringify(req.body));
84+
for (const key in clone) {
85+
if (clone[key] && clone[key].length > 255) {
86+
clone[key] = clone[key].slice(0, 255) + '...';
87+
}
8488
}
8589
}
90+
logContent.body = clone || req.body;
91+
}
92+
if (!config.app.disableLogRequestHeaders) {
93+
logContent.headers = req.headers;
8694
}
87-
req.log.info({url: req.url, body: clone || req.body, headers: req.headers});
95+
req.log.info(logContent);
8896
done()
8997
})
9098
}

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fastify-app",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "A simple fastify app template for JSON API server with helpful routes and initial settings.",
55
"main": "index.js",
66
"scripts": {
@@ -14,7 +14,7 @@
1414
"author": "Zhiyi",
1515
"license": "ISC",
1616
"dependencies": {
17-
"fastify": "^4.26.2",
17+
"fastify": "^4.27.0",
1818
"@fastify/cors": "^9.0.1",
1919
"fast-glob": "^3.3.2"
2020
}

0 commit comments

Comments
 (0)