A parent class for Configurables that log. Subclasses have a log trait, and the default behavior is to get the logger from the currently running Application.
| 463 | |
| 464 | |
| 465 | class LoggingConfigurable(Configurable): |
| 466 | """A parent class for Configurables that log. |
| 467 | |
| 468 | Subclasses have a log trait, and the default behavior |
| 469 | is to get the logger from the currently running Application. |
| 470 | """ |
| 471 | |
| 472 | log = Any(help="Logger or LoggerAdapter instance", allow_none=False) |
| 473 | |
| 474 | @validate("log") |
| 475 | def _validate_log(self, proposal: Bunch) -> LoggerType: |
| 476 | if not isinstance(proposal.value, (logging.Logger, logging.LoggerAdapter)): |
| 477 | # warn about unsupported type, but be lenient to allow for duck typing |
| 478 | warnings.warn( |
| 479 | f"{self.__class__.__name__}.log should be a Logger or LoggerAdapter," |
| 480 | f" got {proposal.value}.", |
| 481 | UserWarning, |
| 482 | stacklevel=2, |
| 483 | ) |
| 484 | return t.cast(LoggerType, proposal.value) |
| 485 | |
| 486 | @default("log") |
| 487 | def _log_default(self) -> LoggerType: |
| 488 | if isinstance(self.parent, LoggingConfigurable): |
| 489 | assert self.parent is not None |
| 490 | return t.cast(logging.Logger, self.parent.log) |
| 491 | from traitlets import log |
| 492 | |
| 493 | return log.get_logger() |
| 494 | |
| 495 | def _get_log_handler(self) -> logging.Handler | None: |
| 496 | """Return the default Handler |
| 497 | |
| 498 | Returns None if none can be found |
| 499 | |
| 500 | Deprecated, this now returns the first log handler which may or may |
| 501 | not be the default one. |
| 502 | """ |
| 503 | if not self.log: |
| 504 | return None |
| 505 | logger: logging.Logger = ( |
| 506 | self.log if isinstance(self.log, logging.Logger) else self.log.logger |
| 507 | ) |
| 508 | if not getattr(logger, "handlers", None): |
| 509 | # no handlers attribute or empty handlers list |
| 510 | return None |
| 511 | return logger.handlers[0] |
| 512 | |
| 513 | |
| 514 | CT = t.TypeVar("CT", bound="SingletonConfigurable") |
nothing calls this directly
no test coverage detected
searching dependent graphs…