【问题标题】:Don't see console.log() or logger messages while running nestjs and hitting any end point在运行nestjs并达到任何端点时看不到console.log()或记录器消息
【发布时间】:2022-02-22 13:23:05
【问题描述】:

在尝试访问任何端点时,我在 IDE 控制台中看不到任何状态或控制台消息。我确实看到返回响应,但控制台中没有调用详细信息。我很困惑为什么它会这样?

以下是我在运行npm run-script start:dev 时看到的 -

> nest start --watch
[3:04:44 PM] Starting compilation in watch mode...


[Nest] 14340   - 05/20/2020, 3:10:58 PM   [NestFactory] Starting Nest application...
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [InstanceLoader] TypeOrmModule dependencies initialized +220ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +195ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [InstanceLoader] TypeOrmModule dependencies initialized +6ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [InstanceLoader] AppModule dependencies initialized +5ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RoutesResolver] AppController {}: +13ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {, GET} route +4ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RoutesResolver] BatchController {/batch}: +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/batch, POST} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/batch, GET} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/batch/:batch, GET} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/batch/:batch, DELETE} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RoutesResolver] StudentController {/student}: +0ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/student, POST} route +2ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/student, GET} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/student/:id, GET} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:58 PM   [RouterExplorer] Mapped {/student/:id, DELETE} route +2ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RoutesResolver] AssignmentController {/assignment}: +1ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RouterExplorer] Mapped {/assignment, POST} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RouterExplorer] Mapped {/assignment, GET} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RouterExplorer] Mapped {/assignment/:id, GET} route +2ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RouterExplorer] Mapped {/assignment/:id, DELETE} route +1ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RoutesResolver] UploadController {/upload}: +1ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [RouterExplorer] Mapped {/upload, POST} route +2ms
[Nest] 14340   - 05/20/2020, 3:10:59 PM   [NestApplication] Nest application successfully started +3ms

当我点击http://localhost:3000/batch 时,我看到了响应,但上面的控制台没有显示类似API 类型GET 或状态200 等的任何内容。

这就是我在控制器中的 -

import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Logger,
} from '@nestjs/common';
import { BatchService } from './batch.service';
import { Batch } from './batch.entity';
import { InsertResult } from 'typeorm';

@Controller('batch')
export class BatchController {
  private readonly logger = new Logger(BatchController.name);

  constructor(private batchService: BatchService) {}
  @Post()
  create(@Body() batchDto: Batch): Promise<InsertResult> {
    this.logger.log(':: BatchController :: create()');
    console.log(':: BatchController :: create()');
    return this.batchService.create(batchDto);
  }

  @Get()
  findAll(): Promise<Batch[]> {
    this.logger.log(':: BatchController :: findAll()');
    console.log(':: BatchController :: findAll()');
    return this.batchService.findAll();
  }

  @Get(':batch')
  findOne(@Param('batch') batchName): Promise<Batch> {
    this.logger.log(':: BatchController :: findOne()');
    console.log(':: BatchController :: findOne()');
    return this.batchService.findOne(batchName);
  }

  @Delete(':batch')
  remove(@Param('batch') batchName) {
    this.logger.log(':: BatchController :: remove()');
    console.log(':: BatchController :: remove()');
    return this.batchService.remove(batchName);
  }
}

consolelogger 都放上,看看有没有用。

【问题讨论】:

  • 看起来有点奇怪。没有控制台日志什么的?你有这个可用的存储库吗?
  • 如果我无法弄清楚,也许我可以把它的一些子集和杰伊分享给你。感谢您的帮助。
  • 你发现了吗?我也有同样的问题

标签: node.js nestjs


【解决方案1】:

您的 NestJS 应用程序似乎已禁用日志。 启用 NestJS 请求日志记录的最佳且简单的方法是使用其内置的记录器服务。 为此,您可以在引导 NestJS 应用程序时传递一个 LogLevels 数组。

在您的 main.ts 或任何您正在引导 Nest 应用程序的地方,

const app = await NestFactory.create<NestExpressApplication>(AppModule, new ExpressAdapter(), {
    cors: true,
    logger: ['error', 'warn', 'log'] // <--- Add this line in options object
});

在这里,在options.logger 中传递LogLevels 的数组。

到目前为止,LogLevel 类型具有以下适用值:

declare type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose';

(代码 sn-p 取自 @nestjs/common

现在再次运行该项目,它应该会在控制台中打印请求日志。

【讨论】:

    【解决方案2】:

    几乎可以肯定有一些主机设置会阻止正常行为。我会联系我的托管服务提供商以了解它,但我是如何获得这些信息的:

    我的 NestJS 应用停止发送电子邮件。我从 NestJS 教程中实现了 Logger - 没有输出。

    我添加了 console.log/error - 没有输出

    我已经从主机中删除了我所有的应用程序文件,只留下了 1 个 app.js 文件,其中包含一个简单的代码 sn-p:

    const http = require("http");
    const port = 80;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader("Content-Type", "text/plain");
      res.end("Hello World");
    });
    
    server.listen(port, () => {
      console.log("I WILL BE LOGGED1?");
      process.stdout.write("I WILL BE LOGGED2?");
      process.stderr.write("I WILL BE LOGGED3?");
    });
    

    并注意到无论您做什么,都只会记录“process.stderr.write”。所以目前我只使用那段代码来记录信息。

    【讨论】:

      【解决方案3】:
      const app = await NestFactory.create(AppModule, {
          logger: console
      });
      

      上面的代码将启用 Nest 控制台输出。

      【讨论】:

      • 所有这一切都是对日志进行格式化。不允许添加您自己的日志
      猜你喜欢
      • 2022-11-27
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2020-07-29
      • 2020-05-21
      • 2021-10-22
      相关资源
      最近更新 更多