nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

Bug using custom logger to replace singleton instance #3336

Closed amctavish closed 4 years ago

amctavish commented 4 years ago

Bug Report

Current behavior

When passing your own logger implementation via options.logger to NestFactory.create, calling app.useLogger on the resulting NestApplication instance or even Logger.overrideLogger it should replace the Logger singleton instance in @nestjs/common but it appears to still be using the default instance.

I thought it might have been juse because I'm extending Logger but the same is true when implementing the LoggerService interface.

Input Code

https://codesandbox.io/s/nest-typescript-starter-1izwc

import { NestFactory } from '@nestjs/core';

import { AppModule } from './app.module';
import { LoggerService } from './logger.service';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new LoggerService(),
  });
  // app.useLogger(new LoggerService())
  // Logger.overrideLogger(new LoggerService())

  await app.listen(3333);
}
bootstrap();
import { Logger } from '@nestjs/common';

export class LoggerService extends Logger {
  public debug(message: any, context?: string): void {
    super.debug(
      (message as string).concat(', via my custom logger'),
      'MyCustomLogger',
    );
  }
}
import { Controller, Get, Logger } from '@nestjs/common';
import { AppService } from './app.service';

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

  @Get()
  getHello(): string {
    const message = this.appService.getHello();
    Logger.debug(message);
    return message;
  }
}

Expected behavior

When either passing a custom logger implementation via options.logger to NestFactory.create, app.useLogger to the resulting NestApplication or Logger.overrideLogger the Logger singleton in @nestjs/common should use the custom logger instance instead of Nests default logger instance.

Environment

Nest version: 6.9.0

jmcdo29 commented 4 years ago

It looks like you are using Logger.debug() which is the static method instead of the instance method which would require you to instantiate the logger first and then call the expected method. You could do something like this:

const myLoggeer = new LoggerService();
myLogger.debug('my message');

And you should get the desired results.

amctavish commented 4 years ago

Hmm, I would have expected this to have set the static property Logger.instance and then calling Logger.debug to have called debug on the singleton instance I've set.

Having had a better look at the code I now see that Logger.printMessage doesn't actually touch the instance property. It looks like I've made an incorrect assumption.

Thanks for the respose.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.