aboutsummaryrefslogtreecommitdiff
blob: c0e5d3f5075f78f2d5a42a68cb2d3ed77d69573d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Logging utilities."""

import logging
from contextlib import contextmanager

from . import __title__

# The logging system will call this automagically if its module-level logging
# functions are used. We call it explicitly to make sure something handles
# messages sent to our non-root logger. If the root logger already has handlers
# this is a noop, and if someone attaches a handler to our logger that
# overrides the root logger handler.
logging.basicConfig()

# Our main logger.
logger = logging.getLogger(__title__)


@contextmanager
def suppress_logging(level=logging.CRITICAL):
    """Context manager to suppress logging messages.

    :param level: logging level and below to suppress
    """
    orig_level = logging.root.manager.disable
    logging.disable(level)
    try:
        yield
    finally:
        logging.disable(orig_level)