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

[ENHANCEMENT] Remove logger-initialization from Pastas #733

Open
rubencalje opened this issue Apr 10, 2024 · 2 comments
Open

[ENHANCEMENT] Remove logger-initialization from Pastas #733

rubencalje opened this issue Apr 10, 2024 · 2 comments
Labels
enhancement Indicates improvement of existing features priority 1 normal, deal with in the foreseeable future

Comments

@rubencalje
Copy link
Collaborator

rubencalje commented Apr 10, 2024

Describe the proposed enhancement
Right now when importing pastas a specific logger for Pastas is initialized, by the following two lines in __init__.py:

logger = logging.getLogger(__name__)
initialize_logger(logger)

This adds a logger with a default level of "INFO". This is useful, as the user then sees all messages in his console. For example, when running example.py (with PR 678), you see:

WARNING: Cannot solve with noise=True while no noisemodel is present. Add a noisemodel with Model.add_noisemodel(ps.NoiseModel()) to replicate results from pastas < 1.5. Solving without a noisemodel.
INFO: Time Series 'rain' was extended in the past to 1975-11-17 00:00:00 with the mean value (0.0021) of the time series.
INFO: Time Series 'evap' was extended in the past to 1975-11-17 00:00:00 with the mean value (0.0016) of the time series.

If the user wants to change the log-level to WARNING, he can use the code ps.set_log_level("WARNING"), and you can remove the logger with the line ps.utils.remove_console_handler(). This works, but is made up by us (me), and I think is not the general way to do it Python. Integrating Pastas in other packages is therefore not so straightforward, and may result in double log-messages: one of the pastas-logger, and one of the logger initiated by the user.

Therefore, it might be better to not initialize a logger in __init__.py anymore, by removing the two lines at the top of this message. Then, by default of the logging-package, only warnings will be displayed. When running example.py (with PR 678) you then see:

Cannot solve with noise=True while no noisemodel is present. Add a noisemodel with Model.add_noisemodel(ps.NoiseModel()) to replicate results from pastas < 1.5. Solving without a noisemodel.

If as user wants to see log-messages of other log-levels, he can set this up with the logging-package (https://docs.python.org/3/library/logging.html):

import logging
logging.basicConfig(level=logging.INFO)

When running example.py (with PR 678) you then see:

WARNING:pastas.model:Cannot solve with noise=True while no noisemodel is present. Add a noisemodel with Model.add_noisemodel(ps.NoiseModel()) to replicate results from pastas < 1.5. Solving without a noisemodel.
INFO:pastas.timeseries:Time Series 'rain' was extended in the past to 1975-11-17 00:00:00 with the mean value (0.0021) of the time series.
INFO:pastas.timeseries:Time Series 'evap' was extended in the past to 1975-11-17 00:00:00 with the mean value (0.0016) of the time series.

Removing the logger-initialization from the import of Pastas will improve integration in people's scripts. However, it may result in people missing info-messages, as is shown above. This may be a good change for Pastas 2.0.

@rubencalje rubencalje added the enhancement Indicates improvement of existing features label Apr 10, 2024
@dbrakenhoff
Copy link
Member

Is it possible to set a log level per package? I've often gotten a bit lost trying to limit the INFO messages from e.g. hydropandas, but keeping the INFO messages from pastas.

@rubencalje
Copy link
Collaborator Author

rubencalje commented Apr 10, 2024

Is it possible to set a log level per package? I've often gotten a bit lost trying to limit the INFO messages from e.g. hydropandas, but keeping the INFO messages from pastas.

You can do (https://stackoverflow.com/questions/7234262/how-to-implement-different-levels-for-specific-modules-in-python):

import logging
logging.basicConfig(level=logging.WARNING)
logging.getLogger("pastas").setLevel(logging.INFO)

@pastas pastas deleted a comment from quanerpython Apr 12, 2024
@martinvonk martinvonk added the priority 1 normal, deal with in the foreseeable future label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Indicates improvement of existing features priority 1 normal, deal with in the foreseeable future
Projects
None yet
Development

No branches or pull requests

3 participants