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

Logging is silently disabled on agnpy import #155

Open
pguenth opened this issue Mar 12, 2024 · 3 comments
Open

Logging is silently disabled on agnpy import #155

pguenth opened this issue Mar 12, 2024 · 3 comments

Comments

@pguenth
Copy link

pguenth commented Mar 12, 2024

Hey all :)

Importing agnpy seems to set the configuration for the python logging library, although in a quick search I could not find out why. Since the logging library silently ignores further configuration calls to logging.basicConfig(...) for some reason I do not understand, this leads to the confusing/frustrating situation that no logging output is shown anymore.

I'm not sure I would consider this a bug in agnpy, but it will probably cause some frustration further down the line nevertheless. So I think it would be better to avoid the problem if that is possible.

Example code:

import logging
import agnpy # removing this line shows the log output

logging.basicConfig(level=logging.INFO)
logging.info("This line is not shown as long as agnpy is imported.")

Note: There is also the kwarg force=True that one can set in basicConfig to circumvent this problem.

@cosimoNigro
Copy link
Owner

cosimoNigro commented Mar 21, 2024

Hello @pguenth,

thanks for reporting this!
I think this is the only point where I use the logging

import logging

i.e. to notify the user if sherpa and Gammapy are not installed.

Can it be that by re-importing logging here it is setting the logging back to the default level (that should be WARNING or ERROR, right)?
Perhaps adding a
logger = logging.getLogger(__name__)
and then using logger.info("...")
in fit/__init__.py will fix the issue (I think it will import the logger already defined).

@pguenth
Copy link
Author

pguenth commented Mar 21, 2024

As far as I understand the logging library, this should not make a difference. Just using logging.info() and similar calls (instead of using a particular logger) should just default to some default logger. I will try to think of a test to find out.

@pguenth
Copy link
Author

pguenth commented Mar 21, 2024

Okay the behavior of the logging module is apparently even more unexpected (at least to me):

The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger.

(https://docs.python.org/3/library/logging.html#logging.basicConfig)

So the problem is most likely that agnpy's call to logging.warning() in the file you mentioned implicitely calls logging.basicConfig() with some defaults, which prevents further code (that imports agnpy) to configure the root logger.

The solution you proposed seems to be the best in my opinion (although for a slightly different reason) and is also what the documentation recommends.

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

2 participants