Skip to content

Conversation

@cahva
Copy link

@cahva cahva commented Nov 6, 2025

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.format to match debug.js formatting behavior, but this inadvertently broke pino's object-first logging pattern. When users called:

debug({ data: 'test' }, 'Some message');

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 in debug.js to 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 use util.format to maintain PR #134's functionality.

Changes

  • debug.js: Added logic to detect and handle object-first logging pattern and Error serialization
  • test/index.js: Added 6 comprehensive tests covering:
    • Object-first with message (the main fix)
    • Object-first with multiple fields
    • Arrays not treated as object-first (formatted as string)
    • Error with message (properly serialized by pino)
    • Error with custom properties (all properties preserved)
    • Object-only argument (no message)

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 output
  • debug(error, 'message') → now properly serialized with stack trace and custom properties

Additional Benefits

This fix also enables proper error serialization matching pino's native behavior. Errors are now serialized in an err object with type, message, stack, and any custom properties preserved, making error logging much more useful for debugging.

Closes #138

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.

PR #134 breaks pino's object-first logging syntax

1 participant