简体   繁体   中英

How to get a classname along with logs in NestJS using Winston logger

I am using NestJS as a backend service in which I want to add some logger for better logging mechanism.Though NestJs has a default logger for logging but I am using Winston in my application. I want functionality so I can get a class name also in logs to which log belongs to.

Using NestJs default logger I can achieve this using below code in particular file

private readonly logger = new Logger(AppService.name);

By using above code I am able to get class name along with logs which is AppService .

But I am using nest-winston package in my application. How can I get the same thing with it?

Below is my code:

import { Module } from '@nestjs/common';
import { WinstonModule,utilities as nestWinstonModuleUtilities } from 'nest-winston';
import * as winston from 'winston';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import * as path from 'path';

@Module({
imports: [ WinstonModule.forRoot({
          format: winston.format.combine(
                   winston.format.timestamp(),
                   winston.format.json(),
                   nestWinstonModuleUtilities.format.nestLike('MyApp', { prettyPrint: true }),
                  ),
          transports: [
                    new winston.transports.Console(),
                    new winston.transports.File({
                      dirname: path.join(__dirname, '../log/info/'), //path to where save logging result 
                      filename: 'info.txt', //name of file where will be saved logging result
                      level: 'info',
                    }),
                  ],        
       }),],
 controllers: [AppController],
 providers: [AppService],
})
export class AppModule {}

What thing do I need to change?

The Logger class in nest-winston behaves differently than the pre-built Nest logger. You have to pass the service name on every call of either the log(), debug(), error() and verbose() methods.

Based on the documentation this is how you can use the winston logger service after you replace it with the nest Logger.

import { Controller, Get, Logger } from '@nestjs/common';
import { AppService } from './app.service';

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

  @Get()
  getHello(): string {
    this.logger.log('Calling getHello()', AppController.name);
    this.logger.debug('Calling getHello()', AppController.name);
    this.logger.verbose('Calling getHello()', AppController.name);
    this.logger.warn('Calling getHello()', AppController.name);

    try {
      throw new Error()
    } catch (e) {
      this.logger.error('Calling getHello()', e.stack, AppController.name);
    }

    return this.appService.getHello();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM