Skip to content

logger

Logging to both file and console

disable_logging()

Temporarily disable logging.

Usage:

from optimade_gateway.common.logger import disable_logging

# Do stuff, logging to all handlers.
# ...
with disable_logging():
    # Do stuff, without logging to any handlers.
    # ...
# Do stuff, logging to all handlers now re-enabled.
# ...
Source code in optimade_gateway/common/logger.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@contextmanager
def disable_logging():
    """Temporarily disable logging.

    Usage:

    ```python
    from optimade_gateway.common.logger import disable_logging

    # Do stuff, logging to all handlers.
    # ...
    with disable_logging():
        # Do stuff, without logging to any handlers.
        # ...
    # Do stuff, logging to all handlers now re-enabled.
    # ...
    ```

    """
    try:
        # Disable logging lower than CRITICAL level
        logging.disable(logging.CRITICAL)
        yield
    finally:
        # Re-enable logging to desired levels
        logging.disable(logging.NOTSET)