[ENHANCEMENT] Remove logger-initialization from Pastas
rubencalje opened this issue · comments
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.
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.
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)
Closing after merging #772