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).
This commit is contained in:
Laurent Bachelier 2014-10-07 17:07:48 +02:00
commit 7be9a6468b
12 changed files with 27 additions and 35 deletions

View file

@ -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,

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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")

View file

@ -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

View file

@ -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

View file

@ -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'<br />'
ul_opened = False
for line in backtrace.split('\n'):

View file

@ -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: