From 5bb32fadce5aa6e78f95a5051cb7eae2a78d051d Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 23 May 2013 19:03:40 +0400 Subject: [PATCH] Use fallback logger if normal one is not available --- powerline/__init__.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/powerline/__init__.py b/powerline/__init__.py index 0e03aaf1..53ffd932 100644 --- a/powerline/__init__.py +++ b/powerline/__init__.py @@ -94,6 +94,29 @@ class PowerlineLogger(object): self._log('debug', msg, *args, **kwargs) +_fallback_logger = None + + +def _get_fallback_logger(): + global _fallback_logger + if _fallback_logger: + return _fallback_logger + + log_format = '%(asctime)s:%(levelname)s:%(message)s' + formatter = logging.Formatter(log_format) + + level = logging.WARNING + handler = logging.StreamHandler() + handler.setLevel(level) + handler.setFormatter(formatter) + + logger = logging.getLogger('powerline') + logger.setLevel(level) + logger.addHandler(handler) + _fallback_logger = PowerlineLogger(None, logger, '_fallback_') + return _fallback_logger + + class Powerline(object): '''Main powerline class, entrance point for all powerline uses. Sets powerline up and loads the configuration. @@ -465,7 +488,8 @@ class Powerline(object): def exception(self, msg, *args, **kwargs): if 'prefix' not in kwargs: kwargs['prefix'] = 'powerline' - if self.pl: + try: return self.pl.exception(msg, *args, **kwargs) - else: - raise + except AttributeError: + pl = _get_fallback_logger() + return pl.exception(msg, *args, **kwargs)