Skip to content

Remove console as default logger to fix structured logging interference#149

Merged
baelter merged 1 commit intomainfrom
copilot/refactor-default-logger-implementation
Sep 11, 2025
Merged

Remove console as default logger to fix structured logging interference#149
baelter merged 1 commit intomainfrom
copilot/refactor-default-logger-implementation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Sep 9, 2025

Problem

The AMQP client library was using console as the default logger, which caused issues for applications using structured logging. Random console.log, console.warn, and console.error calls from the library would:

  • Break log parsers expecting structured JSON output
  • Pollute application logs with unformatted messages
  • Make it impossible to control logging behavior from the consuming application

Solution

This PR changes the default logging behavior to be opt-in rather than automatic:

Key Changes

  1. Default logger is now undefined instead of console in AMQPBaseClient
  2. Added logger parameter to constructors:
    • AMQPClient constructor accepts optional logger parameter
    • AMQPWebSocketClient constructor accepts optional logger parameter
    • AMQPWebSocketClient init object supports logger property
  3. Fixed hardcoded console usage - replaced console.info with this.logger?.info for consistency

Usage Examples

// Default: no logging (won't interfere with structured logging)
const client = new AMQPClient('amqp://localhost')

// Opt-in to console logging for debugging
const debugClient = new AMQPClient('amqp://localhost', undefined, console)

// Use custom structured logger (Winston, Bunyan, etc.)
const structuredClient = new AMQPClient('amqp://localhost', undefined, myLogger)

// WebSocket client with logger via init object
const wsClient = new AMQPWebSocketClient({
  url: 'wss://example.com/amqp-ws',
  logger: myLogger
})

Backward Compatibility

  • No breaking changes - existing code continues to work unchanged
  • All logging calls use optional chaining (?.) so they safely handle null logger
  • Same Logger interface - Pick<typeof console, 'debug' | 'info' | 'warn' | 'error'>

Testing

Added comprehensive test coverage:

  • ✅ Default logger is undefined
  • ✅ Custom logger can be provided via constructor
  • ✅ WebSocket client supports logger in both constructor and init object
  • ✅ No errors when logger is null
  • ✅ Custom logger receives expected calls

Libraries should not log by default unless explicitly configured to do so. This change allows consuming applications to maintain control over their logging strategy while still providing debugging capabilities when needed.

Fixes #143.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Avoid using console as default logger — breaks structured logging in consuming applications Remove console as default logger to fix structured logging interference Sep 9, 2025
Copilot AI requested a review from baelter September 9, 2025 11:36
Copy link
Copy Markdown
Contributor

@antondalgren antondalgren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does what it's expected to do!

Comment thread src/amqp-base-client.ts Outdated
@baelter baelter force-pushed the copilot/refactor-default-logger-implementation branch 2 times, most recently from f7f2ed7 to 072ec1e Compare September 11, 2025 09:26
@baelter baelter marked this pull request as ready for review September 11, 2025 09:30
@baelter baelter force-pushed the copilot/refactor-default-logger-implementation branch from 072ec1e to d565b47 Compare September 11, 2025 09:46
@baelter baelter merged commit 5bdb39c into main Sep 11, 2025
6 checks passed
@baelter baelter deleted the copilot/refactor-default-logger-implementation branch September 11, 2025 09:48
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.

Avoid using console as default logger — breaks structured logging in consuming applications

3 participants