Skip to content

afnio.logging_config

afnio.logging_config.configure_logging(verbosity='info')

Configure logging for the afnio library.

Sets up logging format and levels for CLI, scripts, and Jupyter notebooks. In a notebook, adds a custom handler for INFO-level logs to display user-facing messages with color and formatting.

Parameters:

Name Type Description Default
verbosity str

Logging level as a string ("info", "debug", etc.).

'info'
Source code in afnio/logging_config.py
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def configure_logging(verbosity: str = "info"):
    """
    Configure logging for the `afnio` library.

    Sets up logging format and levels for CLI, scripts, and Jupyter notebooks.
    In a notebook, adds a custom handler for INFO-level logs to display
    user-facing messages with color and formatting.

    Args:
        verbosity: Logging level as a string (`"info"`, `"debug"`, etc.).
    """
    if not isinstance(verbosity, str):
        raise TypeError("verbosity must be a string like 'info', 'debug', etc.")
    level = getattr(logging, verbosity.upper(), logging.INFO)
    if level <= logging.DEBUG:
        fmt = "%(asctime)s - %(name)-35s - %(levelname)-9s - %(message)s"
    else:
        fmt = "%(levelname)-9s: %(message)s"

    # Set root logger to WARNING (default for all libraries)
    logging.basicConfig(
        level=logging.WARNING,
        format=fmt,
        force=True,
    )

    # Set only afnio logs to the desired level
    logging.getLogger("afnio").setLevel(level)

    # Add notebook handler for INFO logs if in notebook
    if _in_notebook() and level == logging.INFO:
        handler = _NotebookInfoHandler()
        handler.setLevel(logging.INFO)
        handler.addFilter(lambda record: record.levelno == logging.INFO)
        logging.getLogger().addHandler(handler)

        class NoInfoFilter(logging.Filter):
            """
            Logging filter that suppresses INFO-level log records.
            Used to prevent duplicate INFO messages in notebook environments.
            """

            def filter(self, record):
                """
                Filter method to suppress INFO-level log records.

                Args:
                    record (logging.LogRecord): The log record to filter.

                Returns:
                    bool: True if the record should be logged, False otherwise.
                """
                return record.levelno != logging.INFO

        for h in logging.getLogger().handlers:
            if not isinstance(h, _NotebookInfoHandler):
                h.addFilter(NoInfoFilter())

afnio.logging_config.set_logger_level(logger_name, level)

Context manager to temporarily set the logging level for a logger.

Parameters:

Name Type Description Default
logger_name str

Name of the logger to set the level for.

required
level int

Logging level to set (e.g., logging.DEBUG, logging.INFO).

required

Examples:

>>> with set_logger_level("afnio.tellurio.run", logging.WARNING):
>>>     # code that should log only WARNING and above
Source code in afnio/logging_config.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@contextmanager
def set_logger_level(logger_name: str, level: int):
    """
    Context manager to temporarily set the logging level for a logger.

    Args:
        logger_name: Name of the logger to set the level for.
        level: Logging level to set (e.g., logging.DEBUG, logging.INFO).

    Examples:
        >>> with set_logger_level("afnio.tellurio.run", logging.WARNING):
        >>>     # code that should log only WARNING and above
    """
    logger = logging.getLogger(logger_name)
    old_level = logger.level
    logger.setLevel(level)
    try:
        yield
    finally:
        logger.setLevel(old_level)