Skip to content

fix: logger print invalid context when no stack trace provided#16307

Merged
kamilmysliwiec merged 2 commits intonestjs:masterfrom
Recursyve:fix/console-logger-error-context
Feb 5, 2026
Merged

fix: logger print invalid context when no stack trace provided#16307
kamilmysliwiec merged 2 commits intonestjs:masterfrom
Recursyve:fix/console-logger-error-context

Conversation

@JulienDuf
Copy link
Contributor

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 using the ConsoleLogger.error with two parameters, the logger will assumed that the second parameter is an Error or override of the context.

If the second parameter not a string or an Error, it will use the second parameter as the context instead of using the default context of the Logger.

Ex:

function bootsrap() {
  const logger = new ConsoleLogger('MyCustomLogger');
  logger.error('Unexpected error', { requestId: '123456' });
}

Will log

{ level: 'error', pid: 1, timestamp: 1770231457646, message: 'Unexpected error', context: { requestId: '123456' } }

What is the new behavior?

When using the ConsoleLogger.error with two parameters, when the second parameter is not an Error, the logger will fallback to the default logger logic the detect the context from the provided arguments.

function bootsrap() {
  const logger = new ConsoleLogger('MyCustomLogger');
  logger.error('Unexpected error', { requestId: '123456' });
}

Will log

{ level: 'error', pid: 1, timestamp: 1770231457646, message: 'Unexpected error', context: 'MyCustomLogger' }
{ level: 'error', pid: 1, timestamp: 1770231457646, message: { requestId: '123456' }, context: 'MyCustomLogger' }

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@coveralls
Copy link

Pull Request Test Coverage Report for Build f2299e45-9676-4504-a184-e3991acbf705

Details

  • 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 89.829%

Totals Coverage Status
Change from base Build de840762-4bfe-4770-992b-5e40c0894ecb: 0.0%
Covered Lines: 7463
Relevant Lines: 8308

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

LGTM

@kamilmysliwiec kamilmysliwiec self-assigned this Feb 5, 2026
@kamilmysliwiec kamilmysliwiec removed their assignment Feb 5, 2026
@kamilmysliwiec kamilmysliwiec merged commit e7d186d into nestjs:master Feb 5, 2026
2 checks passed
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.

3 participants