-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable logging unless code is running in CLI #37
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,20 +10,24 @@ | |
import importlib_metadata as metadata | ||
|
||
from loguru import logger | ||
from typer import Argument, Context, Exit, Option, Typer, echo | ||
from typer import Argument, Context, Exit, Option, Typer, echo, run | ||
|
||
from pms.core import MessageReader, SensorReader, Supported, exit_on_fail | ||
|
||
main = Typer(help="Data acquisition and logging for Air Quality Sensors with UART interface") | ||
|
||
app = Typer(help="Data acquisition and logging for Air Quality Sensors with UART interface") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why change the name of the CLI entry point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typer looks for an entry point called
|
||
|
||
""" | ||
Extra cli commands from plugins | ||
|
||
additional Typer commands are loaded from plugins (entry points) advertized as `"pypms.extras"` | ||
""" | ||
for ep in metadata.entry_points(group="pypms.extras"): | ||
main.command(name=ep.name)(ep.load()) | ||
app.command(name=ep.name)(ep.load()) | ||
|
||
|
||
def main(): # pragma: no cover | ||
logger.enable("pms") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this not be done on the CLI callback function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I did try this originally but it doesn't work for testing: some of the other tests execute the callback, which overrides the package default (that logging is disabled). Since the purpose of the test I added is to check for regression in the package default, I needed to put this line somewhere where no other test would cause it to run. I admit it's not a great change or a great test, but since this is the second PR to try and make logging work better for library users, I wanted to put something in to protect the behaviour. |
||
app() | ||
|
||
|
||
def version_callback(value: bool): # pragma: no cover | ||
|
@@ -35,7 +39,7 @@ def version_callback(value: bool): # pragma: no cover | |
raise Exit() | ||
|
||
|
||
@main.callback() | ||
@app.callback() | ||
def callback( | ||
ctx: Context, | ||
model: Supported = Option(Supported.default, "--sensor-model", "-m", help="sensor model"), | ||
|
@@ -60,7 +64,7 @@ def callback( | |
ctx.obj = {"reader": SensorReader(model, port, seconds, samples)} | ||
|
||
|
||
@main.command() | ||
@app.command() | ||
def info(ctx: Context): # pragma: no cover | ||
"""Information about the sensor observations""" | ||
sensor = ctx.obj["reader"].sensor | ||
|
@@ -84,7 +88,7 @@ def __str__(self) -> str: | |
return self.value | ||
|
||
|
||
@main.command() | ||
@app.command() | ||
def serial( | ||
ctx: Context, | ||
format: Optional[Format] = Option(None, "--format", "-f", help="formatted output"), | ||
|
@@ -111,7 +115,7 @@ def serial( | |
echo(str(obs)) | ||
|
||
|
||
@main.command() | ||
@app.command() | ||
def csv( | ||
ctx: Context, | ||
capture: bool = Option(False, "--capture", help="write raw messages instead of observations"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import pytest | ||
from _pytest.logging import LogCaptureFixture | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not import private modules. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get an error if I do this:
I'm just following loguru's own docs here: https://loguru.readthedocs.io/en/stable/resources/migration.html#making-things-work-with-pytest-and-caplog |
||
from loguru import logger | ||
|
||
from pms import SensorWarmingUp, SensorWarning | ||
from pms.core.reader import SensorReader, UnableToRead | ||
from pms.core.sensor import Sensor | ||
|
||
|
||
@pytest.fixture | ||
def caplog(caplog: LogCaptureFixture): | ||
handler_id = logger.add(caplog.handler, format="{message}") | ||
yield caplog | ||
logger.remove(handler_id) | ||
|
||
|
||
@pytest.fixture | ||
def mock_sleep(monkeypatch): | ||
def sleep(seconds): | ||
|
@@ -233,3 +242,15 @@ def test_reader_sensor_no_response(reader: SensorReader): | |
pass | ||
|
||
assert "did not respond" in str(e.value) | ||
|
||
|
||
def test_logging(reader: SensorReader, capfd, caplog): | ||
with reader: | ||
obs = tuple(reader()) | ||
|
||
# check data was read | ||
assert len(obs) == 1 | ||
assert obs[0].pm10 == 11822 # type:ignore | ||
|
||
# check no logs output | ||
assert caplog.text == "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that disabling the longing is the right behaviour for library use.
In principle, the library user should decide the logging level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have two problems with this:
Normally I don't need DEBUG logs for PyPMS. If I do need them, then as a developer I am willing to go find out how to turn them on. Conversely, when I start using a library I don't want to spend extra effort working out how to switch all the DEBUG logs off to stop them polluting my normal / day-to-day logs.
logger.disable("pms")
seemed like the easiest solution to get back to a sensible default for library users. I also think some documentation would be helpful, but that should be a secondary priority to just having some sensible defaults in the first place - the priority should be ease of use / minimal setup effort.