gremo / nest-winston

A Nest module wrapper form winston logger
MIT License
638 stars 94 forks source link

Support for @algoan/nestjs-logging-interceptor? #268

Closed GrabbenD closed 3 years ago

GrabbenD commented 3 years ago

Hey there!

As it turns out none of the messages from @algoan/nestjs-logging-interceptor (source | npm) are being captured when using nest-winston. That's not the case if I switch to nestjs-pino. I also noticed that nestjs-pino claims that it supports Autobind request data to logs, is that maybe why?

Any help is highly appreciated!

gremo commented 3 years ago

Hi, nest-winston works out of the box with @algoan/nestjs-logging-interceptor without any special configuration. Just setup the library as explained in the section "Use as the main Nest logger". I've done a quick test for you and it works fine.

There is nothing special with nestjs-pino or nest-winston, both work with the interceptor if you replace the Nest logger with a custom logger. I still think that intercepting requests is something out of the scope of this library. You can easily use @algoan/nestjs-logging-interceptor or any library that provides that interceptor.

Here is a minimal example (src/app.module.ts):

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from '@algoan/nestjs-logging-interceptor';
import {
  utilities as nestWinstonModuleUtilities,
  WinstonModule,
} from 'nest-winston';
import * as winston from 'winston';

@Module({
  imports: [
    WinstonModule.forRoot({
      transports: [
        new winston.transports.Console({
          format: winston.format.combine(
            winston.format.timestamp(),
            nestWinstonModuleUtilities.format.nestLike(),
          ),
        }),
      ],
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor,
    },
  ],
})
export class AppModule {}

The key part is to use it as Nest logger (src/main.ts):

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
  await app.listen(3000);
}
bootstrap();

Logging