Log a component instance with class info, repr, and attributes. Description: Emits a structured log block for a single object. The block includes a class line, a representation line, and a recursive attribute dump up to the specified depth. Args: logger (log
(
logger: logging.Logger,
kind: str,
label: str,
obj,
max_depth: int,
)
| 869 | |
| 870 | |
| 871 | def log_component( |
| 872 | logger: logging.Logger, |
| 873 | kind: str, |
| 874 | label: str, |
| 875 | obj, |
| 876 | max_depth: int, |
| 877 | ) -> None: |
| 878 | """Log a component instance with class info, repr, and attributes. |
| 879 | |
| 880 | Description: |
| 881 | Emits a structured log block for a single object. The block includes |
| 882 | a class line, a representation line, and a recursive attribute dump |
| 883 | up to the specified depth. |
| 884 | |
| 885 | Args: |
| 886 | logger (logging.Logger): Logger used to emit messages. |
| 887 | kind (str): Label prefix for the entry (e.g., "Component", "Env"). |
| 888 | label (str): Human-readable label identifying the entry. |
| 889 | obj: Object instance to log. If None, the function returns early. |
| 890 | max_depth (int): Maximum depth for recursive attribute dumping. |
| 891 | |
| 892 | Raises: |
| 893 | None |
| 894 | |
| 895 | Returns: |
| 896 | None |
| 897 | |
| 898 | Example: |
| 899 | ```python |
| 900 | from espnet3.utils.logging_utils import log_component |
| 901 | |
| 902 | # Custom class instance. |
| 903 | class CustomThing: |
| 904 | def __init__(self, name: str, value: int): |
| 905 | self.name = name |
| 906 | self.value = value |
| 907 | |
| 908 | log_component(logger, "Custom", "example", CustomThing("demo", 7)) |
| 909 | ``` |
| 910 | |
| 911 | Example log output: |
| 912 | ``` |
| 913 | Custom[example] class: my_module.CustomThing |
| 914 | Custom[example]: <my_module.CustomThing object at ...> |
| 915 | name: 'demo' |
| 916 | value: 7 |
| 917 | ``` |
| 918 | |
| 919 | Notes: |
| 920 | - The logger uses `stacklevel=2` so log lines point at the caller. |
| 921 | - Attribute dumping uses `build_qualified_name` for readable class names. |
| 922 | - Set `max_depth` to 0 to log only the class and repr lines. |
| 923 | """ |
| 924 | if obj is None: |
| 925 | return |
| 926 | logger.log( |
| 927 | logging.INFO, |
| 928 | "%s[%s] class: %s", |
no test coverage detected
searching dependent graphs…