Cleaning logging method

This commit is contained in:
Benjamin Renard 2019-03-11 14:32:18 +01:00 committed by root
parent 664a8bc0e3
commit 2ed8dea4e2

28
PgDB.py
View file

@ -30,8 +30,8 @@ class PgDB(object):
try:
con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd))
self.con = con
except Exception, e:
logging.fatal(e)
except Exception:
logging.fatal('An error occured during Postgresql database connection.', exc_info=1)
sys.exit(1)
def close(self):
@ -43,8 +43,8 @@ class PgDB(object):
try:
self.con.set_client_encoding(enc)
return True
except Exception, e:
logging.error(e)
except Exception:
logging.error('An error occured setting Postgresql database connection encoding to "%s"', enc, exc_info=1)
return False
def doSQL(self,sql,params=None):
@ -56,8 +56,8 @@ class PgDB(object):
cursor.execute(sql,params)
self.con.commit()
return True
except Exception, e:
logging.error(u'Error during SQL request "%s" :\n\n%s' % (sql.decode('utf-8', 'ignore'), traceback.format_exc()))
except Exception:
logging.error(u'Error during SQL request "%s"', sql.decode('utf-8', 'ignore'), exc_info=1)
self.con.rollback()
return False
@ -67,8 +67,8 @@ class PgDB(object):
cursor.execute(sql)
results = cursor.fetchall()
return results
except Exception, e:
logging.error(u'Error during SQL request "%s" :\n\n%s' % (sql.decode('utf-8', 'ignore'), traceback.format_exc()))
except Exception:
logging.error(u'Error during SQL request "%s"', sql.decode('utf-8', 'ignore'), exc_info=1)
return False
#
@ -94,7 +94,7 @@ class PgDB(object):
return (u" %s " % where_op).join(where_clauses)
elif type(where_clauses) is dict:
return (u" %s " % where_op).join(map(lambda x: "%s=%s" % (x, _quote_value(where_clauses[x])), where_clauses))
logging.error('Unsupported where clauses type %s' % type(where_clauses))
logging.error('Unsupported where clauses type %s', type(where_clauses))
return False
def _format_datetime(self, datetime):
@ -113,7 +113,7 @@ class PgDB(object):
sql=u"INSERT INTO %s (%s) VALUES (%s)" % (table, u', '.join(values.keys()), u", ".join(map(lambda x: self._quote_value(values[x]), values)))
if just_try:
logging.debug(u"Just-try mode : execute INSERT query : %s" % sql)
logging.debug(u"Just-try mode : execute INSERT query : %s", sql)
return True
logging.debug(sql)
@ -130,12 +130,12 @@ class PgDB(object):
sql=u"UPDATE %s SET %s WHERE %s" % (table, u", ".join(map(lambda x: "%s=%s" % (x, self._quote_value(values[x])), values)), where)
if just_try:
logging.debug(u"Just-try mode : execute UPDATE query : %s" % sql)
logging.debug(u"Just-try mode : execute UPDATE query : %s", sql)
return True
logging.debug(sql)
if not self.doSQL(sql):
logging.error(u"Fail to execute UPDATE query (SQL : %s)" % sql)
logging.error(u"Fail to execute UPDATE query (SQL : %s)", sql)
return False
return True
@ -147,12 +147,12 @@ class PgDB(object):
sql=u"DELETE FROM %s WHERE %s" % (table, where)
if just_try:
logging.debug(u"Just-try mode : execute DELETE query : %s" % sql)
logging.debug(u"Just-try mode : execute DELETE query : %s", sql)
return True
logging.debug(sql)
if not self.doSQL(sql):
logging.error(u"Fail to execute DELETE query (SQL : %s)" % sql)
logging.error(u"Fail to execute DELETE query (SQL : %s)", sql)
return False
return True