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)
|
||||
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():
|
||||
from tools.translate import resetlocale
|
||||
resetlocale()
|
||||
|
||||
# create a format for log messages and dates
|
||||
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']:
|
||||
# SysLog Handler
|
||||
|
@ -191,11 +203,21 @@ def init_logger():
|
|||
formatter = DBFormatter(format)
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
# Add the handler to the 'openerp' logger.
|
||||
logger = logging.getLogger()
|
||||
logger.handlers = []
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(int(tools.config['log_level'] or '0'))
|
||||
# Configure handlers
|
||||
logconfig = tools.config['log_handler']
|
||||
logfilter = init_logger_filter()
|
||||
for i in logconfig:
|
||||
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
|
||||
# server intended to test it.
|
||||
|
@ -292,19 +314,17 @@ class Server:
|
|||
def replace_request_password(args):
|
||||
# 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...
|
||||
args = list(args)
|
||||
if len(args) > 2:
|
||||
args = list(args)
|
||||
args[2] = '*'
|
||||
return args
|
||||
return tuple(args)
|
||||
|
||||
def log(title, msg, channel=logging.DEBUG_RPC, depth=None, fn=""):
|
||||
logger = logging.getLogger(title)
|
||||
if logger.isEnabledFor(channel):
|
||||
indent=''
|
||||
indent_after=' '*len(fn)
|
||||
for line in (fn+pformat(msg, depth=depth)).split('\n'):
|
||||
logger.log(channel, indent+line)
|
||||
indent=indent_after
|
||||
def dispatch_rpc_log(logger, prefix, msg, depth=None):
|
||||
indent=''
|
||||
indent_after=' '*len(prefix)
|
||||
for line in (prefix+pformat(msg, depth=depth)).split('\n'):
|
||||
logger.debug(indent+line)
|
||||
indent=indent_after
|
||||
|
||||
def dispatch_rpc(service_name, method, params):
|
||||
""" 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
|
||||
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:
|
||||
start_time = end_time = 0
|
||||
if _logger.isEnabledFor(logging.DEBUG_RPC_ANSWER):
|
||||
_log('service', tuple(replace_request_password(params)), depth=None, fn='%s.%s'%(service_name,method))
|
||||
if _logger.isEnabledFor(logging.DEBUG_RPC):
|
||||
rpc_short = logging.getLogger(__name__ + '.rpc_short')
|
||||
rpc_full = logging.getLogger(__name__ + '.rpc_full')
|
||||
rpc_short_flag = rpc_short.isEnabledFor(logging.DEBUG)
|
||||
rpc_full_flag = rpc_full.isEnabledFor(logging.DEBUG)
|
||||
if rpc_short_flag or rpc_full_flag:
|
||||
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)
|
||||
if _logger.isEnabledFor(logging.DEBUG_RPC):
|
||||
|
||||
if rpc_short_flag or rpc_full_flag:
|
||||
end_time = time.time()
|
||||
if not _logger.isEnabledFor(logging.DEBUG_RPC_ANSWER):
|
||||
_log('service (%.3fs)' % (end_time - start_time), tuple(replace_request_password(params)), depth=1, fn='%s.%s'%(service_name,method))
|
||||
_log('execution time', '%.3fs' % (end_time - start_time), channel=logging.DEBUG_RPC_ANSWER)
|
||||
_log('result', result, channel=logging.DEBUG_RPC_ANSWER)
|
||||
if rpc_full_flag:
|
||||
dispatch_rpc_log(rpc_full,'%s.%s:reply time:%.3fs '%(service_name,method,end_time - start_time), result)
|
||||
else:
|
||||
dispatch_rpc_log(rpc_short,'%s.%s time:%.3fs '%(service_name,method,end_time - start_time), replace_request_password(params), depth=1)
|
||||
|
||||
return result
|
||||
except openerp.exceptions.AccessError:
|
||||
raise
|
||||
|
@ -335,11 +359,11 @@ def dispatch_rpc(service_name, method, params):
|
|||
except openerp.exceptions.Warning:
|
||||
raise
|
||||
except openerp.exceptions.DeferredException, e:
|
||||
_log('exception', tools.exception_to_unicode(e))
|
||||
_logger.error(tools.exception_to_unicode(e))
|
||||
post_mortem(e.traceback)
|
||||
raise
|
||||
except Exception, e:
|
||||
_log('exception', tools.exception_to_unicode(e))
|
||||
_logger.error(tools.exception_to_unicode(e))
|
||||
post_mortem(sys.exc_info())
|
||||
raise
|
||||
|
||||
|
|
|
@ -82,8 +82,7 @@ class configmanager(object):
|
|||
self.config_file = fname
|
||||
self.has_ssl = check_ssl()
|
||||
|
||||
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')])
|
||||
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')])
|
||||
|
||||
version = "%s %s" % (release.description, release.version)
|
||||
self.parser = parser = optparse.OptionParser(version=version, option_class=MyOption)
|
||||
|
@ -178,13 +177,14 @@ class configmanager(object):
|
|||
# Logging Group
|
||||
group = optparse.OptionGroup(parser, "Logging Configuration")
|
||||
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,
|
||||
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('--log-level', dest='log_level', type='choice', choices=self._LOGLEVELS.keys(),
|
||||
my_default='info',
|
||||
help='specify the level of the logging. Accepted values: ' + str(self._LOGLEVELS.keys()))
|
||||
group.add_option("--no-logrotate", dest="logrotate", action="store_false", my_default=True, 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('--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")')
|
||||
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-rpc-full', action="append_const", dest="log_handler", const="openerp.netsvc.rpc_full:DEBUG", help='shortcut for --log-handler=openerp.netsvc.rpc_full:DEBUG')
|
||||
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')
|
||||
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)
|
||||
|
||||
# SMTP Group
|
||||
|
@ -314,6 +314,7 @@ class configmanager(object):
|
|||
if args is None:
|
||||
args = []
|
||||
opt, args = self.parser.parse_args(args)
|
||||
print opt
|
||||
|
||||
def die(cond, msg):
|
||||
if cond:
|
||||
|
@ -361,6 +362,7 @@ class configmanager(object):
|
|||
if self.options['pidfile'] in ('None', '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',
|
||||
'db_port', 'db_template', 'logfile', 'pidfile', 'smtp_port', 'cache_timeout',
|
||||
'email_from', 'smtp_server', 'smtp_user', 'smtp_password',
|
||||
|
@ -368,7 +370,7 @@ class configmanager(object):
|
|||
'netrpc', 'xmlrpc', 'syslog', 'without_demo', 'timezone',
|
||||
'xmlrpcs_interface', 'xmlrpcs_port', 'xmlrpcs',
|
||||
'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:
|
||||
|
@ -379,6 +381,7 @@ class configmanager(object):
|
|||
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])
|
||||
|
||||
# if defined but None take the configfile value
|
||||
keys = [
|
||||
'language', 'translate_out', 'translate_in', 'overwrite_existing_translations',
|
||||
'debug_mode', 'smtp_ssl', 'load_language',
|
||||
|
@ -402,11 +405,6 @@ class configmanager(object):
|
|||
else:
|
||||
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__))))
|
||||
if not self.options['addons_path'] or self.options['addons_path']=='None':
|
||||
self.options['addons_path'] = os.path.join(self.options['root_path'], 'addons')
|
||||
|
|
Loading…
Reference in New Issue