Skip to content

fix: preserve Error stack with splat format args - #2649

Open
SahinurDEV wants to merge 1 commit into
winstonjs:masterfrom
SahinurDEV:fix/splat-preserve-error
Open

SahinurDEV wants to merge 1 commit into
winstonjs:masterfrom
SahinurDEV:fix/splat-preserve-error

Conversation

@SahinurDEV

Copy link
Copy Markdown

Problem

logger.error('Oops', err) correctly logs the error stack, but
logger.error('Oops %d', 1, err) drops the Error entirely — no stack
reaches printf/json formatters.

Cause

logform's splat format Object.assigns leftover metas onto info.
Error.message / Error.stack / Error.cause are non-enumerable, so
the leftover Error after format tokens contributed nothing.

Fix

In Logger#log, normalize Error values in the splat array so stack
and cause become enumerable own properties before they reach splat.
message is intentionally not copied, so the already-formatted log
message is not overwritten.

Tests

Added a unit test that logs error('Oops %d', 1, err) with
format.errors({ stack: true }) + format.splat() and asserts
info.stack is preserved.

npx jest -c test/jest.config.unit.js --testPathPatterns=logger.test

Fixes #2527

logform's splat format Object.assigns leftover metas onto info, but
Error.message/stack/cause are non-enumerable so the stack was dropped
for calls like logger.error('Oops %d', 1, err). Normalize Error splat
args so stack and cause are enumerable without overwriting the
formatted message.

Fixes winstonjs#2527
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Splat and error stack traces do not work when used in conjunction

1 participant