|
19 | 19 | Optional,
|
20 | 20 | TypeVar,
|
21 | 21 | Union,
|
| 22 | + cast, |
22 | 23 | overload,
|
23 | 24 | )
|
24 | 25 |
|
@@ -284,14 +285,14 @@ def _get_logger(self) -> logging.Logger:
|
284 | 285 |
|
285 | 286 | return logging.getLogger(logger_name)
|
286 | 287 |
|
287 |
| - def _get_handler(self) -> logging.Handler | None: |
| 288 | + def _get_handler(self) -> logging.Handler: |
288 | 289 | # is a logger handler already configured?
|
289 | 290 | if getattr(self, LOGGER_ATTRIBUTE_HANDLER, None):
|
290 | 291 | return self.logger_handler
|
291 | 292 |
|
292 | 293 | # for children, use parent's handler
|
293 | 294 | if self.child:
|
294 |
| - return getattr(self._logger.parent, LOGGER_ATTRIBUTE_POWERTOOLS_HANDLER, None) |
| 295 | + return getattr(self._logger.parent, LOGGER_ATTRIBUTE_POWERTOOLS_HANDLER, None) # type: ignore[return-value] # always checked in formatting |
295 | 296 |
|
296 | 297 | # otherwise, create a new stream handler (first time init)
|
297 | 298 | return logging.StreamHandler(self._stream)
|
@@ -695,15 +696,15 @@ def registered_handler(self) -> logging.Handler:
|
695 | 696 | @property
|
696 | 697 | def registered_formatter(self) -> BasePowertoolsFormatter:
|
697 | 698 | """Convenience property to access the first logger formatter"""
|
698 |
| - if self.child: |
699 |
| - if self._get_handler() is None: |
700 |
| - raise OrphanedChildLoggerError( |
701 |
| - "Orphan child loggers cannot append nor remove keys until a parent is initialized first. " |
702 |
| - "To solve this issue, you can A) make sure a parent logger is initialized first, or B) move append/remove keys operations to a later stage." # noqa: E501 |
703 |
| - "Reference: https://docs.powertools.aws.dev/lambda/python/latest/core/logger/#reusing-logger-across-your-code", |
704 |
| - ) |
| 699 | + handler = self.registered_handler |
| 700 | + if handler is None: |
| 701 | + raise OrphanedChildLoggerError( |
| 702 | + "Orphan child loggers cannot append nor remove keys until a parent is initialized first. " |
| 703 | + "To solve this issue, you can A) make sure a parent logger is initialized first, or B) move append/remove keys operations to a later stage." # noqa: E501 |
| 704 | + "Reference: https://docs.powertools.aws.dev/lambda/python/latest/core/logger/#reusing-logger-across-your-code", |
| 705 | + ) |
705 | 706 |
|
706 |
| - return self.registered_handler.formatter |
| 707 | + return cast(BasePowertoolsFormatter, handler.formatter) |
707 | 708 |
|
708 | 709 | @property
|
709 | 710 | def log_level(self) -> int:
|
|
0 commit comments