[IMP] sql_db: better logging

bzr revid: chs@tinyerp.com-20100204172800-oy6uavhn13osk9x1
This commit is contained in:
Christophe Simonis 2010-02-04 18:28:00 +01:00
parent 054cc4aa1f
commit 1c2210064c
1 changed files with 13 additions and 11 deletions

View File

@ -55,6 +55,7 @@ from tools.func import wraps
from datetime import datetime as mdt from datetime import datetime as mdt
from datetime import timedelta from datetime import timedelta
import threading import threading
from inspect import stack
import re import re
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$'); re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
@ -91,10 +92,7 @@ class Cursor(object):
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor) self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
self.__closed = False # real initialisation value self.__closed = False # real initialisation value
self.autocommit(False) self.autocommit(False)
self.__caller = tuple(stack()[2][1:3])
if tools.config['log_level'] in (netsvc.LOG_DEBUG, netsvc.LOG_DEBUG_RPC):
from inspect import stack
self.__caller = tuple(stack()[2][1:3])
def __del__(self): def __del__(self):
if not self.__closed: if not self.__closed:
@ -103,10 +101,9 @@ class Cursor(object):
# but the database connection is not put back into the connection # but the database connection is not put back into the connection
# pool, preventing some operation on the database like dropping it. # pool, preventing some operation on the database like dropping it.
# This can also lead to a server overload. # This can also lead to a server overload.
if tools.config['log_level'] in (netsvc.LOG_DEBUG, netsvc.LOG_DEBUG_RPC): msg = "Cursor not closed explicitly\n" \
msg = "Cursor not closed explicitly\n" \ "Cursor was created at %s:%s" % self.__caller
"Cursor was created at %s:%s" % self.__caller log(msg, netsvc.LOG_WARNING)
log(msg, netsvc.LOG_WARNING)
self.close() self.close()
@check @check
@ -228,9 +225,14 @@ class ConnectionPool(object):
self._lock = threading.Lock() self._lock = threading.Lock()
self._logger = netsvc.Logger() self._logger = netsvc.Logger()
def __repr__(self):
used = len([1 for c, u in self._connections[:] if u])
count = len(self._connections)
return "ConnectionPool(used=%d/count=%d/max=%d)" % (used, count, self._maxconn)
def _debug(self, msg): def _debug(self, msg):
self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, repr(self))
self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg) self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
#pass
@locked @locked
def borrow(self, dsn): def borrow(self, dsn):
@ -250,7 +252,7 @@ class ConnectionPool(object):
return result return result
if len(self._connections) >= self._maxconn: if len(self._connections) >= self._maxconn:
# try to remove the older connection not used # try to remove the oldest connection not used
for i, (cnx, used) in enumerate(self._connections): for i, (cnx, used) in enumerate(self._connections):
if not used: if not used:
self._debug('Removing old connection at index %d: %s' % (i, cnx.dsn)) self._debug('Removing old connection at index %d: %s' % (i, cnx.dsn))
@ -258,7 +260,7 @@ class ConnectionPool(object):
break break
else: else:
# note: this code is called only if the for loop has completed (no break) # note: this code is called only if the for loop has completed (no break)
raise PoolError('Connection Pool Full') raise PoolError('The Connection Pool Is Full')
self._debug('Create new connection') self._debug('Create new connection')
result = psycopg2.connect(dsn=dsn) result = psycopg2.connect(dsn=dsn)