Nikita Fedyashev
Nikita Fedyashev

Reputation: 19048

NestJS in Express mode - log all incoming requests

FastifyAdapter logger is pretty helpful in logging all the incoming requests:

{"level":30,"time":1615660286373,"pid":4,"hostname":"6bc78f92-9bd6-4dfb-bd11-ae4c17a67f7c","reqId":19,"res":{"statusCode":304},"responseTime":6.005196988582611,"msg":"request completed"}

That's how I used it in Fastify mode/adapter:

const app = await NestFactory.create<NestFastifyApplication>(
   AppModule,
   new FastifyAdapter({logger: true})
)

Is there something similar in Express mode/adapter? Ideally without writing custom logging logic. It looks like a pretty common feature after all.

I checked ExpressAdapter source but it doesn't seem to have "log" being mentioned at all.

Upvotes: 0

Views: 4903

Answers (2)

Jay McDoniel
Jay McDoniel

Reputation: 70600

To add to Micael's answer, there's an Express middleware called morgan that can do this, and if you're open to the idea of a different logger there's my own called Ogma that has an interceptor for Nest that mimics this logic.

Upvotes: 1

Micael Levi
Micael Levi

Reputation: 6695

I don't think so. If you need this feature you should install the logger that fastify uses: pino, and pino-http to enable this yourself. Or just use nestjs-pino (which will do this for you)

Upvotes: 2

Related Questions