-
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
Conversation
This will make it possible to customise the logging behaviour when the code is being run as a CLI / script / app (vs a library).
Fixes regression from [^1]. I've manually checked that using the CLI still generates logs as expected. The caplog fixture is necessary to capture loguru logs [^2]. [^1]: avaldebe@ecd0c9d [^2]: https://loguru.readthedocs.io/en/stable/resources/migration.html#making-things-work-with-pytest-and-caplog
084e84e
to
7a7ed8a
Compare
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'm not sure that this is the right approach for library usage.
In principle is it up to the library user to decide on the logger level.
|
||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
typer looks for an entry point called main
, but I want to make this a function so I can override the logging behaviour before the main CLI starts. We can't have:
main = Typer(...)
...
def main(): ...
@@ -1,3 +1,8 @@ | |||
from loguru import logger | |||
|
|||
logger.disable("pms") # disable logging by default |
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:
- The default logging level is "DEBUG", which is not a sensible default.
import loguru
loguru.logger.debug('test')
2023-01-07 10:27:45.491 | DEBUG | __main__:<module>:1 - test
- PyPMS logs now have to be configured using a different framework.
import logging
logging.configure({ ... }) # main logging for other libraries
import loguru
loguru.logger.configure(...) # special config just for PyPMS
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.
@@ -1,10 +1,19 @@ | |||
import pytest | |||
from _pytest.logging import LogCaptureFixture |
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.
Please do not import private modules. Use pytest.LogCaptureFixture
instread.
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 get an error if I do this:
E ModuleNotFoundError: No module named 'pytest.logging'
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
|
||
|
||
def main(): # pragma: no cover | ||
logger.enable("pms") |
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.
can this not be done on the CLI callback function?
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.
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.
I get your point. I'll merge your PR and publish a new version now, and will try to find a better solution later. |
Thanks @avaldebe - logs are now clean again 🎉. Appreciate it! |
Fixes regression from 1.
Previously this repo used Python's inbuilt logger, which does
not emit INFO or DEBUG logs by default. loguru emits all logs
by default. This fixes that by disabling loguru unless the code
is running in CLI mode, which is when logs are actually used.
Footnotes
https://github.com/avaldebe/PyPMS/commit/ecd0c9d1cd0e88284299fecc6416f5f01cadf10d ↩