nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

Default Logger doesn't handle context argument properly #9312

Closed dg-eparizzi closed 2 years ago

dg-eparizzi commented 2 years ago

Is there an existing issue for this?

Current behavior

The default LoggerService implementation provides the following method signature, apparently to override the context:

log(message: any, context?: string): void;

But when calling something like this.logger.log("Hello World!", "AppService:getHello") this results in 2 different log lines:

[Nest] 8  - 03/09/2022, 12:06:13 PM     LOG [AppService] Hello World!
[Nest] 8  - 03/09/2022, 12:06:13 PM     LOG [AppService] AppService:getHello

Minimum reproduction code

https://stackblitz.com/edit/nestjs-starter-uecpmz?file=src/app.service.ts

Steps to reproduce

No response

Expected behavior

It's my understanding that the context argument would allow one to override the context for this particular log.

So calling this.logger.log("Hello World!", "AppService:getHello") should output a single log:

[Nest] 8  - 03/09/2022, 12:06:13 PM     LOG [AppService:getHello] Hello World!

Package

Other package

No response

NestJS version

8.4.0

Packages versions

[System Information]
OS Version     : Linux
NodeJS Version : v14.19.0
NPM Version    : 7.17.0

[Nest CLI]
Nest CLI Version : 8.2.2 

[Nest Platform Information]
platform-express version : 8.4.0
schematics version       : 8.0.8
testing version          : 8.4.0
common version           : 8.4.0
core version             : 8.4.0
cli version              : 8.2.2

Node.js version

14.19.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 2 years ago

context is ignored if you've already specified the context in the logger constructor (new Logger(AppService.name) - it will work as expected once you remove the AppService.name part).

This is the expected behavior.