1.1.1 • Published 4 months ago

nestjs-http-logger v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

NestJS HTTP Logger

npm license

A NestJS middleware, designed to facilitate logging of HTTP requests and responses, providing an efficient and straightforward way in debugging and monitoring network activity.

Logs

Installation

npm install nestjs-http-logger

Usage

After installation, the LoggerMiddleware can be imported and applied in the AppModule or any other module. It can be configured to log all routes ('*') or specific ones, offering flexibility based on the developer's requirements.

Example configuration in AppModule:

import { LoggerMiddleware } from 'nestjs-http-logger'';

@Module()
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('*');
  }
}

excludeFromRequest

excludeFromRequest method allows you to specify which parts of the HTTP request should not be logged.

Available Options:

params: parameters from the route pathquery: key-value pairs in the URL after "?"body: content sent in POST/PUT requestsheaders: meta-info including authorization credentialscookies: data stored on the client for session trackingip: client's IP address, identified by the server

Example of excluding headers from logging:

import { LoggerMiddleware } from 'nestjs-http-logger'';

@Module()
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    // Call 'excludeFromRequest' before 'consumer.apply'
    LoggerMiddleware.excludeFromRequest({ headers: true });
    consumer.apply(LoggerMiddleware).forRoutes('*');
  }
}

Recommendation

In a production environment, consider excluding sensitive parts of the request such as the body, headers, and cookies from logging, as they may contain personal information, passwords, authentication tokens, session cookies, and more.

import { LoggerMiddleware } from 'nestjs-http-logger'';

@Module()
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    const isProduction = process.env.NODE_ENV === 'production';
    LoggerMiddleware.excludeFromRequest({
      body: isProduction,
      headers: isProduction,
      cookies: isProduction,
    });
    consumer.apply(LoggerMiddleware).forRoutes('*');
  }
}

Author

Vladyslav Braslavskyi GitHub

License

Licensed under the MIT License - see the LICENSE file for details.

1.1.1

4 months ago

1.1.0

4 months ago

1.0.2

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago