Skip to content

Commit 0adad83

Browse files
committed
更新了11,12节
Former-commit-id: 2c8917b
1 parent bc47c9d commit 0adad83

8 files changed

+1757
-1042
lines changed

11. useful tools/11.07 logging.ipynb

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# logging 模块:记录日志"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"`logging` 模块可以用来记录日志:"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"outputs": [],
24+
"source": [
25+
"import logging"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"`logging` 的日志类型有以下几种:\n",
33+
"\n",
34+
"- `logging.critical(msg)`\n",
35+
"- `logging.error(msg)`\n",
36+
"- `logging.warning(msg)`\n",
37+
"- `logging.info(msg)`\n",
38+
"- `logging.debug(msg)`\n",
39+
"\n",
40+
"级别排序为:`CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET`\n",
41+
"\n",
42+
"默认情况下,`logging` 的日志级别为 `WARNING`,只有不低于 `WARNING` 级别的日志才会显示在命令行。"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 2,
48+
"metadata": {
49+
"collapsed": false
50+
},
51+
"outputs": [
52+
{
53+
"name": "stderr",
54+
"output_type": "stream",
55+
"text": [
56+
"CRITICAL:root:This is critical message\n",
57+
"ERROR:root:This is error message\n",
58+
"WARNING:root:This is warning message\n"
59+
]
60+
}
61+
],
62+
"source": [
63+
"logging.critical('This is critical message')\n",
64+
"logging.error('This is error message')\n",
65+
"logging.warning('This is warning message')\n",
66+
"\n",
67+
"# 不会显示\n",
68+
"logging.info('This is info message')\n",
69+
"logging.debug('This is debug message')"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"可以这样修改默认的日志级别:"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 3,
82+
"metadata": {
83+
"collapsed": false
84+
},
85+
"outputs": [
86+
{
87+
"name": "stderr",
88+
"output_type": "stream",
89+
"text": [
90+
"INFO:root:This is info message\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"logging.root.setLevel(level=logging.INFO)\n",
96+
"\n",
97+
"logging.info('This is info message')"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"可以通过 `logging.basicConfig()` 函数来改变默认的日志显示方式:"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 4,
110+
"metadata": {
111+
"collapsed": false
112+
},
113+
"outputs": [
114+
{
115+
"name": "stderr",
116+
"output_type": "stream",
117+
"text": [
118+
"CRITICAL:this program:This is critical message\n"
119+
]
120+
}
121+
],
122+
"source": [
123+
"logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')\n",
124+
"\n",
125+
"logger = logging.getLogger(\"this program\")\n",
126+
"\n",
127+
"logger.critical('This is critical message')"
128+
]
129+
}
130+
],
131+
"metadata": {
132+
"kernelspec": {
133+
"display_name": "Python 2",
134+
"language": "python",
135+
"name": "python2"
136+
},
137+
"language_info": {
138+
"codemirror_mode": {
139+
"name": "ipython",
140+
"version": 2
141+
},
142+
"file_extension": ".py",
143+
"mimetype": "text/x-python",
144+
"name": "python",
145+
"nbconvert_exporter": "python",
146+
"pygments_lexer": "ipython2",
147+
"version": "2.7.6"
148+
}
149+
},
150+
"nbformat": 4,
151+
"nbformat_minor": 0
152+
}

0 commit comments

Comments
 (0)