'Don't see console.log() or logger messages while running nestjs and hitting any end point

I don't see any status or console messages in the IDE console while trying to hit any end-point. I do see the response getting returned but no call details in the console. I am confused as why is it behaving this way?

Following is what I see on running 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

When I hit http://localhost:3000/batch, I see the response but the console above doesn't display anything like API type GET or status 200 etc.

This is what I have in controller -

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);
  }
}

Put the console and logger both as to see if anything works.



Solution 1:[1]

It seems like you have the logs disabled for your NestJS Application. The best and simple way to enable NestJS request logging is to use its built-in logger service. To do that, you can pass an array of LogLevels when bootstrapping a NestJS Application.

In your main.ts OR wherever you are bootstrapping the Nest app,

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

Here, pass an array of LogLevels in options.logger.

As of now, LogLevel type has these applicable values:

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

(code snippet taken from @nestjs/common)

Now run the project again and it should print request logs in console.

Solution 2:[2]

Almost sure that there are some host settings that prevent the normal behavior. I will contact my hosting provider to understand it, but how did i get to that information:

My NestJS app stopped to send emails. I implemented Logger from NestJS tutorial - No output.

I have added console.log/error - no output

I have removed all my app files from host and left only 1 app.js file with a simple code snippet:

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?");
});

And noticed that only "process.stderr.write" gets logged no matter what you do. So currently i'm using only that piece of code to log info.

Solution 3:[3]

const app = await NestFactory.create(AppModule, {
    logger: console
});

The code above will enable the Nest console output.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ayush Somani
Solution 2 Cheese
Solution 3 Tyler2P