new logging handler and commandline config, mostly work except the filter on the root logger
bzr revid: al@openerp.com-20120201001019-7fol09su31gbn69w
This commit is contained in:
parent
b80c634176
commit
5a076878af
|
@ -148,13 +148,25 @@ class ColoredFormatter(DBFormatter):
|
||||||
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 DBFormatter.format(self, record)
|
return DBFormatter.format(self, record)
|
||||||
|
|
||||||
|
|
||||||
|
class init_logger_filter(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.exclude = []
|
||||||
|
def filter(self, record):
|
||||||
|
# never called i dont know why
|
||||||
|
print "filter",record
|
||||||
|
for i in self.exclude:
|
||||||
|
if record.name.startswith(i):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def init_logger():
|
def init_logger():
|
||||||
from tools.translate import resetlocale
|
from tools.translate import resetlocale
|
||||||
resetlocale()
|
resetlocale()
|
||||||
|
|
||||||
# create a format for log messages and dates
|
# create a format for log messages and dates
|
||||||
format = '[%(asctime)s][%(dbname)s] %(levelname)s:%(name)s:%(message)s'
|
format = '[%(asctime)s][%(dbname)s] %(levelname)s:%(name)s:%(message)s'
|
||||||
format = '%(asctime)s %(levelname)s %(pid)s %(dbname)s %(name)s: %(message)s'
|
format = '%(asctime)s %(pid)s %(levelname)s %(dbname)s %(name)s: %(message)s'
|
||||||
|
|
||||||
if tools.config['syslog']:
|
if tools.config['syslog']:
|
||||||
# SysLog Handler
|
# SysLog Handler
|
||||||
|
@ -191,11 +203,21 @@ def init_logger():
|
||||||
formatter = DBFormatter(format)
|
formatter = DBFormatter(format)
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
# Add the handler to the 'openerp' logger.
|
# Configure handlers
|
||||||
logger = logging.getLogger()
|
logconfig = tools.config['log_handler']
|
||||||
logger.handlers = []
|
logfilter = init_logger_filter()
|
||||||
logger.addHandler(handler)
|
for i in logconfig:
|
||||||
logger.setLevel(int(tools.config['log_level'] or '0'))
|
prefix, level = i.split(':')
|
||||||
|
level = getattr(logging, level, logging.INFO)
|
||||||
|
logger = logging.getLogger(prefix)
|
||||||
|
logger.handlers = []
|
||||||
|
logger.setLevel(level)
|
||||||
|
logger.addHandler(handler)
|
||||||
|
if prefix == '':
|
||||||
|
logger.addFilter(logfilter)
|
||||||
|
else:
|
||||||
|
logfilter.exclude.append(prefix)
|
||||||
|
|
||||||
|
|
||||||
# A alternative logging scheme for automated runs of the
|
# A alternative logging scheme for automated runs of the
|
||||||
# server intended to test it.
|
# server intended to test it.
|
||||||
|
@ -292,19 +314,17 @@ class Server:
|
||||||
def replace_request_password(args):
|
def replace_request_password(args):
|
||||||
# password is always 3rd argument in a request, we replace it in RPC logs
|
# password is always 3rd argument in a request, we replace it in RPC logs
|
||||||
# so it's easier to forward logs for diagnostics/debugging purposes...
|
# so it's easier to forward logs for diagnostics/debugging purposes...
|
||||||
args = list(args)
|
|
||||||
if len(args) > 2:
|
if len(args) > 2:
|
||||||
|
args = list(args)
|
||||||
args[2] = '*'
|
args[2] = '*'
|
||||||
return args
|
return tuple(args)
|
||||||
|
|
||||||
def log(title, msg, channel=logging.DEBUG_RPC, depth=None, fn=""):
|
def dispatch_rpc_log(logger, prefix, msg, depth=None):
|
||||||
logger = logging.getLogger(title)
|
indent=''
|
||||||
if logger.isEnabledFor(channel):
|
indent_after=' '*len(prefix)
|
||||||
indent=''
|
for line in (prefix+pformat(msg, depth=depth)).split('\n'):
|
||||||
indent_after=' '*len(fn)
|
logger.debug(indent+line)
|
||||||
for line in (fn+pformat(msg, depth=depth)).split('\n'):
|
indent=indent_after
|
||||||
logger.log(channel, indent+line)
|
|
||||||
indent=indent_after
|
|
||||||
|
|
||||||
def dispatch_rpc(service_name, method, params):
|
def dispatch_rpc(service_name, method, params):
|
||||||
""" Handle a RPC call.
|
""" Handle a RPC call.
|
||||||
|
@ -312,21 +332,25 @@ def dispatch_rpc(service_name, method, params):
|
||||||
This is pure Python code, the actual marshalling (from/to XML-RPC or
|
This is pure Python code, the actual marshalling (from/to XML-RPC or
|
||||||
NET-RPC) is done in a upper layer.
|
NET-RPC) is done in a upper layer.
|
||||||
"""
|
"""
|
||||||
def _log(title, msg, channel=logging.DEBUG_RPC, depth=None, fn=""):
|
|
||||||
log(__name__, msg, channel=channel, depth=depth, fn=fn)
|
|
||||||
try:
|
try:
|
||||||
start_time = end_time = 0
|
rpc_short = logging.getLogger(__name__ + '.rpc_short')
|
||||||
if _logger.isEnabledFor(logging.DEBUG_RPC_ANSWER):
|
rpc_full = logging.getLogger(__name__ + '.rpc_full')
|
||||||
_log('service', tuple(replace_request_password(params)), depth=None, fn='%s.%s'%(service_name,method))
|
rpc_short_flag = rpc_short.isEnabledFor(logging.DEBUG)
|
||||||
if _logger.isEnabledFor(logging.DEBUG_RPC):
|
rpc_full_flag = rpc_full.isEnabledFor(logging.DEBUG)
|
||||||
|
if rpc_short_flag or rpc_full_flag:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
if rpc_full_flag:
|
||||||
|
dispatch_rpc_log(rpc_full,'%s.%s:request '%(service_name,method), replace_request_password(params))
|
||||||
|
|
||||||
result = ExportService.getService(service_name).dispatch(method, params)
|
result = ExportService.getService(service_name).dispatch(method, params)
|
||||||
if _logger.isEnabledFor(logging.DEBUG_RPC):
|
|
||||||
|
if rpc_short_flag or rpc_full_flag:
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
if not _logger.isEnabledFor(logging.DEBUG_RPC_ANSWER):
|
if rpc_full_flag:
|
||||||
_log('service (%.3fs)' % (end_time - start_time), tuple(replace_request_password(params)), depth=1, fn='%s.%s'%(service_name,method))
|
dispatch_rpc_log(rpc_full,'%s.%s:reply time:%.3fs '%(service_name,method,end_time - start_time), result)
|
||||||
_log('execution time', '%.3fs' % (end_time - start_time), channel=logging.DEBUG_RPC_ANSWER)
|
else:
|
||||||
_log('result', result, channel=logging.DEBUG_RPC_ANSWER)
|
dispatch_rpc_log(rpc_short,'%s.%s time:%.3fs '%(service_name,method,end_time - start_time), replace_request_password(params), depth=1)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
except openerp.exceptions.AccessError:
|
except openerp.exceptions.AccessError:
|
||||||
raise
|
raise
|
||||||
|
@ -335,11 +359,11 @@ def dispatch_rpc(service_name, method, params):
|
||||||
except openerp.exceptions.Warning:
|
except openerp.exceptions.Warning:
|
||||||
raise
|
raise
|
||||||
except openerp.exceptions.DeferredException, e:
|
except openerp.exceptions.DeferredException, e:
|
||||||
_log('exception', tools.exception_to_unicode(e))
|
_logger.error(tools.exception_to_unicode(e))
|
||||||
post_mortem(e.traceback)
|
post_mortem(e.traceback)
|
||||||
raise
|
raise
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
_log('exception', tools.exception_to_unicode(e))
|
_logger.error(tools.exception_to_unicode(e))
|
||||||
post_mortem(sys.exc_info())
|
post_mortem(sys.exc_info())
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
|
@ -82,8 +82,7 @@ class configmanager(object):
|
||||||
self.config_file = fname
|
self.config_file = fname
|
||||||
self.has_ssl = check_ssl()
|
self.has_ssl = check_ssl()
|
||||||
|
|
||||||
self._LOGLEVELS = dict([(getattr(loglevels, 'LOG_%s' % x), getattr(logging, x))
|
self._LOGLEVELS = dict([(getattr(loglevels, 'LOG_%s' % x), getattr(logging, x)) for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'TEST', 'DEBUG', 'DEBUG_RPC', 'DEBUG_SQL', 'DEBUG_RPC_ANSWER','NOTSET')])
|
||||||
for x in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'TEST', 'DEBUG', 'DEBUG_RPC', 'DEBUG_SQL', 'DEBUG_RPC_ANSWER','NOTSET')])
|
|
||||||
|
|
||||||
version = "%s %s" % (release.description, release.version)
|
version = "%s %s" % (release.description, release.version)
|
||||||
self.parser = parser = optparse.OptionParser(version=version, option_class=MyOption)
|
self.parser = parser = optparse.OptionParser(version=version, option_class=MyOption)
|
||||||
|
@ -178,13 +177,14 @@ class configmanager(object):
|
||||||
# Logging Group
|
# Logging Group
|
||||||
group = optparse.OptionGroup(parser, "Logging Configuration")
|
group = optparse.OptionGroup(parser, "Logging Configuration")
|
||||||
group.add_option("--logfile", dest="logfile", help="file where the server log will be stored")
|
group.add_option("--logfile", dest="logfile", help="file where the server log will be stored")
|
||||||
group.add_option("--no-logrotate", dest="logrotate", action="store_false", my_default=True,
|
group.add_option("--no-logrotate", dest="logrotate", action="store_false", my_default=True, help="do not rotate the logfile")
|
||||||
help="do not rotate the logfile")
|
group.add_option("--syslog", action="store_true", dest="syslog", my_default=False, help="Send the log to the syslog server")
|
||||||
group.add_option("--syslog", action="store_true", dest="syslog",
|
group.add_option('--log-handler', action="append", default=[':INFO'], my_default=[':INFO'], metavar="PREFIX:LEVEL", help='setup an handler at LEVEL for a given PREFIX. Example: "openerp.orm:DEBUG" (default: ":INFO")')
|
||||||
my_default=False, help="Send the log to the syslog server")
|
group.add_option('--log-rpc-short', action="append_const", dest="log_handler", const="openerp.netsvc.rpc_short:DEBUG", help='shortcut for --log-handler=openerp.netsvc.rpc_short:DEBUG')
|
||||||
group.add_option('--log-level', dest='log_level', type='choice', choices=self._LOGLEVELS.keys(),
|
group.add_option('--log-rpc-full', action="append_const", dest="log_handler", const="openerp.netsvc.rpc_full:DEBUG", help='shortcut for --log-handler=openerp.netsvc.rpc_full:DEBUG')
|
||||||
my_default='info',
|
group.add_option('--log-rpc-web', action="append_const", dest="log_handler", const="openerp.addons.web.common.http:DEBUG", help='shortcut for --log-handler=openerp.addons.web.common.http:DEBUG')
|
||||||
help='specify the level of the logging. Accepted values: ' + str(self._LOGLEVELS.keys()))
|
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')
|
||||||
|
|
||||||
parser.add_option_group(group)
|
parser.add_option_group(group)
|
||||||
|
|
||||||
# SMTP Group
|
# SMTP Group
|
||||||
|
@ -314,6 +314,7 @@ class configmanager(object):
|
||||||
if args is None:
|
if args is None:
|
||||||
args = []
|
args = []
|
||||||
opt, args = self.parser.parse_args(args)
|
opt, args = self.parser.parse_args(args)
|
||||||
|
print opt
|
||||||
|
|
||||||
def die(cond, msg):
|
def die(cond, msg):
|
||||||
if cond:
|
if cond:
|
||||||
|
@ -361,6 +362,7 @@ class configmanager(object):
|
||||||
if self.options['pidfile'] in ('None', 'False'):
|
if self.options['pidfile'] in ('None', 'False'):
|
||||||
self.options['pidfile'] = False
|
self.options['pidfile'] = False
|
||||||
|
|
||||||
|
# if defined dont take the configfile value even if the defined value is None
|
||||||
keys = ['xmlrpc_interface', 'xmlrpc_port', 'db_name', 'db_user', 'db_password', 'db_host',
|
keys = ['xmlrpc_interface', 'xmlrpc_port', 'db_name', 'db_user', 'db_password', 'db_host',
|
||||||
'db_port', 'db_template', 'logfile', 'pidfile', 'smtp_port', 'cache_timeout',
|
'db_port', 'db_template', 'logfile', 'pidfile', 'smtp_port', 'cache_timeout',
|
||||||
'email_from', 'smtp_server', 'smtp_user', 'smtp_password',
|
'email_from', 'smtp_server', 'smtp_user', 'smtp_password',
|
||||||
|
@ -368,7 +370,7 @@ class configmanager(object):
|
||||||
'netrpc', 'xmlrpc', 'syslog', 'without_demo', 'timezone',
|
'netrpc', 'xmlrpc', 'syslog', 'without_demo', 'timezone',
|
||||||
'xmlrpcs_interface', 'xmlrpcs_port', 'xmlrpcs',
|
'xmlrpcs_interface', 'xmlrpcs_port', 'xmlrpcs',
|
||||||
'static_http_enable', 'static_http_document_root', 'static_http_url_prefix',
|
'static_http_enable', 'static_http_document_root', 'static_http_url_prefix',
|
||||||
'secure_cert_file', 'secure_pkey_file', 'dbfilter'
|
'secure_cert_file', 'secure_pkey_file', 'dbfilter', 'log_handler'
|
||||||
]
|
]
|
||||||
|
|
||||||
for arg in keys:
|
for arg in keys:
|
||||||
|
@ -379,6 +381,7 @@ class configmanager(object):
|
||||||
elif isinstance(self.options[arg], basestring) and self.casts[arg].type in optparse.Option.TYPE_CHECKER:
|
elif isinstance(self.options[arg], basestring) and self.casts[arg].type in optparse.Option.TYPE_CHECKER:
|
||||||
self.options[arg] = optparse.Option.TYPE_CHECKER[self.casts[arg].type](self.casts[arg], arg, self.options[arg])
|
self.options[arg] = optparse.Option.TYPE_CHECKER[self.casts[arg].type](self.casts[arg], arg, self.options[arg])
|
||||||
|
|
||||||
|
# if defined but None take the configfile value
|
||||||
keys = [
|
keys = [
|
||||||
'language', 'translate_out', 'translate_in', 'overwrite_existing_translations',
|
'language', 'translate_out', 'translate_in', 'overwrite_existing_translations',
|
||||||
'debug_mode', 'smtp_ssl', 'load_language',
|
'debug_mode', 'smtp_ssl', 'load_language',
|
||||||
|
@ -402,11 +405,6 @@ class configmanager(object):
|
||||||
else:
|
else:
|
||||||
self.options['assert_exit_level'] = self._LOGLEVELS.get(self.options['assert_exit_level']) or int(self.options['assert_exit_level'])
|
self.options['assert_exit_level'] = self._LOGLEVELS.get(self.options['assert_exit_level']) or int(self.options['assert_exit_level'])
|
||||||
|
|
||||||
if opt.log_level:
|
|
||||||
self.options['log_level'] = self._LOGLEVELS[opt.log_level]
|
|
||||||
else:
|
|
||||||
self.options['log_level'] = self._LOGLEVELS.get(self.options['log_level']) or int(self.options['log_level'])
|
|
||||||
|
|
||||||
self.options['root_path'] = os.path.abspath(os.path.expanduser(os.path.expandvars(os.path.dirname(openerp.__file__))))
|
self.options['root_path'] = os.path.abspath(os.path.expanduser(os.path.expandvars(os.path.dirname(openerp.__file__))))
|
||||||
if not self.options['addons_path'] or self.options['addons_path']=='None':
|
if not self.options['addons_path'] or self.options['addons_path']=='None':
|
||||||
self.options['addons_path'] = os.path.join(self.options['root_path'], 'addons')
|
self.options['addons_path'] = os.path.join(self.options['root_path'], 'addons')
|
||||||
|
|
Loading…
Reference in New Issue