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
67.7k stars 7.63k forks source link

bufferLogs and autoFlushLogs not working when use NestFactory.create #9880

Closed phoenix741 closed 2 years ago

phoenix741 commented 2 years ago

Is there an existing issue for this?

Current behavior

Hi,

In version 8.4.4 of nestjs log aren't automatically flushed when working in a serverless context.

The code that active autoFlushLogs is only called on the method createApplicationContext

        if (this.autoFlushLogs) {
            context.flushLogsOnOverride();
        }

The method createMicroservice and create don't call this methods, so the logs are never flushed even if we call with autoFlushLogs: true

When calling flushLogs manually, it's working :

  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useLogger(app.get(MyLogger));
  app.flushLogs();

  await  app.init()

  const expressApp = app.getHttpAdapter().getInstance();
  return serverlessExpress({ app: expressApp });

Thanks

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-vcexx9?file=tsconfig.json

Steps to reproduce

  1. set bufferLogs = false
  2. log start in nest format and next with logger
  3. set bufferLogs = true
  4. we don't have log

Expected behavior

The autoflushLogs should work in all context

Package

Other package

No response

NestJS version

8.4.4

Packages versions

 "@nestjs/common": "^8.4.4",
    "@nestjs/core": "^8.4.4",

Node.js version

16

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 2 years ago

If you use a non-standard approach (serverless express) that doesn't call the listen() method, you have to manually flush logs so this is the expected behavior.