Skip to content

Commit

Permalink
Added wrapper handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Sep 20, 2010
1 parent 6aff229 commit 3250265
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docs/api/handlers.rst
Expand Up @@ -57,6 +57,9 @@ Core Handlers
.. autoclass:: NullHandler
:members:

.. autoclass:: WrapperHandler
:members:

.. autofunction:: create_syshandler

Mixin Classes
Expand Down
3 changes: 2 additions & 1 deletion logbook/__init__.py
Expand Up @@ -17,7 +17,8 @@
MonitoringFileHandler, StderrHandler, RotatingFileHandler, \
TimedRotatingFileHandler, TestHandler, MailHandler, SyslogHandler, \
NullHandler, NTEventLogHandler, create_syshandler, StringFormatter, \
StringFormatterHandlerMixin, HashingHandlerMixin, LimitingHandlerMixin
StringFormatterHandlerMixin, HashingHandlerMixin, \
LimitingHandlerMixin, WrapperHandler


# create an anonymous default logger and provide all important
Expand Down
26 changes: 26 additions & 0 deletions logbook/handlers.py
Expand Up @@ -243,6 +243,32 @@ class NullHandler(Handler):
blackhole = True


class WrapperHandler(Handler):
"""A class that can wrap another handler and redirect all calls to the
wrapped handler::
handler = WrapperHandler(other_handler)
Subclasses should override the :attr:`_direct_attrs` attribute as
necessary.
"""

#: a set of direct attributes that are not forwarded to the inner
#: handler. This has to be extended as necessary.
_direct_attrs = frozenset(['handler'])

def __init__(self, handler):
self.handler = handler

def __getattr__(self, name):
return getattr(self.handler, name)

def __setattr__(self, name, value):
if name in self._direct_attrs:
return Handler.__setattr__(self, name, value)
setattr(self.handler, name, value)


class StringFormatter(object):
"""Many handlers format the log entries to text format. This is done
by a callable that is passed a log record and returns an unicode
Expand Down
14 changes: 3 additions & 11 deletions logbook/queues.py
Expand Up @@ -12,7 +12,7 @@
from Queue import Empty, Queue as ThreadQueue
from itertools import cycle
from logbook.base import NOTSET, LogRecord, dispatch_record
from logbook.handlers import Handler
from logbook.handlers import Handler, WrapperHandler
from logbook.helpers import json


Expand Down Expand Up @@ -363,7 +363,7 @@ def _target(self):
self.wrapper_handler.handler.emit(record)


class ThreadedWrapperHandler(Handler):
class ThreadedWrapperHandler(WrapperHandler):
"""This handled uses a single background thread to dispatch log records
to a specific other handler using an internal queue. The idea is that if
you are using a handler that requires some time to hand off the log records
Expand All @@ -382,19 +382,11 @@ class ThreadedWrapperHandler(Handler):
_direct_attrs = frozenset(['handler', 'queue', 'controller'])

def __init__(self, handler):
self.handler = handler
WrapperHandler.__init__(self, handler)
self.queue = ThreadQueue(-1)
self.controller = TWHThreadController(self)
self.controller.start()

def __getattr__(self, name):
return getattr(self.handler, name)

def __setattr__(self, name, value):
if name in self._direct_attrs:
return Handler.__setattr__(self, name, value)
setattr(self.handler, name, value)

def close(self):
self.controller.stop()
self.handler.close()
Expand Down

0 comments on commit 3250265

Please sign in to comment.