Skip to content

Commit

Permalink
chore: Update package version to 1.0.5 and fastify dependency to 4.27.0
Browse files Browse the repository at this point in the history
add: config.app.disableLogRequestHeaders option.
  • Loading branch information
zhiyi7 committed May 17, 2024
1 parent 6c147c4 commit 80b3cf1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ By default, the server will add the CORS headers to the response. This behavior

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.

### Log request body
### Log request body and headers

By default, the server will log the request body. This behavior can be disabled by setting the `app.disableLogRequestBody` to `true`.
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`.

### Send request id header

Expand Down Expand Up @@ -165,6 +165,7 @@ app:
globalAppVariable: app
disableCors: false
disableLogRequestBody: false
disableLogRequestHeaders: false
disableSendRequestIdHeader: false
disableApiErrorHandler: false
internalServerErrorCode: 200
Expand Down
26 changes: 17 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = (
* @param {Object} config.app - Application configuration
* @param {Boolean} config.app.disableCors - Whether to disable the cors plugin, default false
* @param {Boolean} config.app.disableLogRequestBody - Whether to disable logging of request body, default false
* @param {Boolean} config.app.disableLogRequestHeaders - Whether to disable logging of request headers, default false
* @param {Boolean} config.app.disableSendRequestIdHeader - Whether to disable sending Request ID in response headers, default false
* @param {Boolean} config.app.disableApiErrorHandler - Whether to disable the ApiErrorHandler, default false
* @param {Boolean} config.app.disableLogApiError - Whether to disable logging of ApiError, default false
Expand Down Expand Up @@ -71,20 +72,27 @@ module.exports = (
}

/************************************
* Log request body
* Log request body and headers
************************************/
if (!config.app.disableLogRequestBody) {
if (!config.app.disableLogRequestBody || !config.app.disableLogRequestHeaders) {
app.addHook('preHandler', (req, res, done) => {
let clone = null;
if (req.body && req.headers['content-length'] > 1000) {
clone = JSON.parse(JSON.stringify(req.body));
for (const key in clone) {
if (clone[key] && clone[key].length > 100) {
clone[key] = clone[key].slice(0, 100) + '...';
const logContent = {url: req.url}
if (!config.app.disableLogRequestBody) {
let clone = null;
if (req.body && req.headers['content-length'] > 1000) {
clone = JSON.parse(JSON.stringify(req.body));
for (const key in clone) {
if (clone[key] && clone[key].length > 255) {
clone[key] = clone[key].slice(0, 255) + '...';
}
}
}
logContent.body = clone || req.body;
}
if (!config.app.disableLogRequestHeaders) {
logContent.headers = req.headers;
}
req.log.info({url: req.url, body: clone || req.body, headers: req.headers});
req.log.info(logContent);
done()
})
}
Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-app",
"version": "1.0.4",
"version": "1.0.5",
"description": "A simple fastify app template for JSON API server with helpful routes and initial settings.",
"main": "index.js",
"scripts": {
Expand All @@ -14,7 +14,7 @@
"author": "Zhiyi",
"license": "ISC",
"dependencies": {
"fastify": "^4.26.2",
"fastify": "^4.27.0",
"@fastify/cors": "^9.0.1",
"fast-glob": "^3.3.2"
}
Expand Down

0 comments on commit 80b3cf1

Please sign in to comment.