Skip to content

Commit 19adf85

Browse files
committed
Enhance and simplify logging.
1 parent 4654ec8 commit 19adf85

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

roundup/backends/back_mysql.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#$Id: back_mysql.py,v 1.75 2008-02-27 08:32:50 richard Exp $
21
#
32
# Copyright (c) 2003 Martynas Sklyzmantas, Andrey Lebedev <[email protected]>
43
#
@@ -67,11 +66,10 @@ def db_nuke(config):
6766
# stupid MySQL bug requires us to drop all the tables first
6867
for table in tables:
6968
command = 'DROP TABLE `%s`'%table[0]
70-
if __debug__:
71-
logging.getLogger('hyperdb').debug(command)
69+
self.log_debug(command)
7270
cursor.execute(command)
7371
command = "DROP DATABASE %s"%config.RDBMS_NAME
74-
logging.getLogger('hyperdb').info(command)
72+
self.log_info(command)
7573
cursor.execute(command)
7674
conn.commit()
7775
conn.close()
@@ -85,7 +83,7 @@ def db_create(config):
8583
conn = MySQLdb.connect(**kwargs)
8684
cursor = conn.cursor()
8785
command = "CREATE DATABASE %s"%config.RDBMS_NAME
88-
logging.getLogger('hyperdb').info(command)
86+
self.log_info(command)
8987
cursor.execute(command)
9088
conn.commit()
9189
conn.close()
@@ -140,7 +138,7 @@ class Database(Database):
140138

141139
def sql_open_connection(self):
142140
kwargs = connection_dict(self.config, 'db')
143-
logging.getLogger('hyperdb').info('open database %r'%(kwargs['db'],))
141+
self.log_info('open database %r'%(kwargs['db'],))
144142
try:
145143
conn = MySQLdb.connect(**kwargs)
146144
except MySQLdb.OperationalError, message:
@@ -544,7 +542,7 @@ def create_class(self, spec):
544542
def sql_commit(self, fail_ok=False):
545543
''' Actually commit to the database.
546544
'''
547-
logging.getLogger('hyperdb').info('commit')
545+
self.log_info('commit')
548546

549547
# MySQL commits don't seem to ever fail, the latest update winning.
550548
# makes you wonder why they have transactions...
@@ -558,7 +556,7 @@ def sql_commit(self, fail_ok=False):
558556
self.sql("START TRANSACTION")
559557

560558
def sql_close(self):
561-
logging.getLogger('hyperdb').info('close')
559+
self.log_info('close')
562560
try:
563561
self.conn.close()
564562
except MySQLdb.ProgrammingError, message:

roundup/backends/rdbms_common.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: rdbms_common.py,v 1.199 2008-08-18 06:25:47 richard Exp $
1918
""" Relational database (SQL) backend common code.
2019
2120
Basics:
@@ -157,8 +156,7 @@ def open_connection(self):
157156
def sql(self, sql, args=None):
158157
""" Execute the sql with the optional args.
159158
"""
160-
if __debug__:
161-
logging.getLogger('hyperdb').debug('SQL %r %r'%(sql, args))
159+
self.log_debug('SQL %r %r'%(sql, args))
162160
if args:
163161
self.cursor.execute(sql, args)
164162
else:
@@ -262,8 +260,7 @@ def upgrade_db(self):
262260
return 0
263261

264262
if version < 2:
265-
if __debug__:
266-
logging.getLogger('hyperdb').info('upgrade to version 2')
263+
log_info('upgrade to version 2')
267264
# change the schema structure
268265
self.database_schema = {'tables': self.database_schema}
269266

@@ -276,8 +273,7 @@ def upgrade_db(self):
276273
self.create_version_2_tables()
277274

278275
if version < 3:
279-
if __debug__:
280-
logging.getLogger('hyperdb').info('upgrade to version 3')
276+
log_info('upgrade to version 3')
281277
self.fix_version_2_tables()
282278

283279
if version < 4:
@@ -775,9 +771,8 @@ def clear(self):
775771
def addnode(self, classname, nodeid, node):
776772
""" Add the specified node to its class's db.
777773
"""
778-
if __debug__:
779-
logging.getLogger('hyperdb').debug('addnode %s%s %r'%(classname,
780-
nodeid, node))
774+
self.log_debug('addnode %s%s %r'%(classname,
775+
nodeid, node))
781776

782777
# determine the column definitions and multilink tables
783778
cl = self.classes[classname]
@@ -850,9 +845,8 @@ def addnode(self, classname, nodeid, node):
850845
def setnode(self, classname, nodeid, values, multilink_changes={}):
851846
""" Change the specified node.
852847
"""
853-
if __debug__:
854-
logging.getLogger('hyperdb').debug('setnode %s%s %r'
855-
% (classname, nodeid, values))
848+
self.log_debug('setnode %s%s %r'
849+
% (classname, nodeid, values))
856850

857851
# clear this node out of the cache if it's in there
858852
key = (classname, nodeid)
@@ -1113,9 +1107,8 @@ def addjournal(self, classname, nodeid, action, params, creator=None,
11131107
# create the journal entry
11141108
cols = 'nodeid,date,tag,action,params'
11151109

1116-
if __debug__:
1117-
logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%(classname,
1118-
nodeid, journaldate, journaltag, action, params))
1110+
self.log_debug('addjournal %s%s %r %s %s %r'%(classname,
1111+
nodeid, journaldate, journaltag, action, params))
11191112

11201113
# make the journalled data marshallable
11211114
if isinstance(params, type({})):
@@ -1140,10 +1133,9 @@ def setjournal(self, classname, nodeid, journal):
11401133

11411134
dc = self.hyperdb_to_sql_value[hyperdb.Date]
11421135
for nodeid, journaldate, journaltag, action, params in journal:
1143-
if __debug__:
1144-
logging.getLogger('hyperdb').debug('addjournal %s%s %r %s %s %r'%(
1145-
classname, nodeid, journaldate, journaltag, action,
1146-
params))
1136+
self.log_debug('addjournal %s%s %r %s %s %r'%(
1137+
classname, nodeid, journaldate, journaltag, action,
1138+
params))
11471139

11481140
# make the journalled data marshallable
11491141
if isinstance(params, type({})):
@@ -2114,7 +2106,7 @@ def filter(self, search_matches, filterspec, sort=[], group=[]):
21142106
backward-compatibility reasons a single (dir, prop) tuple is
21152107
also allowed.
21162108
2117-
"search_matches" is a sequence type or None
2109+
"search_matches" is a container type or None
21182110
21192111
The filter must match all properties specificed. If the property
21202112
value to match is a list:

roundup/roundupdb.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import re, os, smtplib, socket, time, random
2626
import cStringIO, base64, quopri, mimetypes
2727
import os.path
28+
import logging
2829

2930
from rfc2822 import encode_header
3031

@@ -115,6 +116,29 @@ def confirm_registration(self, otk):
115116
return userid
116117

117118

119+
def log_debug(self, msg, *args, **kwargs):
120+
"""Log a message with level DEBUG."""
121+
122+
logger = self.get_logger()
123+
logger.debug(msg, *args, **kwargs)
124+
125+
def log_info(self, msg, *args, **kwargs):
126+
"""Log a message with level INFO."""
127+
128+
logger = self.get_logger()
129+
logger.info(msg, *args, **kwargs)
130+
131+
def get_logger(self):
132+
"""Return the logger for this database."""
133+
134+
# Because getting a logger requires acquiring a lock, we want
135+
# to do it only once.
136+
if not hasattr(self, '__logger'):
137+
self.__logger = logging.getLogger('hyperdb')
138+
139+
return self.__logger
140+
141+
118142
class DetectorError(RuntimeError):
119143
""" Raised by detectors that want to indicate that something's amiss
120144
"""

0 commit comments

Comments
 (0)