@mgcrea/fastify-request-logger
Advanced tools
Comparing version 0.2.1 to 0.2.3
import type { FastifyPluginAsync } from 'fastify'; | ||
export declare type FastifyRequestLoggerOptions = { | ||
logBody: boolean; | ||
logBindings: Record<string, unknown>; | ||
logBody?: boolean; | ||
logBindings?: Record<string, unknown>; | ||
}; | ||
export declare const plugin: FastifyPluginAsync<FastifyRequestLoggerOptions>; |
@@ -8,3 +8,3 @@ "use strict"; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const plugin = async (fastify, options) => { | ||
const plugin = async (fastify, options = {}) => { | ||
const { logBody = true, logBindings = { plugin: 'fastify-request-logger' } } = options; | ||
@@ -14,13 +14,13 @@ fastify.addHook('onRequest', async (request) => { | ||
request.log.info(logBindings, `${chalk_1.default.bold.yellow('←')}${chalk_1.default.yellow(request.method)}:${chalk_1.default.green(request.url)} request from ip ${chalk_1.default.blue(request.ip)}${contentLength ? ` with a ${chalk_1.default.yellow(contentLength)}-length body` : ''}`); | ||
request.log.trace({ request }, `Request trace`); | ||
request.log.trace({ ...logBindings, req: request }, `Request trace`); | ||
}); | ||
fastify.addHook('preHandler', async (request) => { | ||
if (request.body && logBody) { | ||
request.log.debug({ ...logBindings, body: request.body }, `Request body`); | ||
} | ||
}); | ||
fastify.addHook('onResponse', async (request, reply) => { | ||
request.log.info(logBindings, `${chalk_1.default.bold.yellow('→')}${chalk_1.default.yellow(request.method)}:${chalk_1.default.green(request.url)} response with a ${chalk_1.default.magenta(reply.statusCode)}-status`); | ||
}); | ||
fastify.addHook('preHandler', async (req) => { | ||
if (req.body && logBody) { | ||
req.log.debug(logBindings, `Request body: ${chalk_1.default.gray(JSON.stringify(req.body))}`); | ||
} | ||
}); | ||
}; | ||
exports.plugin = plugin; |
{ | ||
"name": "@mgcrea/fastify-request-logger", | ||
"version": "0.2.1", | ||
"version": "0.2.3", | ||
"description": "Compact request logger plugin for fastify written in TypeScript", | ||
@@ -26,6 +26,6 @@ "main": "lib/index.js", | ||
"@tsconfig/node10": "^1.0.7", | ||
"@types/ioredis": "^4.19.2", | ||
"@types/ioredis": "^4.19.3", | ||
"@types/node": "^14.14.22", | ||
"@typescript-eslint/eslint-plugin": "^4.14.0", | ||
"@typescript-eslint/parser": "^4.14.0", | ||
"@typescript-eslint/eslint-plugin": "^4.14.1", | ||
"@typescript-eslint/parser": "^4.14.1", | ||
"eslint": "^7.18.0", | ||
@@ -35,3 +35,3 @@ "eslint-config-prettier": "^7.2.0", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"fastify": "^3.10.1", | ||
"fastify": "^3.11.0", | ||
"jest": "^26.6.3", | ||
@@ -38,0 +38,0 @@ "pino-pretty": "^4.3.0", |
@@ -18,2 +18,8 @@ # FastifyRequestLogger | ||
## Preview | ||
<p align="left"> | ||
<img src="https://raw.githubusercontent.com/mgcrea/fastify-request-logger/master/docs/preview.png" alt="Preview" /> | ||
</p> | ||
## Usage | ||
@@ -20,0 +26,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7879
76