Partial oslo-incubator sync -- log.py

This patch pulls in the changes in openstack/common/log.py that fix
a problem exposed by the removal of translation from LOG.debug
messages.  This removal causes the messages to no longer be unicode, which
can cause formatting problems.  The changes in log.py ensure that any
message that is not of six.test_type is coverted to six.text_type.

Note that this is required to complete blueprint: i18n-enablement.

Generated with:
python update.py --base nova --dest-dir /opt/stack/nova --modules log

log:
 759bd87 Merge "Set keystonemiddleware and routes.middleware to log on WARN l
 71d072f Merge "Except socket.error if syslog isn't running"
 37c0091 Add unicode coercion of logged messages to ContextFormatter
 6614413 Correct coercion of logged message to unicode
 1188d88 Except socket.error if syslog isn't running
 ac995be Fix E126 pep8 errors
 36e5c2d Merge "Adjust oslo logging to provide adapter is enabled for"
 631f880 Set keystonemiddleware and routes.middleware to log on WARN level
 726d00a Adjust oslo logging to provide adapter is enabled for

Change-Id: I255a68fc60963386e8fefe65c3ffd269795adbf4
Closes-Bug: 1348244
This commit is contained in:
James Carey 2014-08-13 21:05:54 +00:00
parent de32fc405e
commit b29443b7f5
1 changed files with 42 additions and 22 deletions

View File

@ -33,6 +33,7 @@ import logging
import logging.config
import logging.handlers
import os
import socket
import sys
import traceback
@ -40,6 +41,8 @@ from oslo.config import cfg
import six
from six import moves
_PY26 = sys.version_info[0:2] == (2, 6)
from nova.openstack.common.gettextutils import _
from nova.openstack.common import importutils
from nova.openstack.common import jsonutils
@ -124,7 +127,8 @@ DEFAULT_LOG_LEVELS = ['amqp=WARN', 'amqplib=WARN', 'boto=WARN',
'qpid=WARN', 'sqlalchemy=WARN', 'suds=INFO',
'oslo.messaging=INFO', 'iso8601=WARN',
'requests.packages.urllib3.connectionpool=WARN',
'urllib3.connectionpool=WARN', 'websocket=WARN']
'urllib3.connectionpool=WARN', 'websocket=WARN',
"keystonemiddleware=WARN", "routes.middleware=WARN"]
log_opts = [
cfg.StrOpt('logging_context_format_string',
@ -227,6 +231,15 @@ class BaseLoggerAdapter(logging.LoggerAdapter):
def audit(self, msg, *args, **kwargs):
self.log(logging.AUDIT, msg, *args, **kwargs)
def isEnabledFor(self, level):
if _PY26:
# This method was added in python 2.7 (and it does the exact
# same logic, so we need to do the exact same logic so that
# python 2.6 has this capability as well).
return self.logger.isEnabledFor(level)
else:
return super(BaseLoggerAdapter, self).isEnabledFor(level)
class LazyAdapter(BaseLoggerAdapter):
def __init__(self, name='unknown', version='unknown'):
@ -289,11 +302,10 @@ class ContextAdapter(BaseLoggerAdapter):
self.warn(stdmsg, *args, **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):
# NOTE(jecarey): If msg is not unicode, coerce it into unicode
# before it can get to the python logging and
# possibly cause string encoding trouble
if not isinstance(msg, six.text_type):
msg = six.text_type(msg)
if 'extra' not in kwargs:
@ -418,12 +430,12 @@ def set_defaults(logging_context_format_string=None,
# later in a backwards in-compatible change
if default_log_levels is not None:
cfg.set_defaults(
log_opts,
default_log_levels=default_log_levels)
log_opts,
default_log_levels=default_log_levels)
if logging_context_format_string is not None:
cfg.set_defaults(
log_opts,
logging_context_format_string=logging_context_format_string)
log_opts,
logging_context_format_string=logging_context_format_string)
def _find_facility_from_conf():
@ -472,18 +484,6 @@ def _setup_logging_from_conf(project, version):
for handler in log_root.handlers:
log_root.removeHandler(handler)
if CONF.use_syslog:
facility = _find_facility_from_conf()
# TODO(bogdando) use the format provided by RFCSysLogHandler
# after existing syslog format deprecation in J
if CONF.use_syslog_rfc_format:
syslog = RFCSysLogHandler(address='/dev/log',
facility=facility)
else:
syslog = logging.handlers.SysLogHandler(address='/dev/log',
facility=facility)
log_root.addHandler(syslog)
logpath = _get_log_file_path()
if logpath:
filelog = logging.handlers.WatchedFileHandler(logpath)
@ -542,6 +542,20 @@ def _setup_logging_from_conf(project, version):
else:
logger.setLevel(level_name)
if CONF.use_syslog:
try:
facility = _find_facility_from_conf()
# TODO(bogdando) use the format provided by RFCSysLogHandler
# after existing syslog format deprecation in J
if CONF.use_syslog_rfc_format:
syslog = RFCSysLogHandler(facility=facility)
else:
syslog = logging.handlers.SysLogHandler(facility=facility)
log_root.addHandler(syslog)
except socket.error:
log_root.error('Unable to add syslog handler. Verify that syslog'
'is running.')
_loggers = {}
@ -611,6 +625,12 @@ class ContextFormatter(logging.Formatter):
def format(self, record):
"""Uses contextstring if request_id is set, otherwise default."""
# NOTE(jecarey): If msg is not unicode, coerce it into unicode
# before it can get to the python logging and
# possibly cause string encoding trouble
if not isinstance(record.msg, six.text_type):
record.msg = six.text_type(record.msg)
# store project info
record.project = self.project
record.version = self.version