Skip to content

Commit 5fc53df

Browse files
authored
Add files via upload
1 parent c6a7db6 commit 5fc53df

File tree

9 files changed

+376
-0
lines changed

9 files changed

+376
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### Logging with Multiple Loggers\n",
8+
"You can create multiple loggers for different parts of your application."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 2,
14+
"metadata": {},
15+
"outputs": [],
16+
"source": [
17+
"import logging\n",
18+
"## create a logger for module1\n",
19+
"logger1=logging.getLogger(\"module1\")\n",
20+
"logger1.setLevel(logging.DEBUG)\n",
21+
"\n",
22+
"##create a logger for module 2\n",
23+
"\n",
24+
"logger2=logging.getLogger(\"module2\")\n",
25+
"logger2.setLevel(logging.WARNING)\n",
26+
"\n",
27+
"# Configure logging settings\n",
28+
"logging.basicConfig(\n",
29+
" level=logging.DEBUG,\n",
30+
" format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',\n",
31+
" datefmt='%Y-%m-%d %H:%M:%S'\n",
32+
")"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 3,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stderr",
42+
"output_type": "stream",
43+
"text": [
44+
"2024-06-19 13:21:55 - module1 - DEBUG - This is debug message for module1\n",
45+
"2024-06-19 13:21:55 - module2 - WARNING - This is a warning message for module 2\n",
46+
"2024-06-19 13:21:55 - module2 - ERROR - This is an error message\n"
47+
]
48+
}
49+
],
50+
"source": [
51+
"## log message with different loggers\n",
52+
"logger1.debug(\"This is debug message for module1\")\n",
53+
"logger2.warning(\"This is a warning message for module 2\")\n",
54+
"logger2.error(\"This is an error message\")"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": []
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": []
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": []
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": []
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": []
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": null,
95+
"metadata": {},
96+
"outputs": [],
97+
"source": []
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"metadata": {},
103+
"outputs": [],
104+
"source": []
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {},
110+
"outputs": [],
111+
"source": []
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"metadata": {},
117+
"outputs": [],
118+
"source": []
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": []
126+
}
127+
],
128+
"metadata": {
129+
"kernelspec": {
130+
"display_name": "Python 3",
131+
"language": "python",
132+
"name": "python3"
133+
},
134+
"language_info": {
135+
"codemirror_mode": {
136+
"name": "ipython",
137+
"version": 3
138+
},
139+
"file_extension": ".py",
140+
"mimetype": "text/x-python",
141+
"name": "python",
142+
"nbconvert_exporter": "python",
143+
"pygments_lexer": "ipython3",
144+
"version": "3.12.0"
145+
}
146+
},
147+
"nbformat": 4,
148+
"nbformat_minor": 2
149+
}

12-Logging In Python/app.log

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2024-06-19 12:25:46-root-DEBUG-This is a debug message
2+
2024-06-19 12:25:46-root-INFO-This is an info message
3+
2024-06-19 12:25:46-root-WARNING-This is a warning message
4+
2024-06-19 12:25:46-root-ERROR-This is an error message
5+
2024-06-19 12:25:46-root-CRITICAL-This is a critical message
6+
2024-06-19 12:26:04-root-DEBUG-This is a debug message
7+
2024-06-19 12:26:04-root-INFO-This is an info message
8+
2024-06-19 12:26:04-root-WARNING-This is a warning message
9+
2024-06-19 12:26:04-root-ERROR-This is an error message
10+
2024-06-19 12:26:04-root-CRITICAL-This is a critical message

12-Logging In Python/app.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
3+
## logging setting
4+
5+
logging.basicConfig(
6+
level=logging.DEBUG,
7+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
8+
datefmt='%Y-%m-%d %H:%M:%S',
9+
handlers=[
10+
logging.FileHandler("app1.log"),
11+
logging.StreamHandler()
12+
]
13+
)
14+
15+
logger=logging.getLogger("ArithmethicApp")
16+
17+
def add(a,b):
18+
result=a+b
19+
logger.debug(f"Adding {a} + {b}= {result}")
20+
return result
21+
22+
def subtract(a, b):
23+
result = a - b
24+
logger.debug(f"Subtracting {a} - {b} = {result}")
25+
return result
26+
27+
def multiply(a, b):
28+
result = a * b
29+
logger.debug(f"Multiplying {a} * {b} = {result}")
30+
return result
31+
32+
def divide(a, b):
33+
try:
34+
result = a / b
35+
logger.debug(f"Dividing {a} / {b} = {result}")
36+
return result
37+
except ZeroDivisionError:
38+
logger.error("Division by zero error")
39+
return None
40+
41+
add(10,15)
42+
subtract(15,10)
43+
multiply(10,20)
44+
divide(20,0)

12-Logging In Python/app1.log

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2024-06-19 13:34:00 - ArithmethicApp - DEBUG - Subtracting 15 - 10 = 5
2+
2024-06-19 13:34:00 - ArithmethicApp - DEBUG - Multiplying 10 * 20 = 200
3+
2024-06-19 13:34:00 - ArithmethicApp - DEBUG - Dividing 20 / 10 = 2.0
4+
2024-06-19 13:34:31 - ArithmethicApp - DEBUG - Subtracting 15 - 10 = 5
5+
2024-06-19 13:34:31 - ArithmethicApp - DEBUG - Multiplying 10 * 20 = 200
6+
2024-06-19 13:34:31 - ArithmethicApp - DEBUG - Dividing 20 / 10 = 2.0
7+
2024-06-19 13:35:26 - ArithmethicApp - DEBUG - Adding 10 + 15= 25
8+
2024-06-19 13:35:26 - ArithmethicApp - DEBUG - Subtracting 15 - 10 = 5
9+
2024-06-19 13:35:26 - ArithmethicApp - DEBUG - Multiplying 10 * 20 = 200
10+
2024-06-19 13:35:26 - ArithmethicApp - DEBUG - Dividing 20 / 10 = 2.0
11+
2024-06-19 13:36:12 - ArithmethicApp - DEBUG - Adding 10 + 15= 25
12+
2024-06-19 13:36:12 - ArithmethicApp - DEBUG - Subtracting 15 - 10 = 5
13+
2024-06-19 13:36:12 - ArithmethicApp - DEBUG - Multiplying 10 * 20 = 200
14+
2024-06-19 13:36:12 - ArithmethicApp - ERROR - Division by zero error
422 Bytes
Binary file not shown.

12-Logging In Python/logs/app.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2024-06-19 12:29:57-root-DEBUG-The addition function is called
2+
2024-06-19 12:29:57-root-DEBUG-The addition operation is taking place
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## configuring logging
2+
import logging
3+
4+
logging.basicConfig(
5+
filename='app.log',
6+
filemode='w',
7+
level=logging.DEBUG,
8+
format='%(asctime)s-%(name)s-%(levelname)s-%(message)s',
9+
datefmt='%Y-%m-%d %H:%M:%S'
10+
)

12-Logging In Python/logs/test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from logger import logging
2+
3+
def add(a,b):
4+
logging.debug("The addition operation is taking place")
5+
return a+b
6+
7+
logging.debug("The addition function is called")
8+
add(10,15)

0 commit comments

Comments
 (0)