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.

import imod
from imod.logging import LoggerType, LogLevel

# Create a temporary directory
tmpdir = imod.util.temporary_directory()

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:

imod.logging.configure(LoggerType.LOGURU)

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:

imod.logging.configure(LoggerType.LOGURU, log_level=LogLevel.INFO)

simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved")
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...

If we run the command again with the log level set to DEBUG, we can see more detailed logging output:

imod.logging.configure(LoggerType.LOGURU, log_level=LogLevel.DEBUG)

simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved")
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...

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.

imod.logging.configure(
    LoggerType.PYTHON, log_level=LogLevel.INFO, add_default_file_handler=True
)
simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved")
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...

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.

# 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")
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...

Total running time of the script: (0 minutes 1.359 seconds)

Gallery generated by Sphinx-Gallery