https://github.com/iamnnort/nestjs-logger
Logger module for NestJS - Simple - Informative - Pretty
https://github.com/iamnnort/nestjs-logger
Last synced: 4 months ago
JSON representation
Logger module for NestJS - Simple - Informative - Pretty
- Host: GitHub
- URL: https://github.com/iamnnort/nestjs-logger
- Owner: iamnnort
- License: mit
- Created: 2024-02-15T19:14:54.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-12T07:45:14.000Z (about 1 year ago)
- Last Synced: 2025-11-23T07:29:44.822Z (7 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@iamnnort/nestjs-logger
- Size: 305 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# @iamnnort/nestjs-logger
Logger module for NestJS based on [nestjs-pino](https://github.com/iamolegga/nestjs-pino) — structured request logging, clean output, CloudWatch-friendly.
## Features
- Automatic HTTP request/response logging via pino-http
- Clean single-line request logs: `INFO: [Http] GET /v1/visits 200 (3ms)`
- Context-aware logging: `INFO: [AppController] User logged in`
- Global exception filter with proper error responses
- No colors, no timestamps, no PID — optimized for AWS CloudWatch
- Configurable log level
## Installation
```bash
npm install @iamnnort/nestjs-logger
# or
yarn add @iamnnort/nestjs-logger
```
## Usage
**app.module.ts**
```ts
import { Module } from '@nestjs/common';
import { LoggerModule } from '@iamnnort/nestjs-logger';
@Module({
imports: [LoggerModule.forRoot()],
})
export class AppModule {}
```
**main.ts**
```ts
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import { LoggerService } from '@iamnnort/nestjs-logger';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
bufferLogs: true,
});
const loggerService = await app.resolve(LoggerService);
app.useLogger(loggerService);
await app.listen(3000);
}
bootstrap();
```
### Log level
Set the minimum level with `LoggerModule.forRoot({ level: 'debug' })`. Levels (most to least verbose): `trace`, `debug`, `info`, `warn`, `error`, `fatal`. Default is `info`.
```ts
@Module({
imports: [LoggerModule.forRoot({ level: 'debug' })],
})
export class AppModule {}
```
### Using the logger in your code
Inject `LoggerService` and set the context to get `[ClassName]` prefix in all log messages:
```ts
import { Controller, Get, Post } from '@nestjs/common';
import { LoggerService } from '@iamnnort/nestjs-logger';
@Controller()
export class AppController {
constructor(private readonly loggerService: LoggerService) {
this.loggerService.setContext(AppController.name);
}
@Get()
get() {
this.loggerService.log('Handling GET request');
return { success: true };
}
@Post()
post() {
this.loggerService.error('Something failed');
return { success: false };
}
}
```
### Global exception filter
The module registers a global exception filter automatically. It returns proper error responses for both HTTP exceptions and unhandled errors:
```json
// BadRequestException('Example error')
{ "message": "Example error", "error": "Bad Request", "statusCode": 400 }
// throw new Error('Something went wrong.')
{ "message": "Something went wrong.", "error": "Internal Server Error", "statusCode": 500 }
```
## Output
```
INFO: [NestFactory] Application is starting...
INFO: [NestApplication] Application started.
INFO: [Http] GET / 200 (3ms)
INFO: [Http] POST / 200 (1ms)
INFO: [Http] POST /http-error 400 (2ms)
ERROR: [AppController] User error.
```
## Example
An example app lives in [`example/`](example/). To run it:
```bash
yarn start
```
## License
MIT © [Nikita Pavets](https://github.com/iamnnort)