Codebase list logbook / debian/0.7.0-1 logbook / base.py
debian/0.7.0-1

Tree @debian/0.7.0-1 (Download .tar.gz)

base.py @debian/0.7.0-1raw · history · blame

  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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# -*- coding: utf-8 -*-
"""
    logbook.base
    ~~~~~~~~~~~~

    Base implementation for logbook.

    :copyright: (c) 2010 by Armin Ronacher, Georg Brandl.
    :license: BSD, see LICENSE for more details.
"""
import os
import sys
try:
    import thread
except ImportError:
    # for python 3.1,3.2
    import _thread as thread
import threading
import traceback
from itertools import chain
from weakref import ref as weakref
from datetime import datetime

from logbook.helpers import to_safe_json, parse_iso8601, cached_property, \
     PY2, u, string_types, iteritems, integer_types
try:
    from logbook._speedups import group_reflected_property, \
         ContextStackManager, StackedObject
except ImportError:
    from logbook._fallback import group_reflected_property, \
         ContextStackManager, StackedObject

_datetime_factory = datetime.utcnow
def set_datetime_format(datetime_format):
    """
    Set the format for the datetime objects created, which are then
    made available as the :py:attr:`LogRecord.time` attribute of
    :py:class:`LogRecord` instances.

    :param datetime_format: Indicates how to generate datetime objects.  Possible values are:

         "utc"
             :py:attr:`LogRecord.time` will be a datetime in UTC time zone (but not time zone aware)
         "local"
             :py:attr:`LogRecord.time` will be a datetime in local time zone (but not time zone aware)

    This function defaults to creating datetime objects in UTC time,
    using `datetime.utcnow()
    <http://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow>`_,
    so that logbook logs all times in UTC time by default.  This is
    recommended in case you have multiple software modules or
    instances running in different servers in different time zones, as
    it makes it simple and less error prone to correlate logging
    across the different servers.

    On the other hand if all your software modules are running in the
    same time zone and you have to correlate logging with third party
    modules already logging in local time, it can be more convenient
    to have logbook logging to local time instead of UTC.  Local time
    logging can be enabled like this::

       import logbook
       from datetime import datetime
       logbook.set_datetime_format("local")

    """
    global _datetime_factory
    if datetime_format == "utc":
        _datetime_factory = datetime.utcnow
    elif datetime_format == "local":
        _datetime_factory = datetime.now
    else:
        raise ValueError("Invalid value %r.  Valid values are 'utc' and 'local'." % (datetime_format,))

# make sure to sync these up with _speedups.pyx
CRITICAL = 6
ERROR = 5
WARNING = 4
NOTICE = 3
INFO = 2
DEBUG = 1
NOTSET = 0

_level_names = {
    CRITICAL:   'CRITICAL',
    ERROR:      'ERROR',
    WARNING:    'WARNING',
    NOTICE:     'NOTICE',
    INFO:       'INFO',
    DEBUG:      'DEBUG',
    NOTSET:     'NOTSET'
}
_reverse_level_names = dict((v, k) for (k, v) in iteritems(_level_names))
_missing = object()


# on python 3 we can savely assume that frame filenames will be in
# unicode, on Python 2 we have to apply a trick.
if PY2:
    def _convert_frame_filename(fn):
        if isinstance(fn, unicode):
            fn = fn.decode(sys.getfilesystemencoding() or 'utf-8',
                           'replace')
        return fn
else:
    def _convert_frame_filename(fn):
        return fn


def level_name_property():
    """Returns a property that reflects the level as name from
    the internal level attribute.
    """

    def _get_level_name(self):
        return get_level_name(self.level)

    def _set_level_name(self, level):
        self.level = lookup_level(level)
    return property(_get_level_name, _set_level_name,
                    doc='The level as unicode string')


def lookup_level(level):
    """Return the integer representation of a logging level."""
    if isinstance(level, integer_types):
        return level
    try:
        return _reverse_level_names[level]
    except KeyError:
        raise LookupError('unknown level name %s' % level)


def get_level_name(level):
    """Return the textual representation of logging level 'level'."""
    try:
        return _level_names[level]
    except KeyError:
        raise LookupError('unknown level')


class ExtraDict(dict):
    """A dictionary which returns ``u''`` on missing keys."""

    if sys.version_info[:2] < (2, 5):
        def __getitem__(self, key):
            try:
                return dict.__getitem__(self, key)
            except KeyError:
                return u('')
    else:
        def __missing__(self, key):
            return u('')

    def copy(self):
        return self.__class__(self)

    def __repr__(self):
        return '%s(%s)' % (
            self.__class__.__name__,
            dict.__repr__(self)
        )


class _ExceptionCatcher(object):
    """Helper for exception caught blocks."""

    def __init__(self, logger, args, kwargs):
        self.logger = logger
        self.args = args
        self.kwargs = kwargs

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, tb):
        if exc_type is not None:
            kwargs = self.kwargs.copy()
            kwargs['exc_info'] = (exc_type, exc_value, tb)
            self.logger.exception(*self.args, **kwargs)
        return True


class ContextObject(StackedObject):
    """An object that can be bound to a context.  It is managed by the
    :class:`ContextStackManager`"""

    #: subclasses have to instanciate a :class:`ContextStackManager`
    #: object on this attribute which is then shared for all the
    #: subclasses of it.
    stack_manager = None

    def push_thread(self):
        """Pushes the context object to the thread stack."""
        self.stack_manager.push_thread(self)

    def pop_thread(self):
        """Pops the context object from the stack."""
        popped = self.stack_manager.pop_thread()
        assert popped is self, 'popped unexpected object'

    def push_application(self):
        """Pushes the context object to the application stack."""
        self.stack_manager.push_application(self)

    def pop_application(self):
        """Pops the context object from the stack."""
        popped = self.stack_manager.pop_application()
        assert popped is self, 'popped unexpected object'


class NestedSetup(StackedObject):
    """A nested setup can be used to configure multiple handlers
    and processors at once.
    """

    def __init__(self, objects=None):
        self.objects = list(objects or ())

    def push_application(self):
        for obj in self.objects:
            obj.push_application()

    def pop_application(self):
        for obj in reversed(self.objects):
            obj.pop_application()

    def push_thread(self):
        for obj in self.objects:
            obj.push_thread()

    def pop_thread(self):
        for obj in reversed(self.objects):
            obj.pop_thread()


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)


class _InheritedType(object):
    __slots__ = ()

    def __repr__(self):
        return 'Inherit'

    def __reduce__(self):
        return 'Inherit'
Inherit = _InheritedType()


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


def _create_log_record(cls, dict):
    """Extra function for reduce because on Python 3 unbound methods
    can no longer be pickled.
    """
    return cls.from_dict(dict)


