I am trying to get the request object logged (for debugging purposes). My NestJS application uses express (the default option).
I obtain the request object both in my controller and in the service provider (after injection in the latter case).
Relevant part in the controller is:
// this is the Logger from @nestjs/common
private readonly logger = new Logger(MessagesController.name);
@Get()
findAll(@Req() request: Request): string
{
this.logger.log(request);
return this.messagesService.findAll();
}
Relevant part in the service provider is:
// the service is request-scoped since it's injected with a request-scoped provider
@Injectable({scope: Scope.REQUEST})
export class MessagesService {
constructor(@Inject(REQUEST) private request: Request) {
this.request = request;
}
findAll(): string {
this.logger.log(this.request);
return "all messages";
}
}
In both cases where logging occurs (i.e. both in the controller and the service), the object is logged on the console simply as [object Object]
which is not helpful at all.
If the calls to this.logger.log(request)
are replaced by mundane console.log(request)
the object is nicely printed on the console.
How can I get the Logger
from @nestjs/common
to nicely print these request objects?