[MERGE+IMP] logging: merged xrg's branch to set selective logger level + added new 'debug_sql' loglevel to avoid having to modify sql_db.py to enable SQL logging

bzr revid: odo@openerp.com-20100804134900-dfupojmgcj0rjhd2
This commit is contained in:
P. Christeas, Olivier Dony 2010-08-04 15:49:00 +02:00 committed by Olivier Dony
commit 7666ac14a7
4 changed files with 23 additions and 14 deletions

View File

@ -128,6 +128,7 @@ class ExportService(object):
raise
LOG_NOTSET = 'notset'
LOG_DEBUG_SQL = 'debug_sql'
LOG_DEBUG_RPC = 'debug_rpc'
LOG_DEBUG = 'debug'
LOG_TEST = 'test'
@ -138,6 +139,8 @@ LOG_CRITICAL = 'critical'
logging.DEBUG_RPC = logging.DEBUG - 2
logging.addLevelName(logging.DEBUG_RPC, 'DEBUG_RPC')
logging.DEBUG_SQL = logging.DEBUG_RPC - 2
logging.addLevelName(logging.DEBUG_SQL, 'DEBUG_SQL')
logging.TEST = logging.INFO - 5
logging.addLevelName(logging.TEST, 'TEST')
@ -150,6 +153,7 @@ COLOR_SEQ = "\033[1;%dm"
BOLD_SEQ = "\033[1m"
COLOR_PATTERN = "%s%s%%s%s" % (COLOR_SEQ, COLOR_SEQ, RESET_SEQ)
LEVEL_COLOR_MAPPING = {
logging.DEBUG_SQL: (WHITE, MAGENTA),
logging.DEBUG_RPC: (BLUE, WHITE),
logging.DEBUG: (BLUE, DEFAULT),
logging.INFO: (GREEN, DEFAULT),
@ -215,7 +219,6 @@ def init_logger():
logger.setLevel(int(tools.config['log_level'] or '0'))
class Logger(object):
def __init__(self):
warnings.warn("The netsvc.Logger API shouldn't be used anymore, please "
@ -263,8 +266,11 @@ class Logger(object):
# better ignore the exception and carry on..
pass
def set_loglevel(self, level):
log = logging.getLogger()
def set_loglevel(self, level, logger=None):
if logger is not None:
log = logging.getLogger(str(logger))
else:
log = logging.getLogger()
log.setLevel(logging.INFO) # make sure next msg is printed
log.info("Log level changed to %s" % logging.getLevelName(level))
log.setLevel(level)

View File

@ -552,9 +552,9 @@ GNU Public Licence.
def exp_login_message(self):
return tools.config.get('login_message', False)
def exp_set_loglevel(self,loglevel):
def exp_set_loglevel(self, loglevel, logger=None):
l = netsvc.Logger()
l.set_loglevel(int(loglevel))
l.set_loglevel(int(loglevel), logger)
return True
def exp_get_stats(self):

View File

@ -77,7 +77,11 @@ class Cursor(object):
def __init__(self, pool, dbname, serialized=False):
self.sql_from_log = {}
self.sql_into_log = {}
self.sql_log = False
# default log level determined at cursor creation, could be
# overridden later for debugging purposes
self.sql_log = self.__logger.isEnabledFor(logging.DEBUG_SQL)
self.sql_log_count = 0
self.__closed = True # avoid the call of close() (by __del__) if an exception
# is raised by any of the following initialisations
@ -128,7 +132,7 @@ class Cursor(object):
delay = mdt.now() - now
delay = delay.seconds * 1E6 + delay.microseconds
self.__logger.debug("query: %s", self._obj.query)
self.__logger.log(logging.DEBUG_SQL, "query: %s", self._obj.query)
self.sql_log_count+=1
res_from = re_from.match(query.lower())
if res_from:
@ -159,15 +163,15 @@ class Cursor(object):
if sqllogs[type]:
sqllogitems = sqllogs[type].items()
sqllogitems.sort(key=lambda k: k[1][1])
self.__logger.debug("SQL LOG %s:", type)
self.__logger.log(logging.DEBUG_SQL, "SQL LOG %s:", type)
for r in sqllogitems:
delay = timedelta(microseconds=r[1][1])
self.__logger.debug("table: %s: %s/%s",
self.__logger.log(logging.DEBUG_SQL, "table: %s: %s/%s",
r[0], delay, r[1][0])
sum+= r[1][1]
sqllogs[type].clear()
sum = timedelta(microseconds=sum)
self.__logger.debug("SUM %s:%s/%d [%d]",
self.__logger.log(logging.DEBUG_SQL, "SUM %s:%s/%d [%d]",
type, sum, self.sql_log_count, sql_counter)
sqllogs[type].clear()
process('from')
@ -251,8 +255,7 @@ class ConnectionPool(object):
return "ConnectionPool(used=%d/count=%d/max=%d)" % (used, count, self._maxconn)
def _debug(self, msg, *args):
msg = '%r ' + msg
self.__logger.debug(msg, self, *args)
self.__logger.log(logging.DEBUG_SQL, ('%r ' + msg), self, *args)
@locked
def borrow(self, dsn):
@ -323,7 +326,7 @@ class Connection(object):
def cursor(self, serialized=False):
cursor_type = serialized and 'serialized ' or ''
self.__logger.debug('create %scursor to %r', cursor_type, self.dbname)
self.__logger.log(logging.DEBUG_SQL, 'create %scursor to %r', cursor_type, self.dbname)
return Cursor(self._pool, self.dbname, serialized=serialized)
def serialized_cursor(self):

View File

@ -92,7 +92,7 @@ class configmanager(object):
self.has_ssl = check_ssl()
self._LOGLEVELS = dict([(getattr(netsvc, 'LOG_%s' % x), getattr(logging, x))
for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'TEST', 'DEBUG', 'DEBUG_RPC', 'NOTSET')])
for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'TEST', 'DEBUG', 'DEBUG_RPC', 'DEBUG_SQL', 'NOTSET')])
version = "%s %s" % (release.description, release.version)
self.parser = parser = optparse.OptionParser(version=version)