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

Intended way to get DEBUG logging? #83

Open
kannes opened this issue Aug 12, 2024 · 3 comments
Open

Intended way to get DEBUG logging? #83

kannes opened this issue Aug 12, 2024 · 3 comments

Comments

@kannes
Copy link

kannes commented Aug 12, 2024

I cannot figure out how to get DEBUG logging messages to appear with this. I have not traced a message through all the code so sorry if below is stupid:

Logging is set up following https://github.com/GispoCoding/qgis_plugin_tools/blob/main/docs/usage.md#logging

A LOGGER.debug('Log some debug messages') just goes into the void. I am looking at stdout/stderr and the QGIS message log. The docs made me expect that debug messages would be handled in a "visible" way.

I see that the plugin has some logic around logging levels where DEBUG is turned into QGIS' INFO level (as QGIS has no DEBUG level). That seems to work fine by itself. Yet the messages do not appear in the QGIS message log.

In

class LogTarget(Enum):
"""Log target with default logging level as value"""
STREAM = {"id": "stream", "default": "INFO"}
FILE = {"id": "file", "default": "INFO"}
BAR = {"id": "bar", "default": "INFO"}
there are three handlers defined, all with INFO as level. Their levels are used in other places to set the minimum common level. So just looking at that, it makes sense that DEBUG messages are not logged. But I thought they would be turned into INFO level automagically somewhere?

I tried LOGGER.setLevel(logging.DEBUG) with no luck.

I tried setting all the handlers levels to DEBUG, this made them appear as DEBUG logs in the terminal and INFO logs in QGIS. How can I achieve that without modifying the code of this module?

@kannes
Copy link
Author

kannes commented Aug 16, 2024

I will dive deeper into this today and maybe try to add a logging_level parameter to setup_logger if that makes sense.

@kannes
Copy link
Author

kannes commented Aug 16, 2024

[MyPlugin]
log_level\bar=WARNING
log_level\file=CRITICAL
log_level\stream=DEBUG

This took quite a bit of time and then it made me give up trying to think of a reasonable way to integrate in the tools, and go for a quick fix for just my plugin instead:

I went with external overriding in my plugin's __init__.py (as early as possible to avoid confusion if imported modules do stuff with logging during import):

LOGGER = setup_logger(__name__.split(".")[0])

# Initially set DEBUG logging
LOGGER.setLevel(logging.DEBUG)
for handler in LOGGER.handlers:
    handler.setLevel(logging.DEBUG)

# read wanted logging level from somewhere (during which I might want DEBUG logging)
logging_level = ...  

# set the wanted logging level
LOGGER.setLevel(logging_level)
for handler in LOGGER.handlers:
    handler.setLevel(logging_level)

That way I can use LOGGER = logging.getLogger(__name__) anywhere else and have it use the correct level.

@kannes
Copy link
Author

kannes commented Aug 22, 2024

Warning, weird footgun! Due to reasons I switched to using the same logger everywhere via:

setup_logger(plugin_name()) and LOGGER = logging.getLogger(plugin_name())

Don't ask me why, but now I could not use the logger object returned by setup_logger anymore to set the logging level. It turned out to be a different logger:

>>> LOGGER = setup_logger(plugin_name())
>>> print(f"{id(LOGGER)=}")
id(LOGGER)=139887092455936
>>> LOGGER = logging.getLogger(plugin_name())
>>> print(f"{id(LOGGER)=}")
id(LOGGER)=139887097284736

Changing the level of the logger returned by setup_logger(plugin_name()) did nothing for the logger I used everywhere else via logging.getLogger(plugin_name()) D:

Instead I went with:

setup_logger(plugin_name())
LOGGER = logging.getLogger(plugin_name())
...  # as above

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

No branches or pull requests

1 participant