Careeroza
Logging
Structured JSON Logs
Plain text logs are hard to search in production. Log JSON with a request id, so Grafana Loki, Google Cloud Logging or ELK can filter by user, route or error. NestJS 11 has a built-in JSON mode; many teams use nestjs-pino.
// NestJS 11 built-in
import { ConsoleLogger } from '@nestjs/common';
const app = await NestFactory.create(AppModule, {
logger: new ConsoleLogger({ json: true, colors: false }),
});
// nestjs-pino (app.module.ts)
import { LoggerModule } from 'nestjs-pino';
LoggerModule.forRoot({
pinoHttp: {
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
redact: ['req.headers.authorization', 'req.body.password'], // never log secrets
genReqId: (req) => (req.headers['x-request-id'] as string) ?? crypto.randomUUID(),
},
});Example
curl -s localhost:3000/products/7 > /dev/nullExample output
{"level":30,"time":1768471200000,"pid":1,"req":{"id":"0b9a5c1e","method":"GET","url":"/products/7"},"res":{"statusCode":200},"responseTime":4,"msg":"request completed"}