@mgcrea/fastify-request-logger
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -5,3 +5,4 @@ import type { FastifyPluginAsync } from 'fastify'; | ||
logBindings?: Record<string, unknown>; | ||
ignoredPaths?: Array<string>; | ||
}; | ||
export declare const plugin: FastifyPluginAsync<FastifyRequestLoggerOptions>; |
@@ -9,4 +9,7 @@ "use strict"; | ||
const plugin = async (fastify, options = {}) => { | ||
const { logBody = true, logBindings = { plugin: 'fastify-request-logger' } } = options; | ||
const { logBody = true, logBindings = { plugin: 'fastify-request-logger' }, ignoredPaths = [] } = options; | ||
fastify.addHook('onRequest', async (request) => { | ||
if (ignoredPaths.includes(request.routerPath)) { | ||
return; | ||
} | ||
const contentLength = request.headers['content-length']; | ||
@@ -17,2 +20,5 @@ 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` : ''}`); | ||
fastify.addHook('preHandler', async (request) => { | ||
if (ignoredPaths.includes(request.routerPath)) { | ||
return; | ||
} | ||
if (request.body && logBody) { | ||
@@ -23,2 +29,5 @@ request.log.debug({ ...logBindings, body: request.body }, `Request body`); | ||
fastify.addHook('onResponse', async (request, reply) => { | ||
if (ignoredPaths.includes(request.routerPath)) { | ||
return; | ||
} | ||
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`); | ||
@@ -25,0 +34,0 @@ }); |
{ | ||
"name": "@mgcrea/fastify-request-logger", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "Compact request logger plugin for fastify written in TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
8193
56