Skip to content
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

Merged
merged 2 commits into from
Jan 11, 2023

Conversation

benthorner
Copy link
Contributor

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

  1. https://github.com/avaldebe/PyPMS/commit/ecd0c9d1cd0e88284299fecc6416f5f01cadf10d

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
Copy link
Owner

@avaldebe avaldebe left a 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")
Copy link
Owner

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?

Copy link
Contributor Author

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
Copy link
Owner

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.

Copy link
Contributor Author

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:

  1. 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
  1. 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
Copy link
Owner

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.

Copy link
Contributor Author

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")
Copy link
Owner

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?

Copy link
Contributor Author

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.

@benthorner
Copy link
Contributor Author

@avaldebe library users can still customise logging with the changes here - they just need to enable the logger first. The the new default logging level of "DEBUG" (for library users) is a regression from ecd0c9d.

How can we get back to that without requiring library users to learn about loguru?

@avaldebe
Copy link
Owner

I get your point. I'll merge your PR and publish a new version now, and will try to find a better solution later.

@avaldebe avaldebe merged commit 0d44ef0 into avaldebe:master Jan 11, 2023
@benthorner
Copy link
Contributor Author

Thanks @avaldebe - logs are now clean again 🎉. Appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants