class ConnectionLogger(object): """Connection-specific logger object. This provides a way to add some specific information to each log message without generated for a connection without creating a separately-managed logger object using the logging module. Managed logger objects are never discarded (since they're stored in a global table in the logging module), but these are discarded when the corresponding connection object is discarded. """ __slots__ = "__logger", "__prefix" def __init__(self, logger, prefix): if "%" in prefix: raise ValueError("message prefix may not contain '%'") self.__logger = logger self.__prefix = prefix def debug(self, msg, *args, **kw): self.log(logging.DEBUG, msg, *args, **kw) def info(self, msg, *args, **kw): self.log(logging.INFO, msg, *args, **kw) def warning(self, msg, *args, **kw): self.log(logging.WARNING, msg, *args, **kw) warn = warning def error(self, msg, *args, **kw): self.log(logging.ERROR, msg, *args, **kw) def critical(self, msg, *args, **kw): self.log(logging.CRITICAL, msg, *args, **kw) fatal = critical def exception(self, msg, *args): self.__logger.exception(self.__prefix + msg, *args) def log(self, level, msg, *args, **kw): self.__logger.log(level, self.__prefix + msg, *args, **kw)