class LogRecord(object):
    """A LogRecord instance represents an event being logged.

    LogRecord instances are created every time something is logged. They
    contain all the information pertinent to the event being logged. The
    main information passed in is in msg and args
    """
    _pullable_information = frozenset((
        'func_name', 'module', 'filename', 'lineno', 'process_name', 'thread',
        'thread_name', 'formatted_exception', 'message', 'exception_name',
        'exception_message'
    ))
    _noned_on_close = frozenset(('exc_info', 'frame', 'calling_frame'))

    #: can be overriden by a handler to not close the record.  This could
    #: lead to memory leaks so it should be used carefully.
    keep_open = False

    #: the time of the log record creation as :class:`datetime.datetime`
    #: object.  This information is unavailable until the record was
    #: heavy initialized.
    time = None

    #: a flag that is `True` if the log record is heavy initialized which
    #: is not the case by default.
    heavy_initialized = False

    #: a flag that is `True` when heavy initialization is no longer possible
    late = False

    #: a flag that is `True` when all the information was pulled from the
    #: information that becomes unavailable on close.
    information_pulled = False

    def __init__(self, channel, level, msg, args=None, kwargs=None,
                 exc_info=None, extra=None, frame=None, dispatcher=None):
        #: the name of the logger that created it or any other textual
        #: channel description.  This is a descriptive name and can be
        #: used for filtering.
        self.channel = channel
        #: The message of the log record as new-style format string.
        self.msg = msg
        #: the positional arguments for the format string.
        self.args = args or ()
        #: the keyword arguments for the format string.
        self.kwargs = kwargs or {}
        #: the level of the log record as integer.
        self.level = level
        #: optional exception information.  If set, this is a tuple in the
        #: form ``(exc_type, exc_value, tb)`` as returned by
        #: :func:`sys.exc_info`.
        #: This parameter can also be ``True``, which would cause the exception info tuple
        #: to be fetched for you.
        self.exc_info = exc_info
        #: optional extra information as dictionary.  This is the place
        #: where custom log processors can attach custom context sensitive
        #: data.
        self.extra = ExtraDict(extra or ())
        #: If available, optionally the interpreter frame that pulled the
        #: heavy init.  This usually points to somewhere in the dispatcher.
        #: Might not be available for all calls and is removed when the log
        #: record is closed.
        self.frame = frame
        #: the PID of the current process
        self.process = None
        if dispatcher is not None:
            dispatcher = weakref(dispatcher)
        self._dispatcher = dispatcher

    def heavy_init(self):
        """Does the heavy initialization that could be expensive.  This must
        not be called from a higher stack level than when the log record was
        created and the later the initialization happens, the more off the
        date information will be for example.

        This is internally used by the record dispatching system and usually
        something not to worry about.
        """
        if self.heavy_initialized:
            return
        assert not self.late, 'heavy init is no longer possible'
        self.heavy_initialized = True
        self.process = os.getpid()
        self.time = _datetime_factory()
        if self.frame is None and Flags.get_flag('introspection', True):
            self.frame = sys._getframe(1)
        if self.exc_info is True:
            self.exc_info = sys.exc_info()

    def pull_information(self):
        """A helper function that pulls all frame-related information into
        the object so that this information is available after the log
        record was closed.
        """
        if self.information_pulled:
            return
        # due to how cached_property is implemented, the attribute access
        # has the side effect of caching the attribute on the instance of
        # the class.
        for key in self._pullable_information:
            getattr(self, key)
        self.information_pulled = True

    def close(self):
        """Closes the log record.  This will set the frame and calling
        frame to `None` and frame-related information will no longer be
        available unless it was pulled in first (:meth:`pull_information`).
        This makes a log record safe for pickling and will clean up
        memory that might be still referenced by the frames.
        """
        for key in self._noned_on_close:
            setattr(self, key, None)
        self.late = True

    def __reduce_ex__(self, protocol):
        return _create_log_record, (type(self), self.to_dict())

    def to_dict(self, json_safe=False):
        """Exports the log record into a dictionary without the information
        that cannot be safely serialized like interpreter frames and
        tracebacks.
        """
        self.pull_information()
        rv = {}
        for key, value in iteritems(self.__dict__):
            if key[:1] != '_' and key not in self._noned_on_close:
                rv[key] = value
        # the extra dict is exported as regular dict
        rv['extra'] = dict(rv['extra'])
        if json_safe:
            return to_safe_json(rv)
        return rv

    @classmethod
    def from_dict(cls, d):
        """Creates a log record from an exported dictionary.  This also
        supports JSON exported dictionaries.
        """
        rv = object.__new__(cls)
        rv.update_from_dict(d)
        return rv

    def update_from_dict(self, d):
        """Like the :meth:`from_dict` classmethod, but will update the
        instance in place.  Helpful for constructors.
        """
        self.__dict__.update(d)
        for key in self._noned_on_close:
            setattr(self, key, None)
        self._information_pulled = True
        self._channel = None
        if isinstance(self.time, string_types):
            self.time = parse_iso8601(self.time)
        self.extra = ExtraDict(self.extra)
        return self

    @cached_property
    def message(self):
        """The formatted message."""
        if not (self.args or self.kwargs):
            return self.msg
        try:
            try:
                return self.msg.format(*self.args, **self.kwargs)
            except UnicodeDecodeError:
                # Assume an unicode message but mixed-up args
                msg = self.msg.encode('utf-8', 'replace')
                return msg.format(*self.args, **self.kwargs)
            except (UnicodeEncodeError, AttributeError):
                # we catch AttributeError since if msg is bytes, it won't have the 'format' method
                if sys.exc_info()[0] is AttributeError and (PY2 or not isinstance(self.msg, bytes)):
                    # this is not the case we thought it is...
                    raise
                # Assume encoded message with unicode args.
                # The assumption of utf8 as input encoding is just a guess,
                # but this codepath is unlikely (if the message is a constant
                # string in the caller's source file)
                msg = self.msg.decode('utf-8', 'replace')
                return msg.format(*self.args, **self.kwargs)

        except Exception:
            # this obviously will not give a proper error message if the
            # information was not pulled and the log record no longer has
            # access to the frame.  But there is not much we can do about
            # that.
            e = sys.exc_info()[1]
            errormsg = ('Could not format message with provided '
                       'arguments: {err}\n  msg={msg!r}\n  '
                       'args={args!r} \n  kwargs={kwargs!r}.\n'
                       'Happened in file {file}, line {lineno}').format(
                err=e, msg=self.msg, args=self.args,
                kwargs=self.kwargs, file=self.filename,
                lineno=self.lineno
            )
            if PY2:
                errormsg = errormsg.encode('utf-8')
            raise TypeError(errormsg)

    level_name = level_name_property()

    @cached_property
    def calling_frame(self):
        """The frame in which the record has been created.  This only
        exists for as long the log record is not closed.
        """
        frm = self.frame
        globs = globals()
        while frm is not None and frm.f_globals is globs:
            frm = frm.f_back
        return frm

    @cached_property
    def func_name(self):
        """The name of the function that triggered the log call if
        available.  Requires a frame or that :meth:`pull_information`
        was called before.
        """
        cf = self.calling_frame
        if cf is not None:
            return cf.f_code.co_name

    @cached_property
    def module(self):
        """The name of the module that triggered the log call if
        available.  Requires a frame or that :meth:`pull_information`
        was called before.
        """
        cf = self.calling_frame
        if cf is not None:
            return cf.f_globals.get('__name__')

    @cached_property
    def filename(self):
        """The filename of the module in which the record has been created.
        Requires a frame or that :meth:`pull_information` was called before.
        """
        cf = self.calling_frame
        if cf is not None:
            fn = cf.f_code.co_filename
            if fn[:1] == '<' and fn[-1:] == '>':
                return fn
            return _convert_frame_filename(os.path.abspath(fn))

    @cached_property
    def lineno(self):
        """The line number of the file in which the record has been created.
        Requires a frame or that :meth:`pull_information` was called before.
        """
        cf = self.calling_frame
        if cf is not None:
            return cf.f_lineno

    @cached_property
    def thread(self):
        """The ident of the thread.  This is evaluated late and means that
        if the log record is passed to another thread, :meth:`pull_information`
        was called in the old thread.
        """
        return thread.get_ident()

    @cached_property
    def thread_name(self):
        """The name of the thread.  This is evaluated late and means that
        if the log record is passed to another thread, :meth:`pull_information`
        was called in the old thread.
        """
        return threading.currentThread().getName()

    @cached_property
    def process_name(self):
        """The name of the process in which the record has been created."""
        # Errors may occur if multiprocessing has not finished loading
        # yet - e.g. if a custom import hook causes third-party code
        # to run when multiprocessing calls import. See issue 8200
        # for an example
        mp = sys.modules.get('multiprocessing')
        if mp is not None:  # pragma: no cover
            try:
                return mp.current_process().name
            except Exception:
                pass

    @cached_property
    def formatted_exception(self):
        """The formatted exception which caused this record to be created
        in case there was any.
        """
        if self.exc_info is not None:
            rv = ''.join(traceback.format_exception(*self.exc_info))
            if PY2:
                rv = rv.decode('utf-8', 'replace')
            return rv.rstrip()

    @cached_property
    def exception_name(self):
        """The name of the exception."""
        if self.exc_info is not None:
            cls = self.exc_info[0]
            return u(cls.__module__ + '.' + cls.__name__)

    @property
    def exception_shortname(self):
        """An abbreviated exception name (no import path)"""
        return self.exception_name.rsplit('.')[-1]

    @cached_property
    def exception_message(self):
        """The message of the exception."""
        if self.exc_info is not None:
            val = self.exc_info[1]
            try:
                return u(str(val))
            except UnicodeError:
                return str(val).decode('utf-8', 'replace')

    @property
    def dispatcher(self):
        """The dispatcher that created the log record.  Might not exist because
        a log record does not have to be created from a logger or other
        dispatcher to be handled by logbook.  If this is set, it will point to
        an object that implements the :class:`~logbook.base.RecordDispatcher`
        interface.
        """
        if self._dispatcher is not None:
            return self._dispatcher()


