[FIX] logging PostgreSQLHandler: expand message + use contextmanagers to handle exceptions and cursor

This commit is contained in:
Christophe Simonis 2014-05-28 11:43:24 +02:00
parent 04211015fc
commit b4283f0271
2 changed files with 23 additions and 21 deletions

View File

@ -77,26 +77,22 @@ class PostgreSQLHandler(logging.Handler):
ct_db = getattr(ct, 'dbname', None)
ct_uid = getattr(ct, 'uid', None)
dbname = tools.config['log_db'] or ct_db
if dbname:
cr = None
try:
cr = sql_db.db_connect(dbname).cursor()
msg = unicode(record.msg)
traceback = getattr(record, 'exc_text', '')
if traceback:
msg = "%s\n%s" % (msg, traceback)
level = logging.getLevelName(record.levelno)
val = (ct_uid, ct_uid, 'server', ct_db, record.name, level, msg, record.pathname, record.lineno, record.funcName)
cr.execute("""
INSERT INTO ir_logging(create_date, write_date, create_uid, write_uid, type, dbname, name, level, message, path, line, func)
VALUES (NOW() at time zone 'UTC', NOW() at time zone 'UTC', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""", val )
cr.commit()
except Exception, e:
pass
finally:
if cr:
cr.close()
if not dbname:
return
with tools.ignore(Exception), sql_db.db_connect(dbname).cursor() as cr:
msg = tools.ustr(record.msg)
if record.args:
msg = msg % record.args
traceback = getattr(record, 'exc_text', '')
if traceback:
msg = "%s\n%s" % (msg, traceback)
# we do not use record.levelname because it may have been changed by ColoredFormatter.
levelname = logging.getLevelName(record.levelno)
val = (ct_uid, ct_uid, 'server', ct_db, record.name, levelname, msg, record.pathname, record.lineno, record.funcName)
cr.execute("""
INSERT INTO ir_logging(create_date, write_date, create_uid, write_uid, type, dbname, name, level, message, path, line, func)
VALUES (NOW() at time zone 'UTC', NOW() at time zone 'UTC', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""", val)
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, _NOTHING, DEFAULT = range(10)
#The background is set with 40 plus the number of the color, and the foreground with 30

View File

@ -27,6 +27,7 @@ Miscellaneous tools used by OpenERP.
from functools import wraps
import cProfile
from contextlib import contextmanager
import subprocess
import logging
import os
@ -1209,6 +1210,11 @@ def dumpstacks(sig=None, frame=None):
_logger.info("\n".join(code))
@contextmanager
def ignore(*exc):
try:
yield
except exc:
pass
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: