Commit 7a74d8fd authored by Antoine Millet's avatar Antoine Millet
Browse files

Added an helper to patch logging to add getChild method on loggers

This feature of logging module is available since Python 2.7, this commit
backport it to make it available in previous versions.
parent 0fe45e1a
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+30 −0
Original line number Diff line number Diff line
""" Logging helpers.
"""

import types


# This function is taken from logging package from Python stdlib v2.7.
def getChild(self, suffix):
    """
    Get a logger which is a descendant to this one.

    This is a convenience method, such that

    logging.getLogger('abc').getChild('def.ghi')

    is the same as

    logging.getLogger('abc.def.ghi')

    It's useful, for example, when the parent logger is named using
    __name__ rather than a literal string.
    """
    if self.root is not self:
        suffix = '.'.join((self.name, suffix))
    return self.manager.getLogger(suffix)


def patch_logging():
    import logging
    logging.Logger.getChild = types.UnboundMethodType(getChild, None, logging.Logger)