From 7be9a6468b04172461b0ae0bb9d068e5a91ccd14 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Tue, 7 Oct 2014 17:07:48 +0200 Subject: [PATCH] logging: Create a new level DEBUG_FILTERS This is a cleaner approach that requires less configuration in other applications. This also easily allows us to have another color. Many checks were made on being exactly at the DEBUG level, they were fixed to also check on being below DEBUG (i.e. DEBUG_FILTERS). --- weboob/applications/monboob/monboob.py | 2 +- weboob/applications/qboobmsg/messages_manager.py | 2 +- weboob/applications/qhavedate/contacts.py | 8 ++++---- .../applications/qwebcontentedit/main_window.py | 6 +++--- weboob/browser/elements.py | 4 ++-- weboob/browser/filters/standard.py | 4 ++-- weboob/core/modules.py | 2 +- weboob/deprecated/browser/browser.py | 2 +- weboob/tools/application/base.py | 12 +++++------- weboob/tools/application/console.py | 2 +- weboob/tools/application/qt/qt.py | 2 +- weboob/tools/log.py | 16 +++++----------- 12 files changed, 27 insertions(+), 35 deletions(-) diff --git a/weboob/applications/monboob/monboob.py b/weboob/applications/monboob/monboob.py index 11e45d2e..c7d34f72 100644 --- a/weboob/applications/monboob/monboob.py +++ b/weboob/applications/monboob/monboob.py @@ -240,7 +240,7 @@ class Monboob(ReplApplication): except Exception as e: content = u'Unable to send message to %s:\n' % thread_id content += u'\n\t%s\n' % to_unicode(e) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += u'\n%s\n' % to_unicode(get_backtrace(e)) self.send_email(backend, Message(thread, 0, diff --git a/weboob/applications/qboobmsg/messages_manager.py b/weboob/applications/qboobmsg/messages_manager.py index da3fc121..12aa71f1 100644 --- a/weboob/applications/qboobmsg/messages_manager.py +++ b/weboob/applications/qboobmsg/messages_manager.py @@ -273,7 +273,7 @@ class MessagesManager(QWidget): def _postReply_eb(self, backend, error, backtrace): content = unicode(self.tr('Unable to send message:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while posting reply'), content, QMessageBox.Ok) diff --git a/weboob/applications/qhavedate/contacts.py b/weboob/applications/qhavedate/contacts.py index f4838984..c716316b 100644 --- a/weboob/applications/qhavedate/contacts.py +++ b/weboob/applications/qhavedate/contacts.py @@ -193,7 +193,7 @@ class ContactThread(QWidget): def _postReply_eb(self, backend, error, backtrace): content = unicode(self.tr('Unable to send message:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while posting reply'), content, QMessageBox.Ok) @@ -378,7 +378,7 @@ class ContactNotes(QWidget): self.ui.textEdit.setEnabled(True) self.ui.saveButton.setEnabled(True) content = unicode(self.tr('Unable to load notes:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while loading notes'), content, QMessageBox.Ok) @@ -400,7 +400,7 @@ class ContactNotes(QWidget): self.ui.saveButton.setEnabled(True) self.ui.textEdit.setEnabled(True) content = unicode(self.tr('Unable to save notes:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while saving notes'), content, QMessageBox.Ok) @@ -592,7 +592,7 @@ class ContactsWidget(QWidget): def retrieveContact_eb(self, backend, error, backtrace): content = unicode(self.tr('Unable to get contact:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += u'\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while getting contact'), content, QMessageBox.Ok) diff --git a/weboob/applications/qwebcontentedit/main_window.py b/weboob/applications/qwebcontentedit/main_window.py index cded3645..64742ce9 100644 --- a/weboob/applications/qwebcontentedit/main_window.py +++ b/weboob/applications/qwebcontentedit/main_window.py @@ -145,7 +145,7 @@ class MainWindow(QtMainWindow): def _errorLoadPage(self, backend, error, backtrace): """ Error callback for loadPage """ content = unicode(self.tr('Unable to load page:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while loading page'), content, QMessageBox.Ok) @@ -186,7 +186,7 @@ class MainWindow(QtMainWindow): def _errorSavePage(self, backend, error, backtrace): """ """ content = unicode(self.tr('Unable to save page:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while saving page'), content, QMessageBox.Ok) @@ -264,7 +264,7 @@ class MainWindow(QtMainWindow): return content = unicode(self.tr('Unable to load history:\n%s\n')) % to_unicode(error) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: content += '\n%s\n' % to_unicode(backtrace) QMessageBox.critical(self, self.tr('Error while loading history'), content, QMessageBox.Ok) diff --git a/weboob/browser/elements.py b/weboob/browser/elements.py index 2934b174..f4b65009 100644 --- a/weboob/browser/elements.py +++ b/weboob/browser/elements.py @@ -21,7 +21,7 @@ import re import sys from copy import deepcopy -from weboob.tools.log import getLogger +from weboob.tools.log import getLogger, DEBUG_FILTERS from weboob.tools.ordereddict import OrderedDict from weboob.browser.pages import NextPage @@ -270,7 +270,7 @@ class ItemElement(AbstractElement): self.logger.warning('Attribute %s raises %s' % (key, repr(e))) raise logger = getLogger('b2filters') - logger.debug("%s.%s = %r" % (self._random_id, key, value)) + logger.log(DEBUG_FILTERS, "%s.%s = %r" % (self._random_id, key, value)) setattr(self.obj, key, value) diff --git a/weboob/browser/filters/standard.py b/weboob/browser/filters/standard.py index 3258420c..ec8a61b7 100644 --- a/weboob/browser/filters/standard.py +++ b/weboob/browser/filters/standard.py @@ -30,7 +30,7 @@ from weboob.capabilities.base import empty from weboob.tools.compat import basestring from weboob.exceptions import ParseError from weboob.browser.url import URL -from weboob.tools.log import getLogger +from weboob.tools.log import getLogger, DEBUG_FILTERS class NoDefault(object): def __repr__(self): @@ -130,7 +130,7 @@ def debug(*args): continue result += ", %s=%r" % (arg, getattr(self, arg)) result += u')' - logger.debug(result) + logger.log(DEBUG_FILTERS, result) res = function(self, value) return res return print_debug diff --git a/weboob/core/modules.py b/weboob/core/modules.py index 25ad2164..1ad0a7e2 100644 --- a/weboob/core/modules.py +++ b/weboob/core/modules.py @@ -148,7 +148,7 @@ class ModulesLoader(object): if fp: fp.close() except Exception as e: - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: self.logger.exception(e) raise ModuleLoadError(module_name, e) diff --git a/weboob/deprecated/browser/browser.py b/weboob/deprecated/browser/browser.py index 3fdc8f9a..d3a55d79 100644 --- a/weboob/deprecated/browser/browser.py +++ b/weboob/deprecated/browser/browser.py @@ -206,7 +206,7 @@ class StandardBrowser(mechanize.Browser): # display messages from httplib self.set_debug_http(True) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: # Enable log messages from mechanize.Browser self.set_debug_redirects(True) mech_logger = logging.getLogger("mechanize") diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index 918d0fe7..21683c65 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -33,7 +33,7 @@ from weboob.core import Weboob, CallErrors from weboob.core.backendscfg import BackendsConfig from weboob.tools.config.iconfig import ConfigError from weboob.exceptions import FormFieldConversionWarning -from weboob.tools.log import createColoredFormatter, getLogger, DebugFilter, settings as log_settings +from weboob.tools.log import createColoredFormatter, getLogger, DEBUG_FILTERS, settings as log_settings from weboob.tools.misc import to_unicode from .results import ResultsConditionError @@ -314,7 +314,7 @@ class Application(object): return False print(u'Error(%s): %s' % (backend.name, error), file=self.stderr) - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: print(backtrace, file=self.stderr) else: return True @@ -354,7 +354,9 @@ class Application(object): print(' '.join(items)) sys.exit(0) - if self.options.debug or self.options.save_responses: + if self.options.debug >= self.DEBUG_FILTER: + level = DEBUG_FILTERS + elif self.options.debug or self.options.save_responses: level = logging.DEBUG elif self.options.verbose: level = logging.INFO @@ -391,10 +393,6 @@ class Application(object): self._handle_options() self.handle_application_options() - if self.options.debug < self.DEBUG_FILTER: - for handler in handlers: - handler.addFilter(DebugFilter()) - return args @classmethod diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index e8691d8f..c62c98ce 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -572,7 +572,7 @@ class ConsoleApplication(Application): print('New version of module %s has been installed. Retry to call the command.' % minfo.name) return - if logging.root.level == logging.DEBUG: + if logging.root.level <= logging.DEBUG: print(backtrace, file=self.stderr) else: return True diff --git a/weboob/tools/application/qt/qt.py b/weboob/tools/application/qt/qt.py index 11d2d2b0..d084caa4 100644 --- a/weboob/tools/application/qt/qt.py +++ b/weboob/tools/application/qt/qt.py @@ -223,7 +223,7 @@ class QtDo(QObject): elif isinstance(error, UserError): if not msg: msg = type(error).__name__ - elif logging.root.level == logging.DEBUG: + elif logging.root.level <= logging.DEBUG: msg += u'
' ul_opened = False for line in backtrace.split('\n'): diff --git a/weboob/tools/log.py b/weboob/tools/log.py index 1577a416..58bd43c5 100644 --- a/weboob/tools/log.py +++ b/weboob/tools/log.py @@ -21,7 +21,7 @@ from __future__ import print_function import sys from collections import defaultdict -from logging import Filter, Formatter, getLogger as _getLogger +from logging import addLevelName, Formatter, getLogger as _getLogger __all__ = ['getLogger', 'createColoredFormatter', 'settings'] @@ -35,8 +35,12 @@ COLORS = { 'WARNING': COLOR_SEQ % "\033[1;33m", 'ERROR': COLOR_SEQ % "\033[1;31m", 'CRITICAL': COLOR_SEQ % ("\033[1;33m\033[1;41m"), + 'DEBUG_FILTERS': COLOR_SEQ % "\033[0;35m", } +DEBUG_FILTERS = 8 +addLevelName(DEBUG_FILTERS, 'DEBUG_FILTERS') + # Global settings f logger. settings = defaultdict(lambda: None) @@ -50,16 +54,6 @@ def getLogger(name, parent=None): return logger -class DebugFilter(Filter): - """ - Allow a fine filtering of debug output - """ - def filter(self, record): - if record.name == "b2filters": - return False - return True - - class ColoredFormatter(Formatter): """ Class written by airmind: