From 547204b7f4a81b6915c9f84754080dd3195950e9 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Thu, 7 May 2015 15:07:04 +0200 Subject: [PATCH] [IMP] logging: store logs in database in multi databases env. The server parameter `log-db` gives the possibility to store the logs of a server in a specific database, in the ir.logging model. Unfortunately, it wasn't possible to store these logs within the database from which the logs came from in a multi databases environment (e.g. the SAAS). This revision gives the possibility to store the logs within the database where the logs come from, using --log-db=%d (inspired from the --db-filter arg) We also added the possibility to change the level of logs to store in the database, with the --log-db-level argument, which is set by default to `warning`. --- openerp/netsvc.py | 11 +++++++++-- openerp/tools/config.py | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/openerp/netsvc.py b/openerp/netsvc.py index 307ffaf24d3..368e704b22f 100644 --- a/openerp/netsvc.py +++ b/openerp/netsvc.py @@ -78,7 +78,7 @@ class PostgreSQLHandler(logging.Handler): def emit(self, record): ct = threading.current_thread() ct_db = getattr(ct, 'dbname', None) - dbname = tools.config['log_db'] or ct_db + dbname = tools.config['log_db'] if tools.config['log_db'] and tools.config['log_db'] != '%d' else ct_db if not dbname: return with tools.ignore(Exception), tools.mute_logger('openerp.sql_db'), sql_db.db_connect(dbname, allow_uri=True).cursor() as cr: @@ -187,8 +187,15 @@ def init_logger(): logging.getLogger().addHandler(handler) if tools.config['log_db']: + db_levels = { + 'debug': logging.DEBUG, + 'info': logging.INFO, + 'warning': logging.WARNING, + 'error': logging.ERROR, + 'critical': logging.CRITICAL, + } postgresqlHandler = PostgreSQLHandler() - postgresqlHandler.setLevel(25) + postgresqlHandler.setLevel(int(db_levels.get(tools.config['log_db_level'], tools.config['log_db_level']))) logging.getLogger().addHandler(postgresqlHandler) # Configure loggers levels diff --git a/openerp/tools/config.py b/openerp/tools/config.py index 21b15132fa3..2a96ed764bb 100644 --- a/openerp/tools/config.py +++ b/openerp/tools/config.py @@ -209,6 +209,7 @@ class configmanager(object): group.add_option('--log-web', action="append_const", dest="log_handler", const="openerp.http:DEBUG", help='shortcut for --log-handler=openerp.http:DEBUG') group.add_option('--log-sql', action="append_const", dest="log_handler", const="openerp.sql_db:DEBUG", help='shortcut for --log-handler=openerp.sql_db:DEBUG') group.add_option('--log-db', dest='log_db', help="Logging database", my_default=False) + group.add_option('--log-db-level', dest='log_db_level', my_default='warning', help="Logging database level") # For backward-compatibility, map the old log levels to something # quite close. levels = [ @@ -417,7 +418,7 @@ class configmanager(object): 'xmlrpc', 'syslog', 'without_demo', 'timezone', 'xmlrpcs_interface', 'xmlrpcs_port', 'xmlrpcs', 'secure_cert_file', 'secure_pkey_file', 'dbfilter', 'log_level', 'log_db', - 'geoip_database', + 'log_db_level', 'geoip_database', ] for arg in keys: