{ "cells": [ { "cell_type": "markdown", "id": "babe1833", "metadata": {}, "source": [ "# Logging control\n", "Several of the different subpackages of Ubermag use logging to provide additional information if required. Each packages uses a logger with the name equal to the package name, e.g. `oommfc` has a logger called `oommfc`.\n", "\n", "In Python logging information is not show by default if the level is lower than warning (see https://docs.python.org/3/howto/logging.html).\n", "\n", "The `ubermag` metapackage contains a function `setup_logging` to controll logging for all its subpackages, providing two functionalities:\n", "\n", "1. Control over the log level of all Ubermag packages.\n", "2. Setup of a basic logging configuration to print logs to the screen." ] }, { "cell_type": "code", "execution_count": 1, "id": "76e0e76e", "metadata": {}, "outputs": [], "source": [ "import ubermagutil" ] }, { "cell_type": "markdown", "id": "0ee69a1d", "metadata": {}, "source": [ "We can change the log level in two different ways.\n", "\n", "1. We can set a default level for all packages by passing an argument to `level` (see [here](https://docs.python.org/3/library/logging.html#logging-levels) for available log levels)." ] }, { "cell_type": "code", "execution_count": 2, "id": "15a4ee12", "metadata": {}, "outputs": [], "source": [ "ubermagutil.setup_logging(level=\"DEBUG\")" ] }, { "cell_type": "markdown", "id": "4d567713", "metadata": {}, "source": [ "The default value is `level='WARNING'`.\n", "\n", "2. We have fine-grain control over per-packages levels by passing a dictionary to `package_levels`. All subpackages not specified in this dictionary use the level passed to `level` (default `'WANRING'`)." ] }, { "cell_type": "code", "execution_count": 3, "id": "8c9e0759", "metadata": {}, "outputs": [], "source": [ "ubermagutil.setup_logging(\n", " package_levels={\"discretisedfield\": \"INFO\", \"oommfc\": \"DEBUG\"}\n", ")" ] }, { "cell_type": "markdown", "id": "c95381b9", "metadata": {}, "source": [ "To show an example for this configuration, more precisely the log level `'DEBUG'` for `oommfc` we can import `oommfc` and ask `oommfc` to find the default runner (which includes several logging messages)." ] }, { "cell_type": "code", "execution_count": 4, "id": "654d16ae", "metadata": { "tags": [ "nbval-ignore-output" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-05-19:15:45:50,194 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:510] Starting autoselect_runner: cache_runner=True, envvar=OOMMFTCL, oommf_exe=oommf, docker_exe=docker)\n", "2022-05-19:15:45:50,197 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:523] Step 1: Checking for the self.envvar=OOMMFTCL environment variable pointing to oommf.tcl.\n", "2022-05-19:15:45:50,198 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:548] Step 2: is oommf_exe=oommf in PATH? Could be from conda env or manual install.\n", "2022-05-19:15:45:50,200 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:554] Ouput from \"which oommf_exe\"=/opt/miniconda3/envs/ubermagdev/bin/oommf\n", "2022-05-19:15:45:50,201 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:573] Attempt command call\n", "2022-05-19:15:45:50,279 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:575] CompletedProcess(args=['/opt/miniconda3/envs/ubermagdev/bin/oommf', 'boxsi', '+fg', '+version', '-exitondone', '1', '-kill', 'all'], returncode=0, stdout=b'', stderr=b'<51622> OOMMF 2.0a3 info:\\nOOMMF 2.0a3\\n')\n" ] } ], "source": [ "import oommfc\n", "\n", "oommfc.runner.autoselect_runner()" ] }, { "cell_type": "markdown", "id": "5958e2f2", "metadata": {}, "source": [ "We can change the log level at every point of the notebook." ] }, { "cell_type": "code", "execution_count": 5, "id": "1b9eb6dc", "metadata": { "tags": [ "nbval-ignore-output" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Account lookup failure: martin\n" ] } ], "source": [ "ubermagutil.setup_logging(level=\"WARNING\")\n", "oommfc.runner.autoselect_runner()" ] }, { "cell_type": "code", "execution_count": 6, "id": "690a20d8", "metadata": { "tags": [ "nbval-ignore-output" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-05-19:15:45:51,156 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:510] Starting autoselect_runner: cache_runner=True, envvar=OOMMFTCL, oommf_exe=oommf, docker_exe=docker)\n", "2022-05-19:15:45:51,157 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:523] Step 1: Checking for the self.envvar=OOMMFTCL environment variable pointing to oommf.tcl.\n", "2022-05-19:15:45:51,158 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:548] Step 2: is oommf_exe=oommf in PATH? Could be from conda env or manual install.\n", "2022-05-19:15:45:51,159 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:554] Ouput from \"which oommf_exe\"=/opt/miniconda3/envs/ubermagdev/bin/oommf\n", "2022-05-19:15:45:51,159 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:573] Attempt command call\n", "2022-05-19:15:45:51,244 oommfc:DEBUG [/home/martin/PhD/repos/ubermag-devtools/repos/oommfc/oommfc/oommf/oommf.py:autoselect_runner:575] CompletedProcess(args=['/opt/miniconda3/envs/ubermagdev/bin/oommf', 'boxsi', '+fg', '+version', '-exitondone', '1', '-kill', 'all'], returncode=0, stdout=b'', stderr=b'<51659> OOMMF 2.0a3 info:\\nOOMMF 2.0a3\\n')\n", "Account lookup failure: martin\n" ] } ], "source": [ "ubermagutil.setup_logging(level=\"DEBUG\")\n", "oommfc.runner.autoselect_runner()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.12" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }