{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Logging\n", "\n", "With version `0.3.4`, a new logging module is available. `metacatalog` uses the buildin [`logging`](https://docs.python.org/3/howto/logging.html) module, by exposing a [new Handler](https://docs.python.org/3/library/logging.handlers.html). Hence, you can use the standard procedures in Python to set the Log level or add as many other handlers as necessary.\n", "\n", "The logger can be loaded through the API. On first creation, it needs the session object to the database. Any consecutive call to the logger can be done by `logging.getLogger`, like you would do it with any other logger. The `metacatalog` logger is simply named `'metacatalog'`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from metacatalog import api\n", "\n", "# get a DB session\n", "session = api.connect_database()\n", "\n", "# get the logger\n", "logger = api.get_logger(session)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From here on, you can use the logger just like any other `logging.Logger`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# with default settings, this will be ignored\n", "logger.info('Ignored message')\n", "\n", "# only warning and error will be ignored\n", "logger.warning('Serious warning raised in the documentation')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After first creation, it is also possible to load the logger from `logging`, i.e. in other files, but within the same Python session" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "import logging\n", "\n", "same_logger = logging.getLogger('metacatalog')\n", "\n", "print(logger)\n", "print(same_logger)\n", "\n", "same_logger.error('An error raised in the documentation')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, use the `models.Log` class to load the last few messages." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ERROR]: An error raised in the documentation (2021-06-07T10:06:08.658991)\n", "[WARNING]: Serious warning raised in the documentation (2021-06-07T10:02:22.334275)\n", "[MIGRATION]: Migrated database to 7 using metacatalog==0.3.3 (2021-06-07T07:42:08.686526)\n" ] } ], "source": [ "from metacatalog import models\n", "\n", "for log in models.Log.load_last(session, n=3):\n", " print(log)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the error and warning messages were logged into the database, but the info message was ignored, as the log level is set to warning by default." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INFO]: Ignored message - not ignored this time (2021-06-07T10:09:01.311257)\n", "[ERROR]: An error raised in the documentation (2021-06-07T10:06:08.658991)\n", "[WARNING]: Serious warning raised in the documentation (2021-06-07T10:02:22.334275)\n" ] } ], "source": [ "logger.setLevel(10) # set to debug\n", "logger.info('Ignored message - not ignored this time')\n", "\n", "\n", "for log in models.Log.load_last(session, n=3):\n", " print(log)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. autofunction:: metacatalog.api.get_logger" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }