{ "cells": [ { "cell_type": "markdown", "id": "f379db1e", "metadata": {}, "source": [ "# Logging messages in Pastas\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "c89ad8ce", "metadata": {}, "outputs": [], "source": [ "import logging\n", "\n", "import pandas as pd\n", "\n", "import pastas as ps" ] }, { "cell_type": "code", "execution_count": null, "id": "1473d427", "metadata": {}, "outputs": [], "source": [ "# When the script running pastas (this script) does not initialize a logger,\n", "# only warnings and errors are printed to the console\n", "obs = pd.read_csv(\"data/B58C0698001.csv\", parse_dates=[0], index_col=0).squeeze()\n", "\n", "ts = ps.Model(obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "d5f289f1", "metadata": {}, "outputs": [], "source": [ "# when we set the level to ERROR we do not see the warning anymore\n", "ps.set_log_level(\"ERROR\")\n", "\n", "ts = ps.Model(obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "db8a373a", "metadata": {}, "outputs": [], "source": [ "# when we set the log-level to \"INFO\", info-messages are still not printed\n", "ps.set_log_level(\"INFO\")\n", "\n", "ts = ps.Model(obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "48ed31b9", "metadata": {}, "outputs": [], "source": [ "# we need to initialize a handler to also print info-messages\n", "# we can add a handler only for pastas:\n", "ps.utils.initialize_logger()\n", "\n", "ts = ps.Model(obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "cf8415e5", "metadata": {}, "outputs": [], "source": [ "# remove the handler again, as we do not want double log-messages after the next step\n", "ps.utils.remove_console_handler()" ] }, { "cell_type": "code", "execution_count": null, "id": "713cc43e", "metadata": {}, "outputs": [], "source": [ "# or we can set a handler directly via the logging package:\n", "logging.basicConfig(level=logging.INFO)\n", "\n", "ts = ps.Model(obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "7197ceab", "metadata": {}, "outputs": [], "source": [ "# when we also want log-information saved to file, we add file-handlers\n", "ps.utils.add_file_handlers(ps.logger)\n", "ts = ps.Model(obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "59d8fb43", "metadata": {}, "outputs": [], "source": [ "# to get the default logger back we initialize it again\n", "ps.utils.initialize_logger(ps.logger)" ] } ], "metadata": { "kernelspec": { "display_name": "defaultInterpreterPath: 3.13.11.final.0", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.11" } }, "nbformat": 4, "nbformat_minor": 5 }