Package Exports
- @samofprog/nestjs-http-logger
- @samofprog/nestjs-http-logger/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@samofprog/nestjs-http-logger) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
HttpLoggerMiddleware
HttpLoggerMiddleware is a configurable middleware for logging HTTP requests and responses in a NestJS application. It provides detailed information about incoming requests and completed responses, including the method, URL, status code, and processing duration.
Features
- Logs detailed information about incoming HTTP requests (method, URL).
- Logs detailed information about completed responses, including:
- HTTP status code.
- Processing duration in milliseconds.
- Supports custom log messages for incoming and completed requests.
- Allows the use of a custom logger (
LoggerService) or defaults to NestJS's global logger. - Differentiates between successful responses (logged as
log) and error responses (logged aserror) for better debugging. - Easy integration with both default and custom configurations.
Installation
Install the package using npm or yarn:
npm install @samofprog/nestjs-http-loggeror
yarn add @samofprog/nestjs-http-loggerUsage
The HttpLoggerMiddleware can be used directly with the app.use method in a NestJS application:
import {HttpLoggerMiddleware} from 'your-package-name';
import {NestFactory} from '@nestjs/core';
import {AppModule} from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(HttpLoggerMiddleware.create({
incomingRequestMessage: (method, url) => `Received: ${method} ${url}`,
completedRequestMessage: (method, url, statusCode, durationMs) =>
`Handled: ${method} ${url} - ${statusCode} (${durationMs} ms)`
}));
// or
app.use(HttpLoggerMiddleware.create());
await app.listen(3000);
}
bootstrap();Middleware Options
The middleware accepts an optional HttpLoggerOptions object to customize the log messages. If not provided, default messages will be used.
HttpLoggerOptions Interface
export interface HttpLoggerOptions {
logger?: LoggerService; // NestJS LoggerService
incomingRequestMessage?: (method: string, url: string) => string;
completedRequestMessage?: (
method: string,
url: string,
statusCode: number,
durationMs: string
) => string;
}incomingRequestMessage: A function that generates the log message for incoming requests. Receivesmethodandurlas parameters.completedRequestMessage: A function that generates the log message for completed requests. Receivesmethod,url,statusCode, anddurationMsas parameters.logger: An optional custom logger implementing theLoggerServiceinterface from NestJS. If not provided, a defaultLoggerwill be used.
Example
Default Usage
app.use(HttpLoggerMiddleware.create());This will log messages with the following formats:
- Incoming request:
Incoming Request: GET /example - Completed request:
Completed Request: GET /example - 200 (123.45 ms)
Custom Messages
app.use(HttpLoggerMiddleware.create({
incomingRequestMessage: (method, url) => `Received: ${method} ${url}`,
completedRequestMessage: (method, url, statusCode, durationMs) =>
`Handled: ${method} ${url} - ${statusCode} (${durationMs} ms)`
}));This will log messages with custom formats, e.g.,
- Incoming request:
>>> GET /example >>> - Completed request:
<<< GET /example <<< Status: 200, Time: 123.45ms
Custom Logger
If you want to use your own logger instead of the default NestJS logger, you can pass it via the logger option:
import { Logger } from '@nestjs/common';
const customLogger = new Logger('CustomLogger');
app.use(HttpLoggerMiddleware.create({ logger: customLogger }));License
This package is open-source and available under the MIT License.