diff --git a/bin/netsvc.py b/bin/netsvc.py index 3c3a727cb71..11f7ca5b695 100644 --- a/bin/netsvc.py +++ b/bin/netsvc.py @@ -165,21 +165,24 @@ LEVEL_COLOR_MAPPING = { 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): fg_color, bg_color = LEVEL_COLOR_MAPPING[record.levelno] 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(): import os from tools.translate import resetlocale resetlocale() - logger = logging.getLogger() # 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']: # SysLog Handler @@ -188,7 +191,7 @@ def init_logger(): else: handler = logging.handlers.SysLogHandler('/dev/log') 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']: # LogFile Handler @@ -213,10 +216,11 @@ def init_logger(): if isinstance(handler, logging.StreamHandler) and os.isatty(handler.stream.fileno()): formatter = ColoredFormatter(format) else: - formatter = logging.Formatter(format) + formatter = DBFormatter(format) handler.setFormatter(formatter) # add the handler to the root logger + logger = logging.getLogger() logger.addHandler(handler) logger.setLevel(int(tools.config['log_level'] or '0')) diff --git a/bin/service/web_services.py b/bin/service/web_services.py index 8f1fc910739..8b33b41a0b3 100644 --- a/bin/service/web_services.py +++ b/bin/service/web_services.py @@ -20,7 +20,6 @@ ############################################################################## import base64 -import logging import os import security import thread @@ -39,8 +38,6 @@ import tools import locale from cStringIO import StringIO -logging.basicConfig() - class db(netsvc.ExportService): def __init__(self, name="db"): netsvc.ExportService.__init__(self, name) diff --git a/bin/sql_db.py b/bin/sql_db.py index 5ed7b14c0df..76a9e6f5f7c 100644 --- a/bin/sql_db.py +++ b/bin/sql_db.py @@ -21,6 +21,7 @@ __all__ = ['db_connect', 'close_db'] +from threading import current_thread import logging from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE from psycopg2.psycopg1 import cursor as psycopg1cursor @@ -362,11 +363,15 @@ def dsn_are_equals(first, second): _Pool = ConnectionPool(int(tools.config['db_maxconn'])) def db_connect(db_name): + current_thread().dbname = db_name return Connection(_Pool, db_name) def close_db(db_name): _Pool.close_all(dsn(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: