[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_db = getattr(ct, 'dbname', None)
ct_uid = getattr(ct, 'uid', None) ct_uid = getattr(ct, 'uid', None)
dbname = tools.config['log_db'] or ct_db dbname = tools.config['log_db'] or ct_db
if dbname: if not dbname:
cr = None return
try: with tools.ignore(Exception), sql_db.db_connect(dbname).cursor() as cr:
cr = sql_db.db_connect(dbname).cursor() msg = tools.ustr(record.msg)
msg = unicode(record.msg) if record.args:
traceback = getattr(record, 'exc_text', '') msg = msg % record.args
if traceback: traceback = getattr(record, 'exc_text', '')
msg = "%s\n%s" % (msg, traceback) if traceback:
level = logging.getLevelName(record.levelno) msg = "%s\n%s" % (msg, traceback)
val = (ct_uid, ct_uid, 'server', ct_db, record.name, level, msg, record.pathname, record.lineno, record.funcName) # we do not use record.levelname because it may have been changed by ColoredFormatter.
cr.execute(""" levelname = logging.getLevelName(record.levelno)
INSERT INTO ir_logging(create_date, write_date, create_uid, write_uid, type, dbname, name, level, message, path, line, func) val = (ct_uid, ct_uid, 'server', ct_db, record.name, levelname, msg, record.pathname, record.lineno, record.funcName)
VALUES (NOW() at time zone 'UTC', NOW() at time zone 'UTC', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) cr.execute("""
""", val ) INSERT INTO ir_logging(create_date, write_date, create_uid, write_uid, type, dbname, name, level, message, path, line, func)
cr.commit() VALUES (NOW() at time zone 'UTC', NOW() at time zone 'UTC', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
except Exception, e: """, val)
pass
finally:
if cr:
cr.close()
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, _NOTHING, DEFAULT = range(10) 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 #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 from functools import wraps
import cProfile import cProfile
from contextlib import contextmanager
import subprocess import subprocess
import logging import logging
import os import os
@ -1209,6 +1210,11 @@ def dumpstacks(sig=None, frame=None):
_logger.info("\n".join(code)) _logger.info("\n".join(code))
@contextmanager
def ignore(*exc):
try:
yield
except exc:
pass
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: