Digvijay
Digvijay

Reputation: 3279

How to add Bunyan logger in NestJs for logging

I am trying to add Bunyan logging library in my NestJs application. I am using nestjs bunyan official npm package which is npm i nestjs-bunyan but it's showing error when I am trying to instantiate inside service class. I am using its documentation and taken reference from here https://www.npmjs.com/package/nestjs-bunyan

Below is my code:

app.module.ts

import { Module } from '@nestjs/common';
import { BunyanLoggerModule } from 'nestjs-bunyan';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [BunyanLoggerModule.forRoot({
        isGlobal: true,
        isEnableRequestLogger: true,
        bunyan: {
           name: 'MyApp',
         },
         }),],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { BunyanLoggerModule, ReqLogger } from 'nestjs-bunyan';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}
  @ReqLogger() private readonly logger: BunyanLoggerModule

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

app.service.ts

import { Injectable, Logger } from '@nestjs/common';
import { BunyanLoggerModule } from 'nestjs-bunyan';

@Injectable()
export class AppService {
  @Logger() private readonly logger: BunyanLoggerModule //Showing red line below @Logger()

  getHello(): string {
    return 'Hello World!';
  }
}

How can I resolve this?

Upvotes: 1

Views: 1277

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70450

From the npm page it looks like that BunyanLoggerModule type should just be Bunyan, presumably from the bunyan package. Otherwise, you could call it Logger from @nestjs/common

Upvotes: 1

Related Questions