From 5ba45c9ba3314176f3518adcb901ccab1222145f Mon Sep 17 00:00:00 2001 From: Claire Donnelly Date: Tue, 28 Jul 2026 00:19:52 +0200 Subject: [PATCH 1/4] Create basic logging configuration example --- examples/user-guide/13-logging.py | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/user-guide/13-logging.py diff --git a/examples/user-guide/13-logging.py b/examples/user-guide/13-logging.py new file mode 100644 index 000000000..f16fb0f23 --- /dev/null +++ b/examples/user-guide/13-logging.py @@ -0,0 +1,57 @@ +""" +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) + +original_simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") +# %% + +# %% +# We can then run this again but this time using the Python +# logging framework, as well as logging output to a file +# called `imod-python.log`, by adding +# `add_default_file_handler=True` to the command. + +# Setup imod python logging using the python logging framework and write the log output to a file + +imod.logging.configure( + LoggerType.PYTHON, log_level=LogLevel.INFO, add_default_file_handler=True +) +original_simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") From 6a4c0f3140749b640e36888f8905d09d469663c2 Mon Sep 17 00:00:00 2001 From: Claire Donnelly Date: Tue, 28 Jul 2026 11:43:05 +0200 Subject: [PATCH 2/4] Add debug level example and writing output to a custom file --- examples/user-guide/13-logging.py | 41 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/examples/user-guide/13-logging.py b/examples/user-guide/13-logging.py index f16fb0f23..77e6fa036 100644 --- a/examples/user-guide/13-logging.py +++ b/examples/user-guide/13-logging.py @@ -40,18 +40,43 @@ imod.logging.configure(LoggerType.LOGURU, log_level=LogLevel.INFO) -original_simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") +simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") # %% +# If we run the command again with the log level set to DEBUG, +# we can see more detailed logging output: -# %% -# We can then run this again but this time using the Python -# logging framework, as well as logging output to a file -# called `imod-python.log`, by adding -# `add_default_file_handler=True` to the command. +imod.logging.configure(LoggerType.LOGURU, log_level=LogLevel.DEBUG) + +simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") -# Setup imod python logging using the python logging framework and write the log output to a file +# %% +# 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 ) -original_simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") +simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") + +# %% +# 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=False` to avoid logging to the console. +# By default, this is usually True, meaning logging is also sent to the console. + +from contextlib import redirect_stdout + +logfile_path = "open_simulation.log" + +with open(logfile_path, "w") as f: + with redirect_stdout(f): + imod.logging.configure( + LoggerType.PYTHON, + log_level=LogLevel.INFO, + add_default_file_handler=False, + add_default_stream_handler=False, + ) + simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") From 60a87047f56012f9c76d88689c0f43b5a52c1d76 Mon Sep 17 00:00:00 2001 From: Claire Donnelly Date: Wed, 29 Jul 2026 10:12:31 +0200 Subject: [PATCH 3/4] Add some more context to redirecting to file example --- examples/user-guide/13-logging.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/user-guide/13-logging.py b/examples/user-guide/13-logging.py index 77e6fa036..0e481e378 100644 --- a/examples/user-guide/13-logging.py +++ b/examples/user-guide/13-logging.py @@ -2,7 +2,7 @@ Logging in iMOD-Python ====================================== -iMod-python supports logging through both the standard +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 @@ -64,19 +64,23 @@ # 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=False` to avoid logging to the console. -# By default, this is usually True, meaning logging is also sent to the console. +# 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=False, + add_default_stream_handler=True, ) + # Load the simulation simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") From fd5f609d2a2227da82af982514e70b53e702f1f2 Mon Sep 17 00:00:00 2001 From: Claire Donnelly Date: Wed, 29 Jul 2026 10:32:32 +0200 Subject: [PATCH 4/4] Spelling --- examples/user-guide/13-logging.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/user-guide/13-logging.py b/examples/user-guide/13-logging.py index 0e481e378..638af12d5 100644 --- a/examples/user-guide/13-logging.py +++ b/examples/user-guide/13-logging.py @@ -1,8 +1,8 @@ """ -Logging in iMOD-Python +Logging in iMOD Python ====================================== -iMOD-Python supports logging through both the standard +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