notlogs rocket
notlogs

Frameworks

NestJS Integration Guide

Integrate structured NotLogs logging with NestJS dependency injection and exception filters.

1. Install Package

npm install @notlogs/nest

2. Register NotLogsModule in AppModule

src/app.module.ts
typescript
import { Module } from '@nestjs/common';
import { NotLogsModule } from '@notlogs/nest';

@Module({
  imports: [
    NotLogsModule.forRoot({
      apikey: process.env.NOTLOGS_API_KEY!,
      appName: 'order-api',
      environment: process.env.NODE_ENV || 'production',
      isGlobal: true,
    }),
  ],
})
export class AppModule {}

3. Injecting NotLogsService into Services

src/orders/orders.service.ts
typescript
import { Injectable } from '@nestjs/common';
import { NotLogsService } from '@notlogs/nest';

@Injectable()
export class OrdersService {
  constructor(private readonly logger: NotLogsService) {}

  async createOrder(orderDto: any) {
    this.logger.log('Order creation started', 'OrdersService');

    try {
      // business logic...
      return { success: true };
    } catch (err: any) {
      this.logger.error(`Order failed: ${err.message}`, err.stack, 'OrdersService');
      throw err;
    }
  }
}

4. Auto-Tracing HTTP Requests (Global Interceptor)

Attach NotLogsInterceptor in your main.ts to automatically trace request duration, status codes, and unhandled exceptions across all controllers:

src/main.ts
typescript
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NotLogsInterceptor, NotLogsService } from '@notlogs/nest';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Set as system logger
  app.useLogger(app.get(NotLogsService));

  // Trace all HTTP requests with latency and status codes
  app.useGlobalInterceptors(app.get(NotLogsInterceptor));

  await app.listen(3000);
}
bootstrap();