Closed czioutas closed 8 months ago
I updated the reproduce repo with a logSafe
function which essentially checks if context is undefined before sending it to super.
I assumed that this functionality would be handled from the core logic.
app-logger.service.ts
logSafe(message: any, context?: string) {
const modifiedMessage = `${message} test`;
console.log(modifiedMessage, context);
if (context === undefined) {
super.log(modifiedMessage);
} else {
super.log(modifiedMessage, context);
}
}
Since log()
method lets you pass multiple messages (separated by ,) to write to the stdout, second parameter won't necessarily represent a "context" (https://github.dev/nestjs/nest/blob/033f18161fb4d391aab0dac22f71209b97c94993/packages/common/services/console-logger.service.ts#L295) - that's why undefined
is logged down too. While this may not be the most straightforward, we can't really do anything on our side without introducing a breaking change
Is there an existing issue for this?
Current behavior
When extending the ConsoleLogger and then consuming the extended Logger class via injection we are seeing that the overridden methods log an extra line of just
undefined
.Minimum reproduction code
https://github.com/czioutas/plauground
Steps to reproduce
Expected behavior
When we are consuming the custom logger (AppLogger) via the AppController (or any service/controller) and executing the statement
this.logger.log('some message'
we expect only this message to be printed.Expected output
[Nest] 94728 - 02/15/2024, 11:45:36 AM LOG [AppController] some message
However we are seeing an extra line being logged as bellow
[Nest] 94728 - 02/15/2024, 11:45:36 AM LOG [AppController] some message
[Nest] 94728 - 02/15/2024, 11:45:36 AM LOG [AppController] undefined
<--Package
Other package
No response
NestJS version
^10.0.0
Packages versions
Node.js version
v18.19.0
In which operating systems have you tested?
Other
full output. We added a console.log for further debugging.
Other debugging attempts:
a) we tried to adapt the super.error function call to change the stack parameter as follows
super.error(modifiedMessage, { "hey": "you" }, context);
what we saw next is that it logged the message and then the object, so I cannot but I assume that undefined is coming from an undefined stack. Although I am not sure why it would print undefined.b) if we do not pass context in super.log and only message then it does not print the context but also no undefined.