feat: support logging of exceptions#277
Open
IzaakGough wants to merge 4 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces exception logging capabilities to the logger, including a new exception function to log active stack traces and utility functions to safely serialize exceptions and handle circular references in exception arguments. Comprehensive tests have been added to verify these changes. The review feedback suggests improving type safety by changing the type of the refs parameter from set[_typing.Any] to set[int] in both _exception_from_args and _remove_circular, as it is used to track object IDs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Add exception-aware logging support so
firebase_functions.loggercan accept exception objects without failing JSON serialization, and include stack trace information in logged output.Problem/Root Cause
Issue #172 reports that the current logging API does not handle exceptions properly. Passing an exception to
logger.error(..., error=e)can raise a JSON serialization error instead of writing a log entry, and the API does not provide a direct way to log full exception stack traces.The root cause is that exception objects were being passed through the existing log serialization path without being converted into a JSON-safe structure. That path handled common container types and circular references, but it did not recognize
BaseExceptionvalues or extract structured exception details such as type, message, arguments, and traceback information.Solution/Changes
Add exception-specific serialization in the logger so
BaseExceptionvalues are converted into a JSON-safe dictionary containing the exception type, message, arguments, and, when available, a formatted stack trace.Update circular-reference handling so exceptions and exception payloads can be serialized safely even when they contain self-referential data. The PR also adds a new
logger.exception(...)helper that logs at error severity and attaches the active exception stack trace directly to the top-level log entry.Testing
tests/test_logger.pycoveringlogger.error(..., error=exception)with normal exceptions, self-referential exception arguments, and cyclic payloads.logger.exception(...)and verifying that the emitted log includes a stack trace.