class LoggerMixin(object):
    """This mixin class defines and implements the "usual" logger
    interface (i.e. the descriptive logging functions).

    Classes using this mixin have to implement a :meth:`!handle` method which
    takes a :class:`~logbook.LogRecord` and passes it along.
    """

    #: The name of the minimium logging level required for records to be
    #: created.
    level_name = level_name_property()

    def debug(self, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to :data:`~logbook.DEBUG`.
        """
        if not self.disabled and DEBUG >= self.level:
            self._log(DEBUG, args, kwargs)

    def info(self, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to :data:`~logbook.INFO`.
        """
        if not self.disabled and INFO >= self.level:
            self._log(INFO, args, kwargs)

    def warn(self, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to :data:`~logbook.WARNING`.  This function has an alias
        named :meth:`warning`.
        """
        if not self.disabled and WARNING >= self.level:
            self._log(WARNING, args, kwargs)

    def warning(self, *args, **kwargs):
        """Alias for :meth:`warn`."""
        return self.warn(*args, **kwargs)

    def notice(self, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to :data:`~logbook.NOTICE`.
        """
        if not self.disabled and NOTICE >= self.level:
            self._log(NOTICE, args, kwargs)

    def error(self, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to :data:`~logbook.ERROR`.
        """
        if not self.disabled and ERROR >= self.level:
            self._log(ERROR, args, kwargs)

    def exception(self, *args, **kwargs):
        """Works exactly like :meth:`error` just that the message
        is optional and exception information is recorded.
        """
        if self.disabled or ERROR < self.level:
            return
        if not args:
            args = ('Uncaught exception occurred',)
        if 'exc_info' not in kwargs:
            exc_info = sys.exc_info()
            assert exc_info[0] is not None, 'no exception occurred'
            kwargs.setdefault('exc_info', sys.exc_info())
        return self.error(*args, **kwargs)

    def critical(self, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to :data:`~logbook.CRITICAL`.
        """
        if not self.disabled and CRITICAL >= self.level:
            self._log(CRITICAL, args, kwargs)

    def log(self, level, *args, **kwargs):
        """Logs a :class:`~logbook.LogRecord` with the level set
        to the `level` parameter.  Because custom levels are not
        supported by logbook, this method is mainly used to avoid
        the use of reflection (e.g.: :func:`getattr`) for programmatic
        logging.
        """
        level = lookup_level(level)
        if level >= self.level:
            self._log(level, args, kwargs)

    def catch_exceptions(self, *args, **kwargs):
        """A context manager that catches exceptions and calls
        :meth:`exception` for exceptions caught that way.  Example:

        .. code-block:: python

            with logger.catch_exceptions():
                execute_code_that_might_fail()
        """
        if not args:
            args = ('Uncaught exception occurred',)
        return _ExceptionCatcher(self, args, kwargs)

    def _log(self, level, args, kwargs):
        exc_info = kwargs.pop('exc_info', None)
        extra = kwargs.pop('extra', None)
        self.make_record_and_handle(level, args[0], args[1:], kwargs,
                                    exc_info, extra)


class RecordDispatcher(object):
    """A record dispatcher is the internal base class that implements
    the logic used by the :class:`~logbook.Logger`.
    """

    #: If this is set to `True` the dispatcher information will be suppressed
    #: for log records emitted from this logger.
    suppress_dispatcher = False

    def __init__(self, name=None, level=NOTSET):
        #: the name of the record dispatcher
        self.name = name
        #: list of handlers specific for this record dispatcher
        self.handlers = []
        #: optionally the name of the group this logger belongs to
        self.group = None
        #: the level of the record dispatcher as integer
        self.level = level

    disabled = group_reflected_property('disabled', False)
    level = group_reflected_property('level', NOTSET, fallback=NOTSET)

    def handle(self, record):
        """Call the handlers for the specified record.  This is
        invoked automatically when a record should be handled.
        The default implementation checks if the dispatcher is disabled
        and if the record level is greater than the level of the
        record dispatcher.  In that case it will call the handlers
        (:meth:`call_handlers`).
        """
        if not self.disabled and record.level >= self.level:
            self.call_handlers(record)

    def make_record_and_handle(self, level, msg, args, kwargs, exc_info,
                               extra):
        """Creates a record from some given arguments and heads it
        over to the handling system.
        """
        # The channel information can be useful for some use cases which is
        # why we keep it on there.  The log record however internally will
        # only store a weak reference to the channel, so it might disappear
        # from one instruction to the other.  It will also disappear when
        # a log record is transmitted to another process etc.
        channel = None
        if not self.suppress_dispatcher:
            channel = self

        record = LogRecord(self.name, level, msg, args, kwargs, exc_info,
                           extra, None, channel)

        # after handling the log record is closed which will remove some
        # referenes that would require a GC run on cpython.  This includes
        # the current stack frame, exception information.  However there are
        # some use cases in keeping the records open for a little longer.
        # For example the test handler keeps log records open until the
        # test handler is closed to allow assertions based on stack frames
        # and exception information.
        try:
            self.handle(record)
        finally:
            record.late = True
            if not record.keep_open:
                record.close()

    def call_handlers(self, record):
        """Pass a record to all relevant handlers in the following
        order:

        -   per-dispatcher handlers are handled first
        -   afterwards all the current context handlers in the
            order they were pushed

        Before the first handler is invoked, the record is processed
        (:meth:`process_record`).
        """
        # for performance reasons records are only heavy initialized
        # and processed if at least one of the handlers has a higher
        # level than the record and that handler is not a black hole.
        record_initialized = False

        # Both logger attached handlers as well as context specific
        # handlers are handled one after another.  The latter also
        # include global handlers.
        for handler in chain(self.handlers,
                             Handler.stack_manager.iter_context_objects()):
            # skip records that this handler is not interested in based
            # on the record and handler level or in case this method was
            # overridden on some custom logic.
            if not handler.should_handle(record):
                continue

            # a filter can still veto the handling of the record.  This
            # however is already operating on an initialized and processed
            # record.  The impact is that filters are slower than the
            # handler's should_handle function in case there is no default
            # handler that would handle the record (delayed init).
            if handler.filter is not None \
               and not handler.filter(record, handler):
                continue

            # if this is a blackhole handler, don't even try to
            # do further processing, stop right away.  Technically
            # speaking this is not 100% correct because if the handler
            # is bubbling we shouldn't apply this logic, but then we
            # won't enter this branch anyways.  The result is that a
            # bubbling blackhole handler will never have this shortcut
            # applied and do the heavy init at one point.  This is fine
            # however because a bubbling blackhole handler is not very
            # useful in general.
            if handler.blackhole:
                break

            # we are about to handle the record.  If it was not yet
            # processed by context-specific record processors we
            # have to do that now and remeber that we processed
            # the record already.
            if not record_initialized:
                record.heavy_init()
                self.process_record(record)
                record_initialized = True

            # handle the record.  If the record was handled and
            # the record is not bubbling we can abort now.
            if handler.handle(record) and not handler.bubble:
                break

    def process_record(self, record):
        """Processes the record with all context specific processors.  This
        can be overriden to also inject additional information as necessary
        that can be provided by this record dispatcher.
        """
        if self.group is not None:
            self.group.process_record(record)
        for processor in Processor.stack_manager.iter_context_objects():
            processor.process(record)


class Logger(RecordDispatcher, LoggerMixin):
    """Instances of the Logger class represent a single logging channel.
    A "logging channel" indicates an area of an application. Exactly
    how an "area" is defined is up to the application developer.

    Names used by logbook should be descriptive and are intended for user
    display, not for filtering.  Filtering should happen based on the
    context information instead.

    A logger internally is a subclass of a
    :class:`~logbook.base.RecordDispatcher` that implements the actual
    logic.  If you want to implement a custom logger class, have a look
    at the interface of that class as well.
    """


class LoggerGroup(object):
    """A LoggerGroup represents a group of loggers.  It cannot emit log
    messages on its own but it can be used to set the disabled flag and
    log level of all loggers in the group.

    Furthermore the :meth:`process_record` method of the group is called
    by any logger in the group which by default calls into the
    :attr:`processor` callback function.
    """

    def __init__(self, loggers=None, level=NOTSET, processor=None):
        #: a list of all loggers on the logger group.  Use the
        #: :meth:`add_logger` and :meth:`remove_logger` methods to add
        #: or remove loggers from this list.
        self.loggers = []
        if loggers is not None:
            for logger in loggers:
                self.add_logger(logger)

        #: the level of the group.  This is reflected to the loggers
        #: in the group unless they overrode the setting.
        self.level = lookup_level(level)
        #: the disabled flag for all loggers in the group, unless
        #: the loggers overrode the setting.
        self.disabled = False
        #: an optional callback function that is executed to process
        #: the log records of all loggers in the group.
        self.processor = processor

    def add_logger(self, logger):
        """Adds a logger to this group."""
        assert logger.group is None, 'Logger already belongs to a group'
        logger.group = self
        self.loggers.append(logger)

    def remove_logger(self, logger):
        """Removes a logger from the group."""
        self.loggers.remove(logger)
        logger.group = None

    def process_record(self, record):
        """Like :meth:`Logger.process_record` but for all loggers in
        the group.  By default this calls into the :attr:`processor`
        function is it's not `None`.
        """
        if self.processor is not None:
            self.processor(record)


_default_dispatcher = RecordDispatcher()


def dispatch_record(record):
    """Passes a record on to the handlers on the stack.  This is useful when
    log records are created programmatically and already have all the
    information attached and should be dispatched independent of a logger.
    """
    _default_dispatcher.call_handlers(record)


# at that point we are save to import handler
from logbook.handlers import Handler