add --logging-file parameter, and color stdout lines depending of level

This commit is contained in:
Romain Bignon 2010-10-30 16:09:02 +02:00
commit a1a58c7c32
2 changed files with 90 additions and 5 deletions

View file

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

58
weboob/tools/log.py Normal file
View file

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