Skip to content

A custom logger for your NestJS application using Winston, logging class name, function name, time execution ... in a clean way

License

Notifications You must be signed in to change notification settings

wissamdeveloper/nestjs-logger

Repository files navigation

Custom Logger Middleware, Decorators, and Custom Winston Setup for Nest.js

Let's talk about the simplest and most important task developers have to deal with, Logging information.

This repository contains a custom logger middleware, decorators, and a sample Nest.js application demonstrating their usage. The middleware is designed to enhance the logging capabilities of your Nest.js application by providing execution time display and additional information like HTTP method, URL, and response status code. It leverages Nest.js decorators and middleware, along with a custom Winston logger setup.

Local Image

Custom Logger Decorator

custom-logger.decorator.ts

The custom-logger.decorator.ts file defines a custom logger decorator that can be used to log function calls and their context. It is integrated with the Winston logger for enhanced log formatting.

How to Use

To use the CustomLogger decorator, simply apply it to your functions or methods like this:

import { CustomLogger } from './logger/custom-logger.decorator';

@Controller()
export class AppController {
  @Get()
  @CustomLogger()
  getHello(): string {
    // Your code here
  }
}

ExecutionTimeMiddleware

The repository also includes an ExecutionTimeMiddleware class that can be used as a Nest.js middleware to log request execution times.

How to Use

To use the ExecutionTimeMiddleware, you can add it to your Nest.js application:

import { ExecutionTimeMiddleware } from './execution-time.middleware';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(ExecutionTimeMiddleware);
  await app.listen(3000);
}

Installation and Setup

Clone this repository:

git clone https://github.com/yourusername/your-repo.git

Install the required dependencies:

cd nestjs_logger
npm install

Start the Nest.js application:

npm run start

The application will be accessible at http://localhost:3000.

Contributing

If you find any issues or want to contribute to this project, please feel free to create a pull request or open an issue on GitHub.

If you find this project useful, please consider giving it a star (⭐) on GitHub. Your support is greatly appreciated!

License

This project is licensed under the MIT License.

About

A custom logger for your NestJS application using Winston, logging class name, function name, time execution ... in a clean way

Topics

Resources

License

Stars

Watchers

Forks