From 1945b8df0bec32cfdbe817d6ca9a562ae0f47a2b Mon Sep 17 00:00:00 2001 From: Marvin Arnold Date: Fri, 17 Sep 2021 18:16:53 -0500 Subject: [PATCH] feat: add logger It is helpful to have verbose logs. This logger has been copied from hm-config because all services should use the same format. --- .github/workflows/unit-tests.yml | 1 + README.md | 19 +++++++++++++++++ hm_pyhelper/tests/utils/test_logger.py | 28 ++++++++++++++++++++++++++ hm_pyhelper/utils/logger.py | 23 +++++++++++++++++++++ setup.py | 2 +- 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 hm_pyhelper/tests/utils/test_logger.py create mode 100644 hm_pyhelper/utils/logger.py diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 43d16a8..289339e 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -26,4 +26,5 @@ jobs: - name: Run tests run: | + export PYTHONPATH=`pwd` pytest diff --git a/README.md b/README.md index d0e8052..7b9ebc1 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,22 @@ Please note, DIY Hotspots do not earn HNT. | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | Pi Supply IoT LoRa Gateway HAT | RPi | DIY-PISLGH | 0.0 | 22 | | | Light | False | Any pi with 40 pin header | | RAK2287 | RPi | DIY-RAK2287 | 0.0 | 17 | | | Light | False | Any pi with 40 pin header | +## utils + +### logger + +```python +from hm_pyhelper.utils import logger +logger = get_logger(__name__) +logger.debug("message to log") +``` + +## Testing + +To run tests: + +```bash +pip install -r requirements.txt +pip install -r test-requirements.txt +PYTHONPATH=./ pytest +``` diff --git a/hm_pyhelper/tests/utils/test_logger.py b/hm_pyhelper/tests/utils/test_logger.py new file mode 100644 index 0000000..a43d518 --- /dev/null +++ b/hm_pyhelper/tests/utils/test_logger.py @@ -0,0 +1,28 @@ +from unittest import TestCase +from hm_pyhelper.utils.logger import get_logger, _log_format +import re +import logging + + +class TestExample(TestCase): + def test_logging(self): + logger = get_logger(__name__) + + with self.assertLogs() as captured: + logger.debug("Hello world.") + + # check that there is only one log message + self.assertEqual(len(captured.records), 1) + record = captured.records[0] + formatter = logging.Formatter(_log_format) + formatted_output = formatter.format(record) + + # Do not check timestamp and filepath because those change + # based on the environment and run time + expected_partial_output_regex = re.escape( + " - [DEBUG] - test_logger - (test_logger.py).test_logging -- ") + expected_output_regex = ".*" + \ + expected_partial_output_regex + ".*" + \ + " - Hello world." + are_logs_correct = re.search(expected_output_regex, formatted_output) + self.assertTrue(are_logs_correct) diff --git a/hm_pyhelper/utils/logger.py b/hm_pyhelper/utils/logger.py new file mode 100644 index 0000000..2d4ee34 --- /dev/null +++ b/hm_pyhelper/utils/logger.py @@ -0,0 +1,23 @@ +import os +import logging + +LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG") +_log_format = f"%(asctime)s - [%(levelname)s] - %(name)s - (%(filename)s).%(funcName)s -- %(pathname)s:(%(lineno)d) - %(message)s" # noqa: F541 E501 + + +def get_stream_handler(): + stream_handler = logging.StreamHandler() + stream_handler.setLevel(LOGLEVEL) + stream_handler.setFormatter(logging.Formatter(_log_format)) + return stream_handler + + +def get_logger(name): + logger = logging.getLogger(name) + logger.setLevel(LOGLEVEL) + logger.addHandler(get_stream_handler()) + return logger + + +def log(class_name): + return get_logger(class_name) diff --git a/setup.py b/setup.py index 3192e82..cb559ab 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='hm_pyhelper', - version='0.6.1', + version='0.6.3', author="Nebra Ltd", author_email="support@nebra.com", description="Helium Python Helper",