-
Notifications
You must be signed in to change notification settings - Fork 10
Issue 1820 create user guide example for logging #1889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ba45c9
Create basic logging configuration example
ClaireDons 6a4c0f3
Add debug level example and writing output to a custom file
ClaireDons 39e6ab1
Merge branch 'master' into issue-1820-user-guide-logging
ClaireDons 60a8704
Add some more context to redirecting to file example
ClaireDons deb9e59
Merge branch 'master' into issue-1820-user-guide-logging
ClaireDons fd5f609
Spelling
ClaireDons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| 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") | ||
| # %% | ||
| # 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") | ||
|
|
||
| # %% | ||
| # 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") | ||
|
|
||
| # %% | ||
| # 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") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.