Fix Formatter subclasses for Python 3.2+

Python 3.2 added the 'style' parameter to 'logging.Formatter.__init__'.
This is provided by 'logging.config.fileConfig' meaning this currently
raises a 'TypeError' similar to the below when the incompatible
formatters, JSONFormatter and FluentFormatter, are used with Python 3.2
or greater.

  TypeError: __init__() got an unexpected keyword argument 'style'

Resolve this by simply adding the parameter to the list. There is more
work we can do here (like actually supporting this parameter) but that's
a job for another patch.

Note that we can't actually test this as doing so would involve invoking
'logging.config.fileConfig', which in turn would modify the global state
of the 'logging' module. You can thank the singleton pattern for that.

Change-Id: I9b339163ddfe440bc6782ced33595a0dcf60f658
Closes-Bug: 1739743
Co-Authored-By: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Suff 2018-01-17 10:47:11 +03:00 committed by Stephen Finucane
parent 33b5b3fc47
commit 89bbb3fb79
1 changed files with 7 additions and 6 deletions

View File

@ -181,9 +181,10 @@ class _ReplaceFalseValue(dict):
class JSONFormatter(logging.Formatter):
def __init__(self, fmt=None, datefmt=None):
# NOTE(jkoelker) we ignore the fmt argument, but its still there
# since logging.config.fileConfig passes it.
def __init__(self, fmt=None, datefmt=None, style='%'):
# NOTE(sfinucan) we ignore the fmt and style arguments, but they're
# still there since logging.config.fileConfig passes the former in
# Python < 3.2 and both in Python >= 3.2
self.datefmt = datefmt
try:
self.hostname = socket.gethostname()
@ -277,9 +278,9 @@ class FluentFormatter(logging.Formatter):
.. versionadded:: 3.17
"""
def __init__(self, fmt=None, datefmt=None):
# NOTE(masaki) we ignore the fmt argument because of the same reason
# with JSONFormatter.
def __init__(self, fmt=None, datefmt=None, style='%s'):
# NOTE(sfinucan) we ignore the fmt and style arguments for the same
# reason as JSONFormatter.
self.datefmt = datefmt
try:
self.hostname = socket.gethostname()