fix: restore pino object-first logging syntax (#138) #139
+123
−2
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.
Description
This PR fixes #138 by restoring support for pino's object-first logging syntax, which was broken (in my opinion) by PR #134.
Problem
PR #134 introduced
util.formatto match debug.js formatting behavior, but this inadvertently broke pino's object-first logging pattern. When users called:Before this fix (broken):
{ "level": "info", "time": 1761659065406, "ns": "testlogger", "msg": "{ data: 'test' } Some message" }The object was stringified and concatenated with the message, losing all structured logging benefits.
After this fix (working):
{ "level": "info", "time": 1762442953870, "ns": "testlogger", "data": "test", "msg": "Some message" }The object is properly merged into the log entry, preserving structured logging.
Solution
Modified the
enabled()function indebug.jsto detect when the first argument is a plain object or Error with additional arguments. In these cases, arguments are passed directly to pino without formatting, allowing pino to handle them natively. All other argument patterns continue to useutil.formatto maintain PR #134's functionality.Changes
Testing
All previous tests pass, including the one in PR #134. I added new tests for the object- and error-first scenarios.
Backward Compatibility
This fix maintains full backward compatibility with PR #134:
debug('test', { option1: 'value1' })→ still formats as"test { option1: 'value1' }"debug({ data: 'test' }, 'message')→ now correctly produces structured outputdebug(error, 'message')→ now properly serialized with stack trace and custom propertiesAdditional Benefits
This fix also enables proper error serialization matching pino's native behavior. Errors are now serialized in an
errobject withtype,message,stack, and any custom properties preserved, making error logging much more useful for debugging.Closes #138