Logging messages in Pastas#

Pastas has a dedicated logger to log messages using Python’s native logging module. By default, all the log-messages are printed to the screen. The user can choose which (level of) log-messages to print to screen. This notebook shows various examples of how to configure the Pastas logger.

import logging

import pandas as pd

import pastas as ps
# When the script running pastas (this script) does not initialize a logger,
# only warnings and errors are printed to the console
obs = pd.read_csv("data/B58C0698001.csv", parse_dates=[0], index_col=0).squeeze()

ts = ps.Model(obs)
The series 'B58C0698_1' has nan-values. Pastas will use the `fill_nan` from the StressModel's settings (ps.timeseries.settings) parsed to the TimeSeries settings to fill up the nan-values.
# when we set the level to ERROR we do not see the warning anymore
ps.set_log_level("ERROR")

ts = ps.Model(obs)
# when we set the log-level to "INFO", info-messages are still not printed
ps.set_log_level("INFO")

ts = ps.Model(obs)
The series 'B58C0698_1' has nan-values. Pastas will use the `fill_nan` from the StressModel's settings (ps.timeseries.settings) parsed to the TimeSeries settings to fill up the nan-values.
# we need to initialize a handler to also print info-messages
# we can add a handler only for pastas:
ps.utils.initialize_logger()

ts = ps.Model(obs)
WARNING: The series 'B58C0698_1' has nan-values. Pastas will use the `fill_nan` from the StressModel's settings (ps.timeseries.settings) parsed to the TimeSeries settings to fill up the nan-values.
INFO: Time Series 'B58C0698_1': 6 nan-value(s) was/were found and filled with: drop.
# remove the handler again, as we do not want double log-messages after the next step
ps.utils.remove_console_handler()
# or we can set a handler directly via the logging package:
logging.basicConfig(level=logging.INFO)

ts = ps.Model(obs)
WARNING:pastas.timeseries:The series 'B58C0698_1' has nan-values. Pastas will use the `fill_nan` from the StressModel's settings (ps.timeseries.settings) parsed to the TimeSeries settings to fill up the nan-values.
INFO:pastas.timeseries:Time Series 'B58C0698_1': 6 nan-value(s) was/were found and filled with: drop.
# when we also want log-information saved to file, we add file-handlers
ps.utils.add_file_handlers(ps.logger)
ts = ps.Model(obs)
WARNING:pastas.timeseries:The series 'B58C0698_1' has nan-values. Pastas will use the `fill_nan` from the StressModel's settings (ps.timeseries.settings) parsed to the TimeSeries settings to fill up the nan-values.
INFO:pastas.timeseries:Time Series 'B58C0698_1': 6 nan-value(s) was/were found and filled with: drop.
# to get the default logger back we initialize it again
ps.utils.initialize_logger(ps.logger)