From 491e76593c1d61ca4bdefc5d425686496117980d Mon Sep 17 00:00:00 2001 From: Matej Urbas Date: Sun, 17 Nov 2024 16:09:44 +0000 Subject: [PATCH] separate loggers for each module and exception stack traces in debug log level --- py_air_control_exporter/fetchers/http_philips.py | 10 ++++++---- py_air_control_exporter/logging.py | 3 --- py_air_control_exporter/main.py | 3 ++- py_air_control_exporter/metrics.py | 5 ++++- pyproject.toml | 1 + test/fetchers/test_http_philips.py | 1 + 6 files changed, 14 insertions(+), 9 deletions(-) delete mode 100644 py_air_control_exporter/logging.py diff --git a/py_air_control_exporter/fetchers/http_philips.py b/py_air_control_exporter/fetchers/http_philips.py index bb6bb03..0f6fbe6 100644 --- a/py_air_control_exporter/fetchers/http_philips.py +++ b/py_air_control_exporter/fetchers/http_philips.py @@ -1,7 +1,10 @@ +import logging + from pyairctrl import http_client from py_air_control_exporter import fetchers_api -from py_air_control_exporter.logging import LOG + +LOG = logging.getLogger(__name__) _FAN_SPEED_TO_INT = {"s": 0, "1": 1, "2": 2, "3": 3, "t": 4} @@ -26,10 +29,9 @@ def get_reading( ) except Exception as ex: LOG.error( - "Could not read values from air control device %s. Error: %s", - host, - ex, + "Could not read values from air control device %s. Error: %s", host, ex ) + LOG.debug("Exception stack trace:", exc_info=True) return None diff --git a/py_air_control_exporter/logging.py b/py_air_control_exporter/logging.py deleted file mode 100644 index f5c40aa..0000000 --- a/py_air_control_exporter/logging.py +++ /dev/null @@ -1,3 +0,0 @@ -import logging - -LOG = logging.getLogger("py-air-control-exporter") diff --git a/py_air_control_exporter/main.py b/py_air_control_exporter/main.py index 7474d58..e07f582 100644 --- a/py_air_control_exporter/main.py +++ b/py_air_control_exporter/main.py @@ -8,7 +8,8 @@ from py_air_control_exporter import app, fetchers_api, metrics from py_air_control_exporter.fetchers import fetcher_registry -from py_air_control_exporter.logging import LOG + +LOG = logging.getLogger(__name__) @click.command() diff --git a/py_air_control_exporter/metrics.py b/py_air_control_exporter/metrics.py index c5ac935..05af949 100644 --- a/py_air_control_exporter/metrics.py +++ b/py_air_control_exporter/metrics.py @@ -1,4 +1,5 @@ import itertools +import logging from collections.abc import Iterable from dataclasses import dataclass @@ -6,7 +7,8 @@ from prometheus_client import Metric, registry from py_air_control_exporter import fetchers_api -from py_air_control_exporter.logging import LOG + +LOG = logging.getLogger(__name__) @dataclass(frozen=True) @@ -34,6 +36,7 @@ def collect(self): name, ex, ) + LOG.debug("Exception stack trace:", exc_info=True) targets_with_errors.add(name) continue diff --git a/pyproject.toml b/pyproject.toml index 796d144..4dd2bd9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,6 +50,7 @@ lint.ignore = [ "S101", # Use of `assert` detected "PT005", # fixture with leading `_` returns a value "PLR2004", # magic value in comparison + "TRY400", # use logging.exception ] [tool.hatch.build.targets.wheel] diff --git a/test/fetchers/test_http_philips.py b/test/fetchers/test_http_philips.py index 3515502..bc64a4a 100644 --- a/test/fetchers/test_http_philips.py +++ b/test/fetchers/test_http_philips.py @@ -9,6 +9,7 @@ def test_metrics_pyairctrl_failure(mock_http_client, caplog): """Error logs explain that there was a failure getting the status from pyairctrl""" + caplog.set_level(logging.ERROR) mock_http_client["get_status"].side_effect = Exception("Some foobar error") assert http_philips.get_reading(host="1.2.3.4") is None assert "Could not read values from air control device" in caplog.text