Skip to content

Commit 5f2d05c

Browse files
committed
logging module-intro
1 parent 5ce0784 commit 5f2d05c

File tree

2 files changed

+223
-1
lines changed

2 files changed

+223
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
14) Pickle module, introduction: We use *dump* and *load* to save Python object into a byte stream and vice versa.
1717
15) Classes in Python, introdcution: Class definition and how to get instances from it are mentioned. Also, methods are reviewed. The difference between class variables versus instance variables is expressed. Examples are provided to help the explanation.
1818
16) Three different types of methods (functions) in Python are reviewed: instance method, class method, and static method. We use decorators: @classmethod and @staticmethod, for building class methods or static methods.
19-
17) Exercise: Computing the possible outcomes of a ranking event for the FIFA World Cup 2022, knowing the semi-final matches, using *itertools.permutations*.
19+
17) Exercise: Computing the possible outcomes of a ranking event for the FIFA World Cup 2022, knowing the semi-final matches, using *itertools.permutations*.
20+
18) Logging module in Python, introduction: Using functions of logging module, we can display logging messages to the console or save them to a file. Here, these functions such as debug(), info(), warning(), error(), and critical() are mentioned.

logging-module-introduction.ipynb

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "d6ea1456",
6+
"metadata": {},
7+
"source": [
8+
"# Python: Everything\n",
9+
"18) **Logging module Introduction:** Using logging functions to create logging systems<br>\n",
10+
"آشنایی سنجانه رخدادنگاری: بهره‌گیری از کارکنشهای رخدادنگاری، برای آفرینش سامانه‌های رخدادنگاری\n",
11+
"<br> **Hint:** Each cell in this Notebook should be run separately<br>\n",
12+
"نکته: هر یاخته در این نوتبوک، بهتر است که جداگانه دوانده شود\n",
13+
"<br>Instagram: @words.persian\n",
14+
"<br>https://github.com/ostad-ai/Python-Everything\n",
15+
"<br> Slides in English:\n",
16+
"https://www.pinterest.com/HamedShahHosseini/programming-languages/"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"id": "46daad57",
22+
"metadata": {},
23+
"source": [
24+
"These functions: debug(), info(), warning(), error(), and critical() are usually used for showing or recording events.<br>\n",
25+
"پنج کارکنش بالا را میتوان برای نمایش یا آگاشتِ رویدادها، به کار برد"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 1,
31+
"id": "1e79f6f2",
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"name": "stderr",
36+
"output_type": "stream",
37+
"text": [
38+
"WARNING:root:It is a mild warning\n"
39+
]
40+
}
41+
],
42+
"source": [
43+
"# simple example: نمونه ساده\n",
44+
"# as you see, the info message is not shown\n",
45+
"# because the default logging level is warning\n",
46+
"# پیغام آگاهی -اینفو- نشان داده نمیشود \n",
47+
"#زیرا، تراز رخدادنگاری وادنگ، روی هشدار -وارنینگ- است\n",
48+
"import logging\n",
49+
"logging.warning('It is a mild warning')\n",
50+
"logging.info('It is an info. message')"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 2,
56+
"id": "b966d1a7",
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"name": "stderr",
61+
"output_type": "stream",
62+
"text": [
63+
"WARNING:root:It is a mild warning\n",
64+
"INFO:root:It is an info. message\n"
65+
]
66+
}
67+
],
68+
"source": [
69+
"# simple example with changing the default level:\n",
70+
"# now the info message is also shown\n",
71+
"# because the loggeing level is set to: INFO\n",
72+
"# نمونه ساده با دگرش دادن تراز وادنگ\n",
73+
"# اکنون پیغام آگاهی -اینفو- نیز دیده میشود\n",
74+
"# زیرا، تراز رخدادنگاری را، آگاهی -اینفو- گذاشتیم\n",
75+
"import logging\n",
76+
"logging.basicConfig(level=logging.INFO,force=True)\n",
77+
"logging.warning('It is a mild warning')\n",
78+
"logging.info('It is an info. message')"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 7,
84+
"id": "5f202e7f",
85+
"metadata": {},
86+
"outputs": [
87+
{
88+
"name": "stderr",
89+
"output_type": "stream",
90+
"text": [
91+
"ERROR:__main__:It is a serious error\n"
92+
]
93+
}
94+
],
95+
"source": [
96+
"# simple example to get an instance of the Logger class:\n",
97+
"# نمونه ساده برای گرفتن یک گونه‌سار از رده رُخدادنگار لاگر\n",
98+
"import logging\n",
99+
"logger=logging.getLogger(__name__)\n",
100+
"logger.error('It is a serious error')"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 8,
106+
"id": "ffb973a0",
107+
"metadata": {},
108+
"outputs": [
109+
{
110+
"name": "stderr",
111+
"output_type": "stream",
112+
"text": [
113+
"CRITICAL:MyLog:It is a cirtical situation\n"
114+
]
115+
}
116+
],
117+
"source": [
118+
"# simple example with a user-defined name:\n",
119+
"# نمونه ساده با یک نام کاربر-شناسانده\n",
120+
"import logging\n",
121+
"logger=logging.getLogger('MyLog')\n",
122+
"logger.critical('It is a cirtical situation')"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 9,
128+
"id": "9e2a47b4",
129+
"metadata": {},
130+
"outputs": [
131+
{
132+
"name": "stderr",
133+
"output_type": "stream",
134+
"text": [
135+
"2022-12-18 15:25:01,357 - INFO - Starting....\n",
136+
"2022-12-18 15:25:01,357 - WARNING - It is an important warning\n",
137+
"2022-12-18 15:25:01,367 - INFO - Finished!\n"
138+
]
139+
}
140+
],
141+
"source": [
142+
"# using date/time in the messages\n",
143+
"# بهره‌گیری از روزمَه/زمان در پیغامها\n",
144+
"import logging\n",
145+
"FORMAT='%(asctime) s - %(levelname)s - %(message)s'\n",
146+
"logging.basicConfig(format=FORMAT,level=logging.INFO,force=True)\n",
147+
"logger=logging.getLogger('MyLog')\n",
148+
"logger.setLevel(logging.INFO)\n",
149+
"logger.info('Starting....')\n",
150+
"logger.warning('It is an important warning')\n",
151+
"logger.info('Finished!')"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 11,
157+
"id": "3f6f3b62",
158+
"metadata": {},
159+
"outputs": [
160+
{
161+
"name": "stderr",
162+
"output_type": "stream",
163+
"text": [
164+
"2022-12-18 15:51:38,054 - DEBUG - This is only for debugging\n",
165+
"2022-12-18 15:51:38,054 - INFO - So, this is only an info message\n",
166+
"2022-12-18 15:51:38,054 - WARNING - And this is only for warning\n",
167+
"2022-12-18 15:51:38,064 - ERROR - Finally, in Persian: بچه بدی شدی ها\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"# logging into a log file\n",
173+
"# رخدادنگاری تووی یک پرونده رخدادگان\n",
174+
"import logging\n",
175+
"logger=logging.getLogger('myfileLogger')\n",
176+
"fhandler=logging.FileHandler(filename='mylog.log',encoding='utf-8')\n",
177+
"FORMAT='%(asctime) s - %(levelname)s - %(message)s'\n",
178+
"fhandler.setFormatter(logging.Formatter(FORMAT))\n",
179+
"logger.addHandler(fhandler)\n",
180+
"logger.setLevel(logging.DEBUG) #choose other levels to see difference\n",
181+
"logger.debug('This is only for debugging')\n",
182+
"logger.info('So, this is only an info message')\n",
183+
"logger.warning('And this is only for warning')\n",
184+
"logger.error('Finally, in Persian: بچه بدی شدی ها')\n",
185+
"#removing the handler\n",
186+
"# ورداشتن رسیدگی‌گر\n",
187+
"logger.removeHandler(fhandler)\n",
188+
"fhandler.close()"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": null,
194+
"id": "a14e1d25",
195+
"metadata": {},
196+
"outputs": [],
197+
"source": []
198+
}
199+
],
200+
"metadata": {
201+
"kernelspec": {
202+
"display_name": "Python 3 (ipykernel)",
203+
"language": "python",
204+
"name": "python3"
205+
},
206+
"language_info": {
207+
"codemirror_mode": {
208+
"name": "ipython",
209+
"version": 3
210+
},
211+
"file_extension": ".py",
212+
"mimetype": "text/x-python",
213+
"name": "python",
214+
"nbconvert_exporter": "python",
215+
"pygments_lexer": "ipython3",
216+
"version": "3.8.10"
217+
}
218+
},
219+
"nbformat": 4,
220+
"nbformat_minor": 5
221+
}

0 commit comments

Comments
 (0)