Browse Source

change default log level from `DEBUG` to `INFO`; added option `--debug`

https://github.com/fphammerle/location-guessing-game-telegram-bot/commit/0ef6cbb5a7bfacc5d2135cd18461f6061d93c465
https://github.com/fphammerle/wireless-sensor/commit/5cc5b6656e768630455d003ff78b95c0f2332480
Fabian Peter Hammerle 2 years ago
parent
commit
de6d509bdd
3 changed files with 45 additions and 5 deletions
  1. 5 0
      CHANGELOG.md
  2. 10 5
      switchbot_mqtt/__init__.py
  3. 30 0
      tests/test_cli.py

+ 5 - 0
CHANGELOG.md

@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - command-line option `--fetch-device-info` enables battery level reports on topics
   `homeassistant/cover/{switchbot,switchbot-curtain}/MAC_ADDRESS/battery-percentage`
   after every command.
+- option `--debug` to change log level to `DEBUG`
+
+### Changed
+- changed default log level from `DEBUG` to `INFO`
+- shortened log format (revert with `--debug`)
 
 ### Removed
 - compatibility with `python3.5`

+ 10 - 5
switchbot_mqtt/__init__.py

@@ -492,11 +492,6 @@ def _run(
 
 
 def _main() -> None:
-    logging.basicConfig(
-        level=logging.DEBUG,
-        format="%(asctime)s:%(levelname)s:%(name)s:%(message)s",
-        datefmt="%Y-%m-%dT%H:%M:%S%z",
-    )
     argparser = argparse.ArgumentParser(
         description="MQTT client controlling SwitchBot button automators, "
         "compatible with home-assistant.io's MQTT Switch platform"
@@ -540,7 +535,17 @@ def _main() -> None:
         f" topic {_CurtainMotor.get_mqtt_position_topic(mac_address='MAC_ADDRESS')}"
         " after executing stop commands.",
     )
+    argparser.add_argument("--debug", action="store_true")
     args = argparser.parse_args()
+    # https://github.com/fphammerle/python-cc1101/blob/26d8122661fc4587ecc7c73df55b92d05cf98fe8/cc1101/_cli.py#L51
+    logging.basicConfig(
+        level=logging.DEBUG if args.debug else logging.INFO,
+        format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
+        if args.debug
+        else "%(message)s",
+        datefmt="%Y-%m-%dT%H:%M:%S%z",
+    )
+    _LOGGER.debug("args=%r", args)
     if args.mqtt_password_path:
         # .read_text() replaces \r\n with \n
         mqtt_password = args.mqtt_password_path.read_bytes().decode()

+ 30 - 0
tests/test_cli.py

@@ -17,6 +17,8 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 import json
+import logging
+import typing
 import unittest.mock
 
 import pytest
@@ -235,3 +237,31 @@ def test__main_fetch_device_info():
         # pylint: disable=protected-access
         switchbot_mqtt._main()
     run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
+
+
+@pytest.mark.parametrize(
+    ("additional_argv", "root_log_level", "log_format"),
+    [
+        ([], logging.INFO, "%(message)s"),
+        (
+            ["--debug"],
+            logging.DEBUG,
+            "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
+        ),
+    ],
+)
+def test__main_log_config(
+    additional_argv: typing.List[str], root_log_level: int, log_format: str
+):
+    with unittest.mock.patch(
+        "sys.argv", ["", "--mqtt-host", "localhost"] + additional_argv
+    ), unittest.mock.patch(
+        "logging.basicConfig"
+    ) as logging_basic_config_mock, unittest.mock.patch(
+        "switchbot_mqtt._run"
+    ):
+        # pylint: disable=protected-access
+        switchbot_mqtt._main()
+    logging_basic_config_mock.assert_called_once_with(
+        level=root_log_level, format=log_format, datefmt="%Y-%m-%dT%H:%M:%S%z"
+    )