Skip to content

Commit

Permalink
separate loggers for each module and exception stack traces in debug …
Browse files Browse the repository at this point in the history
…log level
  • Loading branch information
urbas committed Nov 17, 2024
1 parent 3558d6b commit 491e765
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 9 deletions.
10 changes: 6 additions & 4 deletions py_air_control_exporter/fetchers/http_philips.py
Original file line number Diff line number Diff line change
@@ -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}

Expand All @@ -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


Expand Down
3 changes: 0 additions & 3 deletions py_air_control_exporter/logging.py

This file was deleted.

3 changes: 2 additions & 1 deletion py_air_control_exporter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion py_air_control_exporter/metrics.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import itertools
import logging
from collections.abc import Iterable
from dataclasses import dataclass

import prometheus_client.core
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)
Expand Down Expand Up @@ -34,6 +36,7 @@ def collect(self):
name,
ex,
)
LOG.debug("Exception stack trace:", exc_info=True)
targets_with_errors.add(name)
continue

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions test/fetchers/test_http_philips.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 491e765

Please sign in to comment.