From: stefan Date: Fri, 20 Feb 2009 04:38:34 +0000 (+0000) Subject: Enhance and simplify logging. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=f68a850e54ace36a24c797b681fe53b10531ff42;p=roundup.git Enhance and simplify logging. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4151 57a73879-2fb5-44c3-a270-3262357dd7e2 --- diff --git a/roundup/backends/back_mysql.py b/roundup/backends/back_mysql.py index ccf7803..bb233ff 100644 --- a/roundup/backends/back_mysql.py +++ b/roundup/backends/back_mysql.py @@ -1,4 +1,3 @@ -#$Id: back_mysql.py,v 1.75 2008-02-27 08:32:50 richard Exp $ # # Copyright (c) 2003 Martynas Sklyzmantas, Andrey Lebedev # @@ -67,11 +66,10 @@ def db_nuke(config): # stupid MySQL bug requires us to drop all the tables first for table in tables: command = 'DROP TABLE `%s`'%table[0] - if __debug__: - logging.getLogger('hyperdb').debug(command) + self.log_debug(command) cursor.execute(command) command = "DROP DATABASE %s"%config.RDBMS_NAME - logging.getLogger('hyperdb').info(command) + self.log_info(command) cursor.execute(command) conn.commit() conn.close() @@ -85,7 +83,7 @@ def db_create(config): conn = MySQLdb.connect(**kwargs) cursor = conn.cursor() command = "CREATE DATABASE %s"%config.RDBMS_NAME - logging.getLogger('hyperdb').info(command) + self.log_info(command) cursor.execute(command) conn.commit() conn.close() @@ -140,7 +138,7 @@ class Database(Database): def sql_open_connection(self): kwargs = connection_dict(self.config, 'db') - logging.getLogger('hyperdb').info('open database %r'%(kwargs['db'],)) + self.log_info('open database %r'%(kwargs['db'],)) try: conn = MySQLdb.connect(**kwargs) except MySQLdb.OperationalError, message: @@ -544,7 +542,7 @@ class Database(Database): def sql_commit(self, fail_ok=False): ''' Actually commit to the database. ''' - logging.getLogger('hyperdb').info('commit') + self.log_info('commit') # MySQL commits don't seem to ever fail, the latest update winning. # makes you wonder why they have transactions... @@ -558,7 +556,7 @@ class Database(Database): self.sql("START TRANSACTION") def sql_close(self): - logging.getLogger('hyperdb').info('close') + self.log_info('close') try: self.conn.close() except MySQLdb.ProgrammingError, message: diff --git a/roundup/backends/rdbms_common.py b/roundup/backends/rdbms_common.py index b5ce677..02a5d7e 100644 --- a/roundup/backends/rdbms_common.py +++ b/roundup/backends/rdbms_common.py @@ -15,7 +15,6 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -#$Id: rdbms_common.py,v 1.199 2008-08-18 06:25:47 richard Exp $ """ Relational database (SQL) backend common code. Basics: @@ -157,8 +156,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): def sql(self, sql, args=None): """ Execute the sql with the optional args. """ - if __debug__: - logging.getLogger('hyperdb').debug('SQL %r %r'%(sql, args)) + self.log_debug('SQL %r %r'%(sql, args)) if args: self.cursor.execute(sql, args) else: @@ -262,8 +260,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): return 0 if version < 2: - if __debug__: - logging.getLogger('hyperdb').info('upgrade to version 2') + log_info('upgrade to version 2') # change the schema structure self.database_schema = {'tables': self.database_schema} @@ -276,8 +273,7 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): self.create_version_2_tables() if version < 3: - if __debug__: - logging.getLogger('hyperdb').info('upgrade to version 3') + log_info('upgrade to version 3') self.fix_version_2_tables() if version < 4: @@ -775,9 +771,8 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): def addnode(self, classname, nodeid, node): """ Add the specified node to its class's db. """ - if __debug__: - logging.getLogger('hyperdb').debug('addnode %s%s %r'%(classname, - nodeid, node)) + self.log_debug('addnode %s%s %r'%(classname, + nodeid, node)) # determine the column definitions and multilink tables cl = self.classes[classname] @@ -850,9 +845,8 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): def setnode(self, classname, nodeid, values, multilink_changes={}): """ Change the specified node. """ - if __debug__: - logging.getLogger('hyperdb').debug('setnode %s%s %r' - % (classname, nodeid, values)) + self.log_debug('setnode %s%s %r' + % (classname, nodeid, values)) # clear this node out of the cache if it's in there key = (classname, nodeid) @@ -1113,9 +1107,8 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): # create the journal entry cols = 'nodeid,date,tag,action,params' - if __debug__: - logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%(classname, - nodeid, journaldate, journaltag, action, params)) + self.log_debug('addjournal %s%s %r %s %s %r'%(classname, + nodeid, journaldate, journaltag, action, params)) # make the journalled data marshallable if isinstance(params, type({})): @@ -1140,10 +1133,9 @@ class Database(FileStorage, hyperdb.Database, roundupdb.Database): dc = self.hyperdb_to_sql_value[hyperdb.Date] for nodeid, journaldate, journaltag, action, params in journal: - if __debug__: - logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%( - classname, nodeid, journaldate, journaltag, action, - params)) + self.log_debug('addjournal %s%s %r %s %s %r'%( + classname, nodeid, journaldate, journaltag, action, + params)) # make the journalled data marshallable if isinstance(params, type({})): @@ -2114,7 +2106,7 @@ class Class(hyperdb.Class): backward-compatibility reasons a single (dir, prop) tuple is also allowed. - "search_matches" is a sequence type or None + "search_matches" is a container type or None The filter must match all properties specificed. If the property value to match is a list: diff --git a/roundup/roundupdb.py b/roundup/roundupdb.py index 1ddc30a..a129480 100644 --- a/roundup/roundupdb.py +++ b/roundup/roundupdb.py @@ -25,6 +25,7 @@ __docformat__ = 'restructuredtext' import re, os, smtplib, socket, time, random import cStringIO, base64, quopri, mimetypes import os.path +import logging from rfc2822 import encode_header @@ -115,6 +116,29 @@ class Database: return userid + def log_debug(self, msg, *args, **kwargs): + """Log a message with level DEBUG.""" + + logger = self.get_logger() + logger.debug(msg, *args, **kwargs) + + def log_info(self, msg, *args, **kwargs): + """Log a message with level INFO.""" + + logger = self.get_logger() + logger.info(msg, *args, **kwargs) + + def get_logger(self): + """Return the logger for this database.""" + + # Because getting a logger requires acquiring a lock, we want + # to do it only once. + if not hasattr(self, '__logger'): + self.__logger = logging.getLogger('hyperdb') + + return self.__logger + + class DetectorError(RuntimeError): """ Raised by detectors that want to indicate that something's amiss """