[IMP] display database name in the logger
bzr revid: chs@openerp.com-20100903074132-nrhd3p2i53hmtt9j
This commit is contained in:
parent
827ce83b57
commit
7cf31b4869
|
@ -165,21 +165,24 @@ LEVEL_COLOR_MAPPING = {
|
||||||
logging.CRITICAL: (WHITE, RED),
|
logging.CRITICAL: (WHITE, RED),
|
||||||
}
|
}
|
||||||
|
|
||||||
class ColoredFormatter(logging.Formatter):
|
class DBFormatter(logging.Formatter):
|
||||||
|
def format(self, record):
|
||||||
|
record.dbname = getattr(threading.current_thread(), 'dbname', '?')
|
||||||
|
return logging.Formatter.format(self, record)
|
||||||
|
|
||||||
|
class ColoredFormatter(DBFormatter):
|
||||||
def format(self, record):
|
def format(self, record):
|
||||||
fg_color, bg_color = LEVEL_COLOR_MAPPING[record.levelno]
|
fg_color, bg_color = LEVEL_COLOR_MAPPING[record.levelno]
|
||||||
record.levelname = COLOR_PATTERN % (30 + fg_color, 40 + bg_color, record.levelname)
|
record.levelname = COLOR_PATTERN % (30 + fg_color, 40 + bg_color, record.levelname)
|
||||||
return logging.Formatter.format(self, record)
|
return DBFormatter.format(self, record)
|
||||||
|
|
||||||
|
|
||||||
def init_logger():
|
def init_logger():
|
||||||
import os
|
import os
|
||||||
from tools.translate import resetlocale
|
from tools.translate import resetlocale
|
||||||
resetlocale()
|
resetlocale()
|
||||||
|
|
||||||
logger = logging.getLogger()
|
|
||||||
# create a format for log messages and dates
|
# create a format for log messages and dates
|
||||||
format = '[%(asctime)s] %(levelname)s:%(name)s:%(message)s'
|
format = '[%(asctime)s][%(dbname)s] %(levelname)s:%(name)s:%(message)s'
|
||||||
|
|
||||||
if tools.config['syslog']:
|
if tools.config['syslog']:
|
||||||
# SysLog Handler
|
# SysLog Handler
|
||||||
|
@ -188,7 +191,7 @@ def init_logger():
|
||||||
else:
|
else:
|
||||||
handler = logging.handlers.SysLogHandler('/dev/log')
|
handler = logging.handlers.SysLogHandler('/dev/log')
|
||||||
format = '%s %s' % (release.description, release.version) \
|
format = '%s %s' % (release.description, release.version) \
|
||||||
+ ':%(levelname)s:%(name)s:%(message)s'
|
+ ':%(dbname)s:%(levelname)s:%(name)s:%(message)s'
|
||||||
|
|
||||||
elif tools.config['logfile']:
|
elif tools.config['logfile']:
|
||||||
# LogFile Handler
|
# LogFile Handler
|
||||||
|
@ -213,10 +216,11 @@ def init_logger():
|
||||||
if isinstance(handler, logging.StreamHandler) and os.isatty(handler.stream.fileno()):
|
if isinstance(handler, logging.StreamHandler) and os.isatty(handler.stream.fileno()):
|
||||||
formatter = ColoredFormatter(format)
|
formatter = ColoredFormatter(format)
|
||||||
else:
|
else:
|
||||||
formatter = logging.Formatter(format)
|
formatter = DBFormatter(format)
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
# add the handler to the root logger
|
# add the handler to the root logger
|
||||||
|
logger = logging.getLogger()
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
logger.setLevel(int(tools.config['log_level'] or '0'))
|
logger.setLevel(int(tools.config['log_level'] or '0'))
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import security
|
import security
|
||||||
import thread
|
import thread
|
||||||
|
@ -39,8 +38,6 @@ import tools
|
||||||
import locale
|
import locale
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
logging.basicConfig()
|
|
||||||
|
|
||||||
class db(netsvc.ExportService):
|
class db(netsvc.ExportService):
|
||||||
def __init__(self, name="db"):
|
def __init__(self, name="db"):
|
||||||
netsvc.ExportService.__init__(self, name)
|
netsvc.ExportService.__init__(self, name)
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
__all__ = ['db_connect', 'close_db']
|
__all__ = ['db_connect', 'close_db']
|
||||||
|
|
||||||
|
from threading import current_thread
|
||||||
import logging
|
import logging
|
||||||
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE
|
||||||
from psycopg2.psycopg1 import cursor as psycopg1cursor
|
from psycopg2.psycopg1 import cursor as psycopg1cursor
|
||||||
|
@ -362,11 +363,15 @@ def dsn_are_equals(first, second):
|
||||||
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
|
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
|
||||||
|
|
||||||
def db_connect(db_name):
|
def db_connect(db_name):
|
||||||
|
current_thread().dbname = db_name
|
||||||
return Connection(_Pool, db_name)
|
return Connection(_Pool, db_name)
|
||||||
|
|
||||||
def close_db(db_name):
|
def close_db(db_name):
|
||||||
_Pool.close_all(dsn(db_name))
|
_Pool.close_all(dsn(db_name))
|
||||||
tools.cache.clean_caches_for_db(db_name)
|
tools.cache.clean_caches_for_db(db_name)
|
||||||
|
ct = current_thread()
|
||||||
|
if hasattr(ct, 'dbname'):
|
||||||
|
delattr(ct, 'dbname')
|
||||||
|
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||||
|
|
Loading…
Reference in New Issue