summaryrefslogtreecommitdiff
path: root/paperbot/logstuff.py
blob: a4dbea62994445a9a5b60683dc5a6b898954a3b5 (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
31
32
33
34
import tempfile
import logging


def setup_logging():
    """
    Setup default logging handler to avoid "No handler found" warnings.
    """
    try:  # python 2.7+
        from logging import NullHandler
    except ImportError:
        class NullHandler(logging.Handler):
            def emit(self, record):
                pass
    finally:
        logging.getLogger("paperbot").addHandler(NullHandler())

    # this might be rude to enforce?
    logging.basicConfig(format='%(levelname)s:%(message)s',
                        level=logging.DEBUG)


def loghijack():
    """
    Hijack the logs for paperbot to go straight to a temporary file. The logs
    should still also be going to whatever other places have been configured.
    """
    logpath = tempfile.mktemp()

    logger = logging.getLogger("paperbot")
    fh = logging.FileHandler(logpath)
    logger.addHandler(fh)

    return (logpath, fh)