feat: Add stream_name to structured logs#3680
Conversation
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideIntroduce structured logging support for streams by wrapping loggers in LoggerAdapter instances that inject a stream_name field, and adjust typing and log formatter behavior to handle the new context field safely. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Documentation build overview
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
PluginBase.logger,logging.LoggerAdapteris constructed without the requiredextraargument, which will raise aTypeErrorat runtime; pass an explicitextradict (e.g.extra={}) when instantiating the adapter. - In
Stream.__init__, accessingtap.logger.logger.getChild(self.name)assumestap.loggeris aLoggerAdapter; it would be safer to rely on a documented interface or helper method rather than the.loggerattribute to avoid coupling to the adapter’s internal structure. - Updating
loggertype hints tologging.Logger | logging.LoggerAdapterin some places but not others may cause inconsistencies for callers; consider centralizing a common logger protocol or alias to keep the logger interface uniform across the SDK.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `PluginBase.logger`, `logging.LoggerAdapter` is constructed without the required `extra` argument, which will raise a `TypeError` at runtime; pass an explicit `extra` dict (e.g. `extra={}`) when instantiating the adapter.
- In `Stream.__init__`, accessing `tap.logger.logger.getChild(self.name)` assumes `tap.logger` is a `LoggerAdapter`; it would be safer to rely on a documented interface or helper method rather than the `.logger` attribute to avoid coupling to the adapter’s internal structure.
- Updating `logger` type hints to `logging.Logger | logging.LoggerAdapter` in some places but not others may cause inconsistencies for callers; consider centralizing a common logger protocol or alias to keep the logger interface uniform across the SDK.
## Individual Comments
### Comment 1
<location path="singer_sdk/plugin_base.py" line_range="215-221" />
<code_context>
@classproperty
- def logger(cls) -> logging.Logger: # noqa: N805
+ def logger(cls) -> logging.LoggerAdapter: # noqa: N805
"""Get logger.
Returns:
Plugin logger.
"""
- return logging.getLogger(cls.name)
+ return logging.LoggerAdapter(logging.getLogger(cls.name))
# Constructor
</code_context>
<issue_to_address>
**issue (bug_risk):** LoggerAdapter is instantiated without the required `extra` mapping.
`logging.LoggerAdapter` requires both a logger and an `extra` mapping. This call (`logging.LoggerAdapter(logging.getLogger(cls.name))`) will raise `TypeError` at runtime. Please pass an `extra` dict-like mapping, e.g. `logging.LoggerAdapter(logging.getLogger(cls.name), extra={"app_name": cls.name})` (or other appropriate metadata).
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| def logger(cls) -> logging.LoggerAdapter: # noqa: N805 | ||
| """Get logger. | ||
|
|
||
| Returns: | ||
| Plugin logger. | ||
| """ | ||
| return logging.getLogger(cls.name) | ||
| return logging.LoggerAdapter(logging.getLogger(cls.name)) |
There was a problem hiding this comment.
issue (bug_risk): LoggerAdapter is instantiated without the required extra mapping.
logging.LoggerAdapter requires both a logger and an extra mapping. This call (logging.LoggerAdapter(logging.getLogger(cls.name))) will raise TypeError at runtime. Please pass an extra dict-like mapping, e.g. logging.LoggerAdapter(logging.getLogger(cls.name), extra={"app_name": cls.name}) (or other appropriate metadata).
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## main #3680 +/- ##
==========================================
- Coverage 94.12% 88.39% -5.74%
==========================================
Files 73 73
Lines 6200 6202 +2
Branches 762 763 +1
==========================================
- Hits 5836 5482 -354
- Misses 270 620 +350
- Partials 94 100 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
SSIA
Summary by Sourcery
Propagate structured logging context by wrapping plugin and stream loggers in LoggerAdapter and updating logging utilities to handle optional stream-specific metadata.
Enhancements: