From 7a6ac578b41292c7a36d72bc259fa48456d22410 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 22 Mar 2020 05:29:50 -0700 Subject: [PATCH] Fix script logging with name (#33120) --- homeassistant/helpers/script.py | 4 +++- tests/helpers/test_script.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 937a675aada..4fe6d062bd5 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -701,7 +701,9 @@ class Script: def _log(self, msg, *args, level=logging.INFO): if self.name: - msg = f"{self.name}: {msg}" + msg = f"%s: {msg}" + args = [self.name, *args] + if level == _LOG_EXCEPTION: self._logger.exception(msg, *args) else: diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 443b131b2aa..5f0281d3f95 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -1743,3 +1743,15 @@ async def test_if_running_parallel(hass): assert len(events) == 4 assert events[2].data["value"] == 2 assert events[3].data["value"] == 2 + + +async def test_script_logging(caplog): + """Test script logging.""" + script_obj = script.Script(None, [], "Script with % Name") + script_obj._log("Test message with name %s", 1) + + assert "Script with % Name: Test message with name 1" in caplog.text + + script_obj = script.Script(None, []) + script_obj._log("Test message without name %s", 2) + assert "Test message without name 2" in caplog.text