{ "cells": [ { "cell_type": "markdown", "id": "94fac996-f1b9-4cea-ba12-1a3619f2ae27", "metadata": { "tags": [] }, "source": [ "# Logging\n", "\n", "Scipp is generally quiet because it mostly provides low level components.\n", "It can however produce warnings and some functions (e.g. [scipp.transform_coords](../generated/functions/scipp.transform_coords.rst)) log at the info level.\n", "For this purpose, scipp defines a logger which allows customizing how those warnings and errors are reported.\n", "\n", "The logger is an instance of [logging.Logger](https://docs.python.org/3/library/logging.html#logging.Logger) with name 'scipp'.\n", "It has no handlers by default and needs to be configured using the [logging](https://docs.python.org/3/library/logging.html) module of the standard library." ] }, { "cell_type": "code", "execution_count": null, "id": "6fe5c6cf-0260-45c0-a525-48af9905429b", "metadata": {}, "outputs": [], "source": [ "import scipp as sc\n", "logger = sc.get_logger()" ] }, { "cell_type": "markdown", "id": "ed3fcbae-1a9e-492e-96fd-b46883d48483", "metadata": {}, "source": [ "You can silence Scipp's info messages if they get in your way using" ] }, { "cell_type": "code", "execution_count": null, "id": "6365de50-9a7c-42f7-9308-b73d6e10e527", "metadata": {}, "outputs": [], "source": [ "logger.setLevel('WARNING')" ] }, { "cell_type": "markdown", "id": "90f3f6ec-79dc-472c-b7f4-1528f19bca48", "metadata": {}, "source": [ "For illustration purposes, install a basic [logging.StreamHandler](https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler) and enable logging of info messages:" ] }, { "cell_type": "code", "execution_count": null, "id": "1456dece-9774-4aac-bea1-3b6baa872b99", "metadata": {}, "outputs": [], "source": [ "import logging\n", "stream_handler = logging.StreamHandler()\n", "stream_handler.setLevel(logging.INFO)\n", "logger.addHandler(stream_handler)\n", "logger.setLevel(logging.INFO)" ] }, { "cell_type": "code", "execution_count": null, "id": "e28e2e0b-0ebc-4b84-9cfd-0d0c85224d43", "metadata": {}, "outputs": [], "source": [ "logger.info('The stream handler simply prints messages')" ] }, { "cell_type": "markdown", "id": "0fcd4bdd-9bd4-45f8-8738-6c18d1338ecb", "metadata": {}, "source": [ "## Jupyter Log Widget\n", "\n", "When running in Jupyter notebooks, [scipp.logging](../generated/modules/scipp.logging.rst) provides a special handler which sends messages to a widget.\n", "It can be added like any other handler.\n", "Once Scipp's logger has a [WidgetHandler](../generated/modules/scipp.logging.WidgetHandler.rst), [scipp.display_logs](../generated/modules/scipp.logging.display_logs.rst) can be used to render its widget in a notebook." ] }, { "cell_type": "code", "execution_count": null, "id": "39f1e3ae-16dd-4d43-beb1-c02908719882", "metadata": {}, "outputs": [], "source": [ "# This only works in a Jupyter notebook.\n", "logger.addHandler(sc.logging.make_widget_handler())" ] }, { "cell_type": "code", "execution_count": null, "id": "f2cebf31-d262-4a5a-a077-398d8d6cbe29", "metadata": {}, "outputs": [], "source": [ "sc.display_logs()" ] }, { "cell_type": "markdown", "id": "a9349f14-2eee-4e84-b4bc-4e52a81d56d2", "metadata": {}, "source": [ "(Click and drag the bottom right corner to increase the size of the widget if it is too small.)" ] }, { "cell_type": "code", "execution_count": null, "id": "74fab397-3af7-4acc-b439-49240ebda806", "metadata": {}, "outputs": [], "source": [ "logger.info('This shows up in the widget and error stream.')" ] }, { "cell_type": "markdown", "id": "22ba3fe2-4473-49e5-9562-22aadb105d4d", "metadata": {}, "source": [ "[scipp.logging](../generated/modules/scipp.logging.rst) provides a number of high level convenience functions for managing log widgets and handlers.\n", "See the module documentation for more details.\n", "\n", "