# nestjs-http-logger

> NestJS middleware for Logging HTTP requests and responses

Latest version **1.1.1** (published 2024-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-http-logger
pnpm add nestjs-http-logger
yarn add nestjs-http-logger
bun add nestjs-http-logger
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-01-17 |
| First published | 2024-01-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Vladyslav Braslavskyi |
| Maintainers | vlbras |
| Keywords | nestjs, express, middleware, logger, http |

## Links

- npm: https://www.npmjs.com/package/nestjs-http-logger
- Repository: https://github.com/vlbras/nestjs-http-logger
- Homepage: https://github.com/vlbras/nestjs-http-logger#readme
- Issues: https://github.com/vlbras/nestjs-http-logger/issues
- npm.io page: https://npm.io/package/nestjs-http-logger

## Dependencies (1)

- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.2.10

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2024-01-17
- 1.1.0 — 2024-01-14

## README

# NestJS HTTP Logger

[![npm](https://img.shields.io/npm/v/nestjs-http-logger.svg)](https://www.npmjs.com/package/nestjs-http-logger)
[![license](https://img.shields.io/github/license/vlbras/nestjs-http-logger.svg)](https://www.npmjs.com/package/nestjs-http-logger)

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](logs.png)

## 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:

```ts
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.

<b>Available Options:</b>

• ```params```: parameters from the route path<br>
• ```query```: key-value pairs in the URL after "?"<br>
• ```body```: content sent in POST/PUT requests<br>
• ```headers```: meta-info including authorization credentials<br>
• ```cookies```: data stored on the client for session tracking<br>
• ```ip```: client's IP address, identified by the server

Example of excluding ```headers``` from logging:

```ts
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.

```ts
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](https://github.com/vlbras)

## License

Licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---
_Source: https://npm.io/package/nestjs-http-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
