Fix common.log.ContextFormatter for Python 3
The incompatibilty of common.log.ContextFormatter with Python 3 is due to the use of 'hack' with Formatter._fmt, which is not working since Python 3.2 after introduction of different styles of formatting syntax. The formatting string in Python3 should be stored in Formatter._style object. In this patch the property where the formatting string should be written is selected based on the current version of Python. Change-Id: Ibb2244ce95137996ad927fbb9d4380a0e32d8b46
This commit is contained in:
@@ -666,14 +666,19 @@ class ContextFormatter(logging.Formatter):
|
||||
record.__dict__[key] = ''
|
||||
|
||||
if record.__dict__.get('request_id'):
|
||||
self._fmt = CONF.logging_context_format_string
|
||||
fmt = CONF.logging_context_format_string
|
||||
else:
|
||||
self._fmt = CONF.logging_default_format_string
|
||||
fmt = CONF.logging_default_format_string
|
||||
|
||||
if (record.levelno == logging.DEBUG and
|
||||
CONF.logging_debug_format_suffix):
|
||||
self._fmt += " " + CONF.logging_debug_format_suffix
|
||||
fmt += " " + CONF.logging_debug_format_suffix
|
||||
|
||||
if sys.version_info < (3, 2):
|
||||
self._fmt = fmt
|
||||
else:
|
||||
self._style = logging.PercentStyle(fmt)
|
||||
self._fmt = self._style._fmt
|
||||
# Cache this on the record, Logger will respect our formatted copy
|
||||
if record.exc_info:
|
||||
record.exc_text = self.formatException(record.exc_info, record)
|
||||
|
||||
Reference in New Issue
Block a user