Nest: Setting up console logger for HTTP requests

Created on 26 Jul 2018  路  6Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I start nestjs sever with yarn start command, which is alias to nodemon, nodemon.json has the following exec property: ts-node -r tsconfig-paths/register src/main.ts

Server starts and writes some logs on start:

[nodemon] 1.17.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /app/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
[Nest] 107   - 2018-7-26 12:27:15   [NestFactory] Starting Nest application...
[Nest] 107   - 2018-7-26 12:27:15   [InstanceLoader] AppModule dependencies initialized +89ms
[Nest] 107   - 2018-7-26 12:27:15   [InstanceLoader] HttpModule dependencies initialized +0ms

But when I send requests to endpoints the logs keep silence.

Expected behavior

I would expect to see some information in logs regarding request:

  • HTTP PATH
  • HTTP METHOD
  • Response Status

And similar information when I hit unexisting endpoint.

Minimal reproduction of the problem with instructions

The issue can be reproduced with the example from official documentation: https://docs.nestjs.com/first-steps

What is the motivation / use case for changing the behavior?

My goal is to simplify debugging in development mode and just see some information, which requests hit the service and what service sends back (at least HTTP status)

I assume, that this (no logs behaviour) could be done by design, but I would be very useful if someone could provide working examples with the log setup that I described.

IMHO: it would be good to have such examples in tutorial.

Environment


Nest version: 5.0..0


For Tooling issues:
- Node version: 8.11.3 
- Platform:  node:8.11.3-jessie docker image  (Linux)

Others:
OS: node:8.11.3-jessie docker image

Notes

I read Logger tutorial page and tried to inject my custom logger, but I still can not see request logs.

I do not have a strong Angular background, so it could be that I missing something very obvious.

Thanks in advance for your support.

Most helpful comment

@greyblake Is there has a builtin LoggerMiddleware?

All 6 comments

I've managed to implement middleware, described in this article: https://docs.nestjs.com/middleware

And if I applyied to / like this:

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware)
      .forRoutes('/');
  }
}

It's working for all requests.
However logging res.body is quite challenging. Sometimes the middleware gets it, sometimes no.

To get response body I use hacky solution provided in this StackOverflow response

@greyblake Is there has a builtin LoggerMiddleware?

@xdamman No, you need to implement it yourself.
You can copy/paste this one from the tutorial: https://docs.nestjs.com/middleware

I guess we can close the issue then

same problem.
we can log the start of request, but log the end of response needs some trick.
In koa, we can just log the start and end event like this:

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

it seems we cannot achieve something like this in nestjs, since the next() in middleware is NOT async.

@sophister you can use an interceptor for this, see example https://docs.nestjs.com/interceptors#aspect-interception

Was this page helpful?
0 / 5 - 0 ratings

Related issues

janckerchen picture janckerchen  路  3Comments

tronginc picture tronginc  路  3Comments

menme95 picture menme95  路  3Comments

hackboy picture hackboy  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments