Esempio n. 1
0
class Flags(ContextObject):
    """Allows flags to be pushed on a flag stack.  Currently two flags
    are available:

    `errors`
        Can be set to override the current error behaviour.  This value is
        used when logging calls fail.  The default behaviour is spitting
        out the stacktrace to stderr but this can be overridden:

        =================== ==========================================
        ``'silent'``        fail silently
        ``'raise'``         raise a catchable exception
        ``'print'``         print the stacktrace to stderr (default)
        =================== ==========================================

    `introspection`
        Can be used to disable frame introspection.  This can give a
        speedup on production systems if you are using a JIT compiled
        Python interpreter such as pypy.  The default is `True`.

        Note that the default setup of some of the handler (mail for
        instance) includes frame dependent information which will
        not be available when introspection is disabled.

    Example usage::

        with Flags(errors='silent'):
            ...
    """
    stack_manager = ContextStackManager()

    def __init__(self, **flags):
        self.__dict__.update(flags)

    @staticmethod
    def get_flag(flag, default=None):
        """Looks up the current value of a specific flag."""
        for flags in Flags.stack_manager.iter_context_objects():
            val = getattr(flags, flag, Inherit)
            if val is not Inherit:
                return val
        return default
Esempio n. 2
0
class Processor(ContextObject):
    """Can be pushed to a stack to inject additional information into
    a log record as necessary::

        def inject_ip(record):
            record.extra['ip'] = '127.0.0.1'

        with Processor(inject_ip):
            ...
    """

    stack_manager = ContextStackManager()

    def __init__(self, callback=None):
        #: the callback that was passed to the constructor
        self.callback = callback

    def process(self, record):
        """Called with the log record that should be overridden.  The default
        implementation calls :attr:`callback` if it is not `None`.
        """
        if self.callback is not None:
            self.callback(record)