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: try:
con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd)) con = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" % (self.db,self.user,self.host,self.pwd))
self.con = con self.con = con
except Exception, e: except Exception:
logging.fatal(e) logging.fatal('An error occured during Postgresql database connection.', exc_info=1)
sys.exit(1) sys.exit(1)
def close(self): def close(self):
@ -43,8 +43,8 @@ class PgDB(object):
try: try:
self.con.set_client_encoding(enc) self.con.set_client_encoding(enc)
return True return True
except Exception, e: except Exception:
logging.error(e) logging.error('An error occured setting Postgresql database connection encoding to "%s"', enc, exc_info=1)
return False return False
def doSQL(self,sql,params=None): def doSQL(self,sql,params=None):
@ -56,8 +56,8 @@ class PgDB(object):
cursor.execute(sql,params) cursor.execute(sql,params)
self.con.commit() self.con.commit()
return True return True
except Exception, e: except Exception:
logging.error(u'Error during SQL request "%s" :\n\n%s' % (sql.decode('utf-8', 'ignore'), traceback.format_exc())) logging.error(u'Error during SQL request "%s"', sql.decode('utf-8', 'ignore'), exc_info=1)
self.con.rollback() self.con.rollback()
return False return False
@ -67,8 +67,8 @@ class PgDB(object):
cursor.execute(sql) cursor.execute(sql)
results = cursor.fetchall() results = cursor.fetchall()
return results return results
except Exception, e: except Exception:
logging.error(u'Error during SQL request "%s" :\n\n%s' % (sql.decode('utf-8', 'ignore'), traceback.format_exc())) logging.error(u'Error during SQL request "%s"', sql.decode('utf-8', 'ignore'), exc_info=1)
return False return False
# #
@ -94,7 +94,7 @@ class PgDB(object):
return (u" %s " % where_op).join(where_clauses) return (u" %s " % where_op).join(where_clauses)
elif type(where_clauses) is dict: 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)) 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 return False
def _format_datetime(self, datetime): 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))) 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: 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 return True
logging.debug(sql) 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) 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: 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 return True
logging.debug(sql) logging.debug(sql)
if not self.doSQL(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 False
return True return True
@ -147,12 +147,12 @@ class PgDB(object):
sql=u"DELETE FROM %s WHERE %s" % (table, where) sql=u"DELETE FROM %s WHERE %s" % (table, where)
if just_try: 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 return True
logging.debug(sql) logging.debug(sql)
if not self.doSQL(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 False
return True return True