From 89bbb3fb79ecfa45ed41afee86900131b3e37dc6 Mon Sep 17 00:00:00 2001 From: Suff Date: Wed, 17 Jan 2018 10:47:11 +0300 Subject: [PATCH] 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 --- oslo_log/formatters.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/oslo_log/formatters.py b/oslo_log/formatters.py index 9277856c..25450354 100644 --- a/oslo_log/formatters.py +++ b/oslo_log/formatters.py @@ -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()