From 741cffdfe7a5188dfbd1e471d5b6d56f522ff85e Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Fri, 28 Jan 2022 01:21:31 +0900 Subject: [PATCH] Use LOG.warning instead of deprecated LOG.warn The LOG.warn method is deprecated[1] and the LOG.warning method should be used instead. [1] https://docs.python.org/3/library/logging.html#logging.warning Change-Id: I7e3dc5d1897cd10b94e0a5a5a06db667cba7d443 --- HACKING.rst | 2 ++ masakarimonitors/hacking/checks.py | 15 +++++++++++++++ .../introspectiveinstancemonitor/qemu_utils.py | 8 ++++---- masakarimonitors/tests/unit/test_hacking.py | 12 ++++++++++++ tox.ini | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index a2754da..b3b107e 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -9,3 +9,5 @@ masakari-monitors Specific Commandments - [M301] Ensure that the _() function is explicitly imported to ensure proper translations. - [M302] Validate that log messages are not translated. - [M303] Yield must always be followed by a space when yielding a value. +- [M304] Check for usage of deprecated assertRaisesRegexp +- [M305] LOG.warn is deprecated. Enforce use of LOG.warning. diff --git a/masakarimonitors/hacking/checks.py b/masakarimonitors/hacking/checks.py index 89a00d4..a2a4004 100644 --- a/masakarimonitors/hacking/checks.py +++ b/masakarimonitors/hacking/checks.py @@ -114,3 +114,18 @@ def assert_raisesRegexp(logical_line): if res: yield (0, "M304: assertRaisesRegex must be used instead " "of assertRaisesRegexp") + + +@core.flake8ext +def no_log_warn(logical_line): + """Disallow 'LOG.warn(' + + LOG.warn() is deprecated and LOG.warning should be used instead. + https://docs.python.org/3/library/logging.html#logging.warning + + N352 + """ + + msg = ("M305: LOG.warn is deprecated, please use LOG.warning!") + if "LOG.warn(" in logical_line: + yield (0, msg) diff --git a/masakarimonitors/introspectiveinstancemonitor/qemu_utils.py b/masakarimonitors/introspectiveinstancemonitor/qemu_utils.py index d94a9b5..51ce281 100755 --- a/masakarimonitors/introspectiveinstancemonitor/qemu_utils.py +++ b/masakarimonitors/introspectiveinstancemonitor/qemu_utils.py @@ -271,8 +271,8 @@ class QemuGuestAgent(object): self._getJournalObject(domain_uuid).processEvent('report') self._getJournalObject(domain_uuid).setSentNotification(True) except Exception: - LOG.warn('Exception :' + domain_uuid + - ' @ ' + get_function_name()) + LOG.warning('Exception :' + domain_uuid + + ' @ ' + get_function_name()) pass def _qemuAgentGuestPing(self, domain, timeout, flags=0): @@ -405,10 +405,10 @@ class QemuGuestAgent(object): do_qemuAgentGuestPing(domain, ICONF.guest_monitoring_timeout) except libvirt.libvirtError as le: - LOG.warn(le) + LOG.warning(le) continue except Exception as e: - LOG.warn(e) + LOG.warning(e) pass diff --git a/masakarimonitors/tests/unit/test_hacking.py b/masakarimonitors/tests/unit/test_hacking.py index 129bec9..811d0b9 100644 --- a/masakarimonitors/tests/unit/test_hacking.py +++ b/masakarimonitors/tests/unit/test_hacking.py @@ -145,3 +145,15 @@ class HackingTestCase(testtools.TestCase): yieldx_func(a, b) """ self._assert_has_no_errors(code, checks.yield_followed_by_space) + + def test_no_log_warn(self): + code = """ + LOG.warn("LOG.warn is deprecated") + """ + errors = [(1, 0, 'M305')] + self._assert_has_errors(code, checks.no_log_warn, + expected_errors=errors) + code = """ + LOG.warning("LOG.warn is deprecated") + """ + self._assert_has_no_errors(code, checks.no_log_warn) diff --git a/tox.ini b/tox.ini index 0b33446..2cca0a2 100644 --- a/tox.ini +++ b/tox.ini @@ -115,6 +115,7 @@ extension = M302 = checks:no_translate_logs M303 = checks:yield_followed_by_space M304 = checks:assert_raisesRegexp + M305 = checks:no_log_warn paths = ./masakarimonitors/hacking [testenv:bindep]