Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): correct order for logger error parameters #10531

Merged
merged 1 commit into from Feb 1, 2023
Merged

fix(common): correct order for logger error parameters #10531

merged 1 commit into from Feb 1, 2023

Conversation

ChrisiPK
Copy link
Contributor

@ChrisiPK ChrisiPK commented Nov 7, 2022

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?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

When implementing a custom logger, instantiating the Nest Logger with a context and then calling logger.error(message), the Nest logger will call the underlying logger's function with customLogger.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 with customLogger.error(message, undefined, context). Note how the first optional parameter (the trace) is now passed as undefined and the second optional parameter contains the context.

Does this PR introduce a breaking change?

  • Yes
  • No

If users correctly used the error() function of the Logger, 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. calling logger.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

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
@coveralls
Copy link

Pull Request Test Coverage Report for Build b3bcc5d3-3522-4c1d-abfb-ace9db0256e5

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.776%

Totals Coverage Status
Change from base Build db07b797-b226-4c6c-9b82-22af4fb43cb5: 0.0%
Covered Lines: 6147
Relevant Lines: 6555

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

LGTM

@kamilmysliwiec kamilmysliwiec merged commit 38c3652 into nestjs:master Feb 1, 2023
@ChrisiPK ChrisiPK deleted the patch-logger-error-call branch February 2, 2023 20:41
jmcdo29 added a commit to jmcdo29/nest that referenced this pull request Feb 8, 2023
Due to a change in nestjs#10531 when the `stack` is added as `undefined` a
new `undefined` log would be added whenever `this.logger.error()`
would be called. Now, we check that the last element is either
a string or undefined, and if so strip the last element. That element
should only exist when we artificially add the stack via the changes
from nestjs#10531.

closes nestjs#11074
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Context is lost when logging errors
3 participants