Merge "sync log from oslo"

This commit is contained in:
Jenkins 2013-11-01 12:29:00 +00:00 committed by Gerrit Code Review
commit 2171815e0e

View File

@ -29,8 +29,6 @@ It also allows setting of formatting information through conf.
""" """
import ConfigParser
import cStringIO
import inspect import inspect
import itertools import itertools
import logging import logging
@ -41,8 +39,10 @@ import sys
import traceback import traceback
from oslo.config import cfg from oslo.config import cfg
import six
from six import moves
from neutron.openstack.common.gettextutils import _ from neutron.openstack.common.gettextutils import _ # noqa
from neutron.openstack.common import importutils from neutron.openstack.common import importutils
from neutron.openstack.common import jsonutils from neutron.openstack.common import jsonutils
from neutron.openstack.common import local from neutron.openstack.common import local
@ -64,17 +64,20 @@ common_cli_opts = [
] ]
logging_cli_opts = [ logging_cli_opts = [
cfg.StrOpt('log-config', cfg.StrOpt('log-config-append',
metavar='PATH', metavar='PATH',
help='If this option is specified, the logging configuration ' deprecated_name='log-config',
'file specified is used and overrides any other logging ' help='The name of logging configuration file. It does not '
'options specified. Please see the Python logging module ' 'disable existing loggers, but just appends specified '
'logging configuration to any other existing logging '
'options. Please see the Python logging module '
'documentation for details on logging configuration ' 'documentation for details on logging configuration '
'files.'), 'files.'),
cfg.StrOpt('log-format', cfg.StrOpt('log-format',
default=None, default=None,
metavar='FORMAT', metavar='FORMAT',
help='A logging.Formatter log message format string which may ' help='DEPRECATED. '
'A logging.Formatter log message format string which may '
'use any of the available logging.LogRecord attributes. ' 'use any of the available logging.LogRecord attributes. '
'This option is deprecated. Please use ' 'This option is deprecated. Please use '
'logging_context_format_string and ' 'logging_context_format_string and '
@ -126,12 +129,13 @@ log_opts = [
help='prefix each line of exception output with this format'), help='prefix each line of exception output with this format'),
cfg.ListOpt('default_log_levels', cfg.ListOpt('default_log_levels',
default=[ default=[
'amqp=WARN',
'amqplib=WARN', 'amqplib=WARN',
'sqlalchemy=WARN',
'boto=WARN', 'boto=WARN',
'suds=INFO',
'keystone=INFO', 'keystone=INFO',
'eventlet.wsgi.server=WARN' 'qpid=WARN',
'sqlalchemy=WARN',
'suds=INFO',
], ],
help='list of logger=LEVEL pairs'), help='list of logger=LEVEL pairs'),
cfg.BoolOpt('publish_errors', cfg.BoolOpt('publish_errors',
@ -207,6 +211,8 @@ def _get_log_file_path(binary=None):
binary = binary or _get_binary_name() binary = binary or _get_binary_name()
return '%s.log' % (os.path.join(logdir, binary),) return '%s.log' % (os.path.join(logdir, binary),)
return None
class BaseLoggerAdapter(logging.LoggerAdapter): class BaseLoggerAdapter(logging.LoggerAdapter):
@ -249,6 +255,13 @@ class ContextAdapter(BaseLoggerAdapter):
self.warn(stdmsg, *args, **kwargs) self.warn(stdmsg, *args, **kwargs)
def process(self, msg, kwargs): def process(self, msg, kwargs):
# NOTE(mrodden): catch any Message/other object and
# coerce to unicode before they can get
# to the python logging and possibly
# cause string encoding trouble
if not isinstance(msg, six.string_types):
msg = six.text_type(msg)
if 'extra' not in kwargs: if 'extra' not in kwargs:
kwargs['extra'] = {} kwargs['extra'] = {}
extra = kwargs['extra'] extra = kwargs['extra']
@ -260,14 +273,14 @@ class ContextAdapter(BaseLoggerAdapter):
extra.update(_dictify_context(context)) extra.update(_dictify_context(context))
instance = kwargs.pop('instance', None) instance = kwargs.pop('instance', None)
instance_uuid = (extra.get('instance_uuid', None) or
kwargs.pop('instance_uuid', None))
instance_extra = '' instance_extra = ''
if instance: if instance:
instance_extra = CONF.instance_format % instance instance_extra = CONF.instance_format % instance
else: elif instance_uuid:
instance_uuid = kwargs.pop('instance_uuid', None) instance_extra = (CONF.instance_uuid_format
if instance_uuid: % {'uuid': instance_uuid})
instance_extra = (CONF.instance_uuid_format
% {'uuid': instance_uuid})
extra.update({'instance': instance_extra}) extra.update({'instance': instance_extra})
extra.update({"project": self.project}) extra.update({"project": self.project})
@ -344,17 +357,18 @@ class LogConfigError(Exception):
err_msg=self.err_msg) err_msg=self.err_msg)
def _load_log_config(log_config): def _load_log_config(log_config_append):
try: try:
logging.config.fileConfig(log_config) logging.config.fileConfig(log_config_append,
except ConfigParser.Error as exc: disable_existing_loggers=False)
raise LogConfigError(log_config, str(exc)) except moves.configparser.Error as exc:
raise LogConfigError(log_config_append, str(exc))
def setup(product_name): def setup(product_name):
"""Setup logging.""" """Setup logging."""
if CONF.log_config: if CONF.log_config_append:
_load_log_config(CONF.log_config) _load_log_config(CONF.log_config_append)
else: else:
_setup_logging_from_conf() _setup_logging_from_conf()
sys.excepthook = _create_logging_excepthook(product_name) sys.excepthook = _create_logging_excepthook(product_name)
@ -520,7 +534,7 @@ class ContextFormatter(logging.Formatter):
if not record: if not record:
return logging.Formatter.formatException(self, exc_info) return logging.Formatter.formatException(self, exc_info)
stringbuffer = cStringIO.StringIO() stringbuffer = moves.StringIO()
traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
None, stringbuffer) None, stringbuffer)
lines = stringbuffer.getvalue().split('\n') lines = stringbuffer.getvalue().split('\n')