fix(common): correct order for logger error parameters #10531
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The error() function of a logger has two optional parameters: the trace and the context. The logger now no longer passes the global context as the first optional parameter when there are is no value supplied for the trace parameter. Instead, the trace parameter is passed as undefined and the context is passed as the second optional parameter.
This fixes a bug where the trace is overwritten by the context when passed to custom loggers. Fixes gremo/nest-winston#473
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When implementing a custom logger, instantiating the Nest
Logger
with a context and then callinglogger.error(message)
, the Nest logger will call the underlying logger's function withcustomLogger.error(message, context)
.However, the interface for the
error
message is defined as follows:error(message: any, stack?: string, context?: string): void;
This clearly shows that the first parameter after the message is the stack and the second parameter is the context. Consequently, Nest passing the context as the second parameter (i.e. first optional parameter) is incorrect.An example of this behavior causing problems for custom logger implementations can be found in gremo/nest-winston#473
Issue Number: N/A
What is the new behavior?
In the case mentioned above with a custom logger and a Nest logger with a global context, calling
logger.error(message)
in the code will now make Nest correctly call the underlying logger withcustomLogger.error(message, undefined, context)
. Note how the first optional parameter (the trace) is now passed asundefined
and the second optional parameter contains the context.Does this PR introduce a breaking change?
If users correctly used the
error()
function of theLogger
, i.e. they used the first optional parameter for the trace and the second optional parameter for the context, there is no need to change anything.If users copied the behavior of Nest of setting the context to the first optional parameter of the
error()
function (e.g. callinglogger.error(message, context)
), then the code needs to be changed to correctly pass the context as the second optional parameter:logger.error(message, undefined, context)
Other information