From a1a58c7c32a0986040690b0c602ebf050c656698 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 30 Oct 2010 16:09:02 +0200 Subject: [PATCH] add --logging-file parameter, and color stdout lines depending of level --- weboob/tools/application/base.py | 37 +++++++++++++++++--- weboob/tools/log.py | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 weboob/tools/log.py diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index 2c90be7a..99447a41 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -25,6 +25,7 @@ from weboob.capabilities.base import NotAvailable, NotLoaded from weboob.core import Weboob, CallErrors from weboob.tools.config.iconfig import ConfigError from weboob.tools.backend import ObjectNotAvailable +from weboob.tools.log import createColoredFormatter, getLogger __all__ = ['BackendNotFound', 'BaseApplication', 'ConfigError'] @@ -120,6 +121,7 @@ class BaseApplication(object): # ------ BaseApplication methods ------------------------------- def __init__(self, option_parser=None): + self.logger = getLogger(self.APPNAME) self.weboob = self.create_weboob() self.config = None self.options = None @@ -265,6 +267,9 @@ class BaseApplication(object): from weboob.tools.browser import BaseBrowser BaseBrowser.SAVE_RESPONSES = True + for handler in logging.root.handlers: + logging.root.removeHandler(handler) + if self.options.debug: level = logging.DEBUG elif self.options.verbose: @@ -274,13 +279,35 @@ class BaseApplication(object): else: level = logging.WARNING - log_format = '%(asctime)s:%(levelname)s:%(pathname)s:%(lineno)d:%(funcName)s %(message)s' + logging.root.setLevel(level) + + # file logger if self.options.logging_file: - print self.options.logging_file - logging.basicConfig(filename=self.options.logging_file, level=level, format=log_format) + try: + stream = open(self.options.logging_file, 'w') + except IOError, e: + self.logger.error('Unable to create the logging file: %s' % e) + sys.exit(1) + else: + format = '%(asctime)s:%(levelname)s:%(name)s:%(pathname)s:%(lineno)d:%(funcName)s %(message)s' + handler = logging.StreamHandler(stream) + handler.setLevel(level) + handler.setFormatter(logging.Formatter(format)) + logging.root.addHandler(handler) else: - logging.basicConfig(stream=sys.stdout, level=level, format=log_format) - logging.root.level = level + # stdout logger + format = '%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(funcName)s %(message)s' + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(createColoredFormatter(sys.stdout, format)) + handler.setLevel(level) + logging.root.addHandler(handler) + + #log_format = '%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(funcName)s %(message)s' + #if self.options.logging_file: + # print self.options.logging_file + # logging.basicConfig(filename=self.options.logging_file, level=level, format=log_format) + #else: + # logging.basicConfig(stream=sys.stdout, level=level, format=log_format) self._handle_options() self.handle_application_options() diff --git a/weboob/tools/log.py b/weboob/tools/log.py new file mode 100644 index 00000000..7a03a09a --- /dev/null +++ b/weboob/tools/log.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Romain Bignon +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +from logging import Formatter, getLogger as _getLogger +import sys + + +__all__ = ['getLogger', 'createColoredFormatter'] + + +RESET_SEQ = "\033[0m" +COLOR_SEQ = "%s%%s" + RESET_SEQ + +COLORS = { + 'DEBUG': COLOR_SEQ % "\033[36m", + 'INFO': "%s", + 'WARNING': COLOR_SEQ % "\033[1;1m", + 'ERROR': COLOR_SEQ % "\033[1;31m", + 'CRITICAL': COLOR_SEQ % ("\033[1;33m\033[1;41m"), +} + +def getLogger(name, parent=None): + if parent: + name = parent.name + '.' + name + return _getLogger(name) + +class ColoredFormatter(Formatter): + """ + Class written by airmind: + http://stackoverflow.com/questions/384076/how-can-i-make-the-python-logging-output-to-be-colored + """ + def format(self, record): + levelname = record.levelname + msg = Formatter.format(self, record) + if levelname in COLORS: + msg = COLORS[levelname] % msg + return msg + +def createColoredFormatter(stream, format): + if (sys.platform != 'win32') and stream.isatty(): + return ColoredFormatter(format) + else: + return Formatter(format) +