nestjs-logging-interceptor
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Nest Logo

A Nest interceptor to log the incoming/outgoing requests.

NestJS Logging interceptor

(copied from nestjs-components)

A simple NestJS interceptor intercepting request details and logging it using the built-in Logger class. It will use the default Logger implementation unless you pass your own to your Nest application.

Installation

npm install --save nestjs-logging-interceptor

Usage

Default usage

Use the interceptor as a global interceptor (refer to the last paragraph of this section for more details).

Example:

import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from 'nestjs-logging-interceptor';

@Module({
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useClass: LoggingInterceptor,
        },
    ],
})
export class AppModule {}

Factory

You can also manually pass an interceptor instance through a factory function. This will allow you to set a custom scope for your interceptor to use when logging.

Example:

@Module({
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useValue: new LoggingInterceptor('CUSTOM CONTEXT'),
        },
    ],
})
export class AppModule {}

Custom Handlers

When constructing the logging interceptor yourself using a factory it is possible to override the handlers with your own logging handlers:

Example:

@Module({
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useValue: new LoggingInterceptor({
                requestHandler: (request, logger) => logger.log(`URL Requested: ${request.url}`),
                responseHandler: (request, response, _body, logger) =>
                    logger.log(`URL Response: ${request.url} status code: ${response.status}`),
                errorHandler: (request, error, logger) => logger.error(`Request Error: ${request.url}`, error),
            }),
        },
    ],
})
export class AppModule {}

Multiple Interceptors

Lastly it is possible to configure a different logger with a different context for each handler:

@Module({
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useValue: new LoggingInterceptor({
                context: 'REQUEST LOGGER',
                requestHandler: (request, logger) => logger.log(`URL Requested: ${request.url}`),
                responseHandler: null,
                errorHandler: null,
            }),
        },
        {
            provide: APP_INTERCEPTOR,
            useValue: new LoggingInterceptor({
                context: 'RESPONSE LOGGER',
                requestHandler: null,
                responseHandler: (request, response, _body, logger) =>
                    logger.log(`URL Response: ${request.url} status code: ${response.status}`),
                errorHandler: null,
            }),
        },
        {
            provide: APP_INTERCEPTOR,
            useValue: new LoggingInterceptor({
                context: 'ERROR LOGGER',
                requestHandler: null,
                responseHandler: null,
                errorHandler: (request, error, logger) => logger.error(`Request Error: ${request.url}`, error),
            }),
        },
    ],
})
export class AppModule {}

Package Sidebar

Install

npm i nestjs-logging-interceptor

Weekly Downloads

6

Version

0.1.2

License

ISC

Unpacked Size

15.5 kB

Total Files

8

Last publish

Collaborators

  • roaders