.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user-guide\13-logging.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_user-guide_13-logging.py: Logging in iMOD Python ====================================== iMOD Python supports logging through both the standard Python logging framework and Loguru, so that you can choose whichever best fits your needs and project. By default, logging is silent, so messages are only output once a logger is configured. In this example, we will use loading in the hondsrug simulation to demonstrate logging capabilities. .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: Python import imod from imod.logging import LoggerType, LogLevel # Create a temporary directory tmpdir = imod.util.temporary_directory() .. GENERATED FROM PYTHON SOURCE LINES 23-30 Fetching an iMOD5 model ----------------------- You can set up the logger by calling imod.logging.configure and choosing the type of logger (PYTHON, LOGURU) you would like to use: .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: Python imod.logging.configure(LoggerType.LOGURU) .. GENERATED FROM PYTHON SOURCE LINES 35-40 Additionally, you can set the level of logging you want (DEBUG, INFO, WARNING, ERROR, CRITICAL), where the default is WARNING. Here, we will use the Loguru logger and set the level to INFO so that we can see some logging output: .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python imod.logging.configure(LoggerType.LOGURU, log_level=LogLevel.INFO) simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-08-04 14:56:38.777 | INFO | imod.data.sample_data:hondsrug_simulation:201 - Beginning execution of imod.mf6.simulation.from_file for object WindowsPath... 2026-08-04 14:56:38.779 | INFO | imod.logging.ilogger:log:94 - iMOD Python version in current environment: 1.1.0 2026-08-04 14:56:38.780 | INFO | imod.logging.ilogger:log:94 - No iMOD Python version information found in dumped simulation. 2026-08-04 14:56:38.791 | INFO | imod.mf6.model:from_file:687 - Initializing the GroundwaterFlowModel package... 2026-08-04 14:56:38.792 | INFO | imod.common.utilities.schemata:validate_with_error_message:132 - Beginning execution of imod.mf6.model._validate_options for object GroundwaterFlowModel... .. GENERATED FROM PYTHON SOURCE LINES 45-47 If we run the command again with the log level set to DEBUG, we can see more detailed logging output: .. GENERATED FROM PYTHON SOURCE LINES 47-52 .. code-block:: Python imod.logging.configure(LoggerType.LOGURU, log_level=LogLevel.DEBUG) simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") .. rst-class:: sphx-glr-script-out .. code-block:: none 2026-08-04 14:56:39.069 | INFO | imod.data.sample_data:hondsrug_simulation:201 - Beginning execution of imod.mf6.simulation.from_file for object WindowsPath... 2026-08-04 14:56:39.071 | INFO | imod.logging.ilogger:log:94 - iMOD Python version in current environment: 1.1.0 2026-08-04 14:56:39.071 | INFO | imod.logging.ilogger:log:94 - No iMOD Python version information found in dumped simulation. 2026-08-04 14:56:39.081 | INFO | imod.mf6.model:from_file:687 - Initializing the GroundwaterFlowModel package... 2026-08-04 14:56:39.081 | INFO | imod.common.utilities.schemata:validate_with_error_message:132 - Beginning execution of imod.mf6.model._validate_options for object GroundwaterFlowModel... 2026-08-04 14:56:39.081 | DEBUG | imod.common.utilities.schemata:validate_with_error_message:132 - Finished execution of imod.mf6.model._validate_options for object GroundwaterFlowModel in 0.00010919570922851562 seconds... 2026-08-04 14:56:39.082 | DEBUG | imod.mf6.model:from_file:687 - Successfully initialized the GroundwaterFlowModel in 0.00039768218994140625 seconds... 2026-08-04 14:56:39.258 | DEBUG | imod.data.sample_data:hondsrug_simulation:201 - Finished execution of imod.mf6.simulation.from_file for object WindowsPath in 0.18831133842468262 seconds... .. GENERATED FROM PYTHON SOURCE LINES 53-57 It is also possible to log the output to a default log file, `imod-python.log`, by adding `add_default_file_handler=True` to the command. Here we setup logging using the python logging framework. Notice how the output is slightly different than the Loguru output, but the information is similar. .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: Python imod.logging.configure( LoggerType.PYTHON, log_level=LogLevel.INFO, add_default_file_handler=True ) simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") .. rst-class:: sphx-glr-script-out .. code-block:: none imod: 2026-08-04 14:56:39,296 | INFO | sample_data.py:201 | 3628 >>> Beginning execution of imod.mf6.simulation.from_file for object WindowsPath... imod: 2026-08-04 14:56:39,298 | INFO | ilogger.py:94 | 3628 >>> iMOD Python version in current environment: 1.1.0 imod: 2026-08-04 14:56:39,299 | INFO | ilogger.py:94 | 3628 >>> No iMOD Python version information found in dumped simulation. imod: 2026-08-04 14:56:39,309 | INFO | model.py:687 | 3628 >>> Initializing the GroundwaterFlowModel package... imod: 2026-08-04 14:56:39,310 | INFO | schemata.py:132 | 3628 >>> Beginning execution of imod.mf6.model._validate_options for object GroundwaterFlowModel... .. GENERATED FROM PYTHON SOURCE LINES 64-69 Sometimes, it might be useful to redirect logging to a specific file, such as when processing large datasets or running automated simulations, to keep logging output organised for debugging and verification. Here, we also set `add_default_stream_handler=True`, which controls if logging output is also sent to the console. .. GENERATED FROM PYTHON SOURCE LINES 69-87 .. code-block:: Python # Context manager to handle redirection of log output from contextlib import redirect_stdout logfile_path = "open_simulation.log" with open(logfile_path, "w") as f: # Redirect stdout to the log file with redirect_stdout(f): # Configure logging imod.logging.configure( LoggerType.PYTHON, log_level=LogLevel.INFO, add_default_file_handler=False, add_default_stream_handler=True, ) # Load the simulation simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") .. rst-class:: sphx-glr-script-out .. code-block:: none imod: 2026-08-04 14:56:39,600 | INFO | sample_data.py:201 | 3628 >>> Beginning execution of imod.mf6.simulation.from_file for object WindowsPath... imod: 2026-08-04 14:56:39,602 | INFO | ilogger.py:94 | 3628 >>> iMOD Python version in current environment: 1.1.0 imod: 2026-08-04 14:56:39,603 | INFO | ilogger.py:94 | 3628 >>> No iMOD Python version information found in dumped simulation. imod: 2026-08-04 14:56:39,614 | INFO | model.py:687 | 3628 >>> Initializing the GroundwaterFlowModel package... imod: 2026-08-04 14:56:39,615 | INFO | schemata.py:132 | 3628 >>> Beginning execution of imod.mf6.model._validate_options for object GroundwaterFlowModel... .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.359 seconds) .. _sphx_glr_download_user-guide_13-logging.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 13-logging.ipynb <13-logging.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 13-logging.py <13-logging.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 13-logging.zip <13-logging.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_