|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#### Python Logging\n", |
| 8 | + "Logging is a crucial aspect of any application, providing a way to track events, errors, and operational information. Python's built-in logging module offers a flexible framework for emitting log messages from Python programs. In this lesson, we will cover the basics of logging, including how to configure logging, log levels, and best practices for using logging in Python applications." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 3, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [ |
| 16 | + { |
| 17 | + "name": "stderr", |
| 18 | + "output_type": "stream", |
| 19 | + "text": [ |
| 20 | + "2024-06-19 12:14:39 - root - DEBUG - This is a debug message\n", |
| 21 | + "2024-06-19 12:14:39 - root - INFO - This is an info message\n", |
| 22 | + "2024-06-19 12:14:39 - root - WARNING - This is a warning message\n", |
| 23 | + "2024-06-19 12:14:39 - root - ERROR - This is an error message\n", |
| 24 | + "2024-06-19 12:14:39 - root - CRITICAL - This is a critical message\n" |
| 25 | + ] |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "import logging\n", |
| 30 | + "\n", |
| 31 | + "## Configure the basic logging settings\n", |
| 32 | + "logging.basicConfig(level=logging.DEBUG)\n", |
| 33 | + "\n", |
| 34 | + "## log messages with different severity levels\n", |
| 35 | + "logging.debug(\"This is a debug message\")\n", |
| 36 | + "logging.info(\"This is an info message\")\n", |
| 37 | + "logging.warning(\"This is a warning message\")\n", |
| 38 | + "logging.error(\"This is an error message\")\n", |
| 39 | + "logging.critical(\"This is a critical message\")\n", |
| 40 | + "\n" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "metadata": {}, |
| 46 | + "source": [ |
| 47 | + "#### Log Levels\n", |
| 48 | + "Python's logging module has several log levels indicating the severity of events. The default levels are:\n", |
| 49 | + "\n", |
| 50 | + "- DEBUG: Detailed information, typically of interest only when diagnosing problems.\n", |
| 51 | + "- INFO: Confirmation that things are working as expected.\n", |
| 52 | + "- WARNING: An indication that something unexpected happened or indicative of some problem in the near future (e.g., ‘disk space low’). The software is still working as expected.\n", |
| 53 | + "- ERROR: Due to a more serious problem, the software has not been able to perform some function.\n", |
| 54 | + "- CRITICAL: A very serious error, indicating that the program itself may be unable to continue running." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 1, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "\n", |
| 64 | + "## configuring logging\n", |
| 65 | + "import logging\n", |
| 66 | + "\n", |
| 67 | + "logging.basicConfig(\n", |
| 68 | + " filename='app.log',\n", |
| 69 | + " filemode='w',\n", |
| 70 | + " level=logging.DEBUG,\n", |
| 71 | + " format='%(asctime)s-%(name)s-%(levelname)s-%(message)s',\n", |
| 72 | + " datefmt='%Y-%m-%d %H:%M:%S'\n", |
| 73 | + " )\n", |
| 74 | + "\n", |
| 75 | + "## log messages with different severity levels\n", |
| 76 | + "logging.debug(\"This is a debug message\")\n", |
| 77 | + "logging.info(\"This is an info message\")\n", |
| 78 | + "logging.warning(\"This is a warning message\")\n", |
| 79 | + "logging.error(\"This is an error message\")\n", |
| 80 | + "logging.critical(\"This is a critical message\")" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 2, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [], |
| 88 | + "source": [ |
| 89 | + "logging.debug(\"This is a debug message\")\n", |
| 90 | + "logging.info(\"This is an info message\")\n", |
| 91 | + "logging.warning(\"This is a warning message\")\n", |
| 92 | + "logging.error(\"This is an error message\")\n", |
| 93 | + "logging.critical(\"This is a critical message\")" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": null, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [] |
| 116 | + } |
| 117 | + ], |
| 118 | + "metadata": { |
| 119 | + "kernelspec": { |
| 120 | + "display_name": "Python 3", |
| 121 | + "language": "python", |
| 122 | + "name": "python3" |
| 123 | + }, |
| 124 | + "language_info": { |
| 125 | + "codemirror_mode": { |
| 126 | + "name": "ipython", |
| 127 | + "version": 3 |
| 128 | + }, |
| 129 | + "file_extension": ".py", |
| 130 | + "mimetype": "text/x-python", |
| 131 | + "name": "python", |
| 132 | + "nbconvert_exporter": "python", |
| 133 | + "pygments_lexer": "ipython3", |
| 134 | + "version": "3.12.0" |
| 135 | + } |
| 136 | + }, |
| 137 | + "nbformat": 4, |
| 138 | + "nbformat_minor": 2 |
| 139 | +} |
0 commit comments