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.
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.
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
}
}
The repository also includes an ExecutionTimeMiddleware
class that can be used as a Nest.js middleware to log request execution times.
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);
}
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.
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!
This project is licensed under the MIT License.