KOMBU_LOG_CHANNEL now logs every channel method call

This commit is contained in:
Ask Solem 2011-03-22 16:29:18 +01:00
parent 080e0db4b9
commit 245a96aa7d
4 changed files with 77 additions and 1 deletions

View File

@ -57,3 +57,8 @@ if not os.environ.get("KOMBU_NO_EVAL", False):
"__homepage__": __homepage__,
"__docformat__": __docformat__,
"VERSION": VERSION})
if os.environ.get("KOMBU_LOG_DEBUG"):
os.environ["KOMBU_LOG_CHANNEL"] = "1"
from kombu.utils import debug
debug.setup_logging()

View File

@ -8,6 +8,7 @@ Broker connection and pools.
:license: BSD, see LICENSE for more details.
"""
import os
import socket
from copy import copy
@ -96,7 +97,12 @@ class BrokerConnection(object):
def channel(self):
"""Request a new channel."""
return self.transport.create_channel(self.connection)
chan = self.transport.create_channel(self.connection)
if os.environ.get("KOMBU_LOG_CHANNEL", False):
from kombu.utils.debug import Logwrapped
return Logwrapped(chan, "kombu.channel",
"[Kombu channel:%(channel_id)s] ")
return chan
def drain_events(self, **kwargs):
"""Wait for a single event from the server.

50
kombu/utils/debug.py Normal file
View File

@ -0,0 +1,50 @@
import logging
from kombu.utils.functional import wraps
from kombu.utils.log import get_logger
def setup_logging(loglevel=logging.DEBUG):
root = logging.getLogger()
if not root.handlers:
root.addHandler(logging.StreamHandler())
root.setLevel(loglevel)
class Logwrapped(object):
def __init__(self, instance, logger=None, ident=None):
self.instance = instance
self.logger = get_logger(logger)
self.ident = ident
def __getattr__(self, key):
meth = getattr(self.instance, key)
if not callable(meth):
return meth
@wraps(meth)
def __wrapped(*args, **kwargs):
info = ""
if self.ident:
info += self.ident % vars(self.instance)
info += "%s(" % (meth.__name__, )
if args:
info += ", ".join(map(repr, args))
if kwargs:
if args:
info += ", "
info += ", ".join("%s=%r" % (key, value)
for key, value in kwargs.iteritems())
info += ")"
self.logger.debug(info)
return meth(*args, **kwargs)
return __wrapped
def __repr__(self):
return repr(self.instance)
def __dir__(self):
return dir(self.instance)

15
kombu/utils/log.py Normal file
View File

@ -0,0 +1,15 @@
import logging
class NullHandler(logging.Handler):
def emit(self, record):
pass
def get_logger(logger):
if isinstance(logger, basestring):
logger = logging.getLogger(logger)
if not logger.handlers:
logger.addHandler(NullHandler())
return logger