Skip to content

Adds console logging functionality to the Sonner toast library, enabling developers to log toast events (create, update, dismiss) for debugging and automated testing purposes. Supports both human-readable and JSON output formats.#728

Open
jhubbardsf wants to merge 1 commit intoemilkowalski:mainfrom
jhubbardsf:feat/console-logging

Conversation

@jhubbardsf
Copy link
Copy Markdown

Summary

Adds console logging functionality to the Sonner toast library, enabling developers to log toast events (create, update, dismiss) for debugging and automated testing purposes. Supports both human-readable and JSON output formats.

Changes

  • src/types.ts: Added ConsoleLogFormat type and ConsoleLogConfig interface; extended ToasterProps with consoleLog and consoleLogFormat properties
  • src/state.ts: Implemented core logging functionality:
    • Added consoleLogConfig state management
    • Created extractText() helper to extract text from React nodes
    • Implemented logToast() function supporting both 'human' and 'json' formats
    • Added configure() method to Observer class for global config
    • Integrated logging calls in addToast() and dismiss() methods
    • Exported configure method in the public toast API
  • src/index.tsx: Added consoleLog and consoleLogFormat props to Toaster component; added useEffect to configure ToastState when props change
  • README.md: Added documentation section with usage examples and output format samples
  • website/src/pages/toast.mdx: Added documentation for toast.configure() method
  • website/src/pages/toaster.mdx: Added comprehensive console logging documentation with examples and API reference entries
  • package.json: Bumped version from 2.0.7 to 2.0.8

Testing

  • Verify console logging works with consoleLog prop enabled on Toaster component
  • Test both 'human' and 'json' output formats produce correct output
  • Confirm toast.configure() method properly enables logging before Toaster mounts
  • Validate SSR guard prevents logging on server-side
  • Test logging for create, update, and dismiss events
  • Ensure extractText() correctly handles strings, numbers, functions, React elements, and arrays
  • Verify logging can be disabled dynamically by changing props

Checklist

  • Code follows project conventions
  • Tests added/updated as needed
  • Documentation updated as needed

- add consoleLog and consoleLogFormat props to Toaster component
- implement toast.configure() for global logging configuration
- support human-readable and JSON output formats
- log toast create and dismiss events to console
- add SSR guard to prevent logging on server side
- extract text content from React nodes for logging
- update documentation with usage examples
- bump version to 2.0.8
@vercel
Copy link
Copy Markdown

vercel bot commented Dec 29, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
sonner Ready Ready Preview, Comment Dec 29, 2025 10:08am

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.

1 participant