Skip to content

Commit a1075de

Browse files
committed
13.11小节完成
1 parent cc5a9a4 commit a1075de

File tree

1 file changed

+70
-62
lines changed

1 file changed

+70
-62
lines changed

source/c13/p11_add_logging_to_simple_scripts.rst

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
----------
66
问题
77
----------
8-
You want scripts and simple programs to write diagnostic information to log files.
8+
你希望在脚本和程序中将诊断信息写入日志文件。
99

1010
|
1111
@@ -14,69 +14,77 @@ You want scripts and simple programs to write diagnostic information to log file
1414
----------
1515
The easiest way to add logging to simple programs is to use the logging module. For
1616
example:
17+
打印日志最简单方式是使用 ``logging`` 模块。例如:
1718

18-
import logging
19+
.. code-block:: python
1920
20-
def main():
21-
# Configure the logging system
22-
logging.basicConfig(
23-
filename='app.log',
24-
level=logging.ERROR
25-
)
26-
27-
# Variables (to make the calls that follow work)
28-
hostname = 'www.python.org'
29-
item = 'spam'
30-
filename = 'data.csv'
31-
mode = 'r'
32-
33-
# Example logging calls (insert into your program)
34-
logging.critical('Host %s unknown', hostname)
35-
logging.error("Couldn't find %r", item)
36-
logging.warning('Feature is deprecated')
37-
logging.info('Opening file %r, mode=%r', filename, mode)
38-
logging.debug('Got here')
39-
40-
if __name__ == '__main__':
41-
main()
42-
43-
The five logging calls (critical(), error(), warning(), info(), debug()) represent
44-
different severity levels in decreasing order. The level argument to basicConfig() is
45-
a filter. All messages issued at a level lower than this setting will be ignored.
46-
The argument to each logging operation is a message string followed by zero or more
47-
arguments. When making the final log message, the % operator is used to format the
48-
message string using the supplied arguments.
49-
If you run this program, the contents of the file app.log will be as follows:
21+
import logging
22+
23+
def main():
24+
# Configure the logging system
25+
logging.basicConfig(
26+
filename='app.log',
27+
level=logging.ERROR
28+
)
29+
30+
# Variables (to make the calls that follow work)
31+
hostname = 'www.python.org'
32+
item = 'spam'
33+
filename = 'data.csv'
34+
mode = 'r'
35+
36+
# Example logging calls (insert into your program)
37+
logging.critical('Host %s unknown', hostname)
38+
logging.error("Couldn't find %r", item)
39+
logging.warning('Feature is deprecated')
40+
logging.info('Opening file %r, mode=%r', filename, mode)
41+
logging.debug('Got here')
42+
43+
if __name__ == '__main__':
44+
main()
45+
46+
上面五个日志调用(critical(), error(), warning(), info(), debug())以降序方式表示不同的严重级别。
47+
``basicConfig()`` 的 ``level`` 参数是一个过滤器。
48+
所有级别低于此级别的日志消息都会被忽略掉。
49+
每个logging操作的参数是一个消息字符串,后面再跟一个或多个参数。
50+
构造最终的日志消息的时候我们使用了%操作符来格式化消息字符串。
51+
52+
运行这个程序后,在文件 ``app.log`` 中的内容应该是下面这样:
5053

5154
CRITICAL:root:Host www.python.org unknown
5255
ERROR:root:Could not find 'spam'
5356

5457
If you want to change the output or level of output, you can change the parameters to
5558
the basicConfig() call. For example:
59+
如果你想改变输出等级,你可以修改 ``basicConfig()`` 调用中的参数。例如:
60+
61+
.. code-block:: python
5662
57-
logging.basicConfig(
58-
filename='app.log',
59-
level=logging.WARNING,
60-
format='%(levelname)s:%(asctime)s:%(message)s')
63+
logging.basicConfig(
64+
filename='app.log',
65+
level=logging.WARNING,
66+
format='%(levelname)s:%(asctime)s:%(message)s')
6167
62-
As a result, the output changes to the following:
68+
最后输出变成如下:
6369

6470
CRITICAL:2012-11-20 12:27:13,595:Host www.python.org unknown
6571
ERROR:2012-11-20 12:27:13,595:Could not find 'spam'
6672
WARNING:2012-11-20 12:27:13,595:Feature is deprecated
6773

68-
As shown, the logging configuration is hardcoded directly into the program. If you want
69-
to configure it from a configuration file, change the basicConfig() call to the following:
74+
上面的日志配置都是硬编码到程序中的。如果你想使用配置文件,
75+
可以像下面这样修改 ``basicConfig()`` 调用:
7076

71-
import logging
72-
import logging.config
77+
.. code-block:: python
7378
74-
def main():
75-
# Configure the logging system
76-
logging.config.fileConfig('logconfig.ini')
77-
...
79+
import logging
80+
import logging.config
7881
79-
Now make a configuration file logconfig.ini that looks like this:
82+
def main():
83+
# Configure the logging system
84+
logging.config.fileConfig('logconfig.ini')
85+
...
86+
87+
创建一个下面这样的文件,名字叫 ``logconfig.ini`` :
8088

8189
[loggers]
8290
keys=root
@@ -100,30 +108,30 @@ Now make a configuration file logconfig.ini that looks like this:
100108
[formatter_defaultFormatter]
101109
format=%(levelname)s:%(name)s:%(message)s
102110

103-
If you want to make changes to the configuration, you can simply edit the logcon‐
104-
fig.ini file as appropriate.
111+
如果你想修改配置,可以直接编辑文件logconfig.ini即可。
105112

106113
|
107114
108115
----------
109116
讨论
110117
----------
111-
Ignoring for the moment that there are about a million advanced configuration options
112-
for the logging module, this solution is quite sufficient for simple programs and scripts.
113-
Simply make sure that you execute the basicConfig() call prior to making any logging
114-
calls, and your program will generate logging output.
115-
If you want the logging messages to route to standard error instead of a file, don’t supply
116-
any filename information to basicConfig(). For example, simply do this:
118+
尽管对于 ``logging`` 模块而已有很多更高级的配置选项,
119+
不过这里的方案对于简单的程序和脚本已经足够了。
120+
只想在调用日志操作前先执行下basicConfig()函数方法,你的程序就能产生日志输出了。
121+
122+
如果你想要你的日志消息写到标准错误中,而不是日志文件中,调用 ``basicConfig()`` 时不传文件名参数即可。例如:
123+
124+
.. code-block:: python
117125
118-
logging.basicConfig(level=logging.INFO)
126+
logging.basicConfig(level=logging.INFO)
119127
120-
One subtle aspect of basicConfig() is that it can only be called once in your program.
121-
If you later need to change the configuration of the logging module, you need to obtain
122-
the root logger and make changes to it directly. For example:
128+
``basicConfig()`` 在程序中只能被执行一次。如果你稍后想改变日志配置,
129+
就需要先获取 ``root logger`` ,然后直接修改它。例如:
123130

124-
logging.getLogger().level = logging.DEBUG
131+
.. code-block:: python
125132
126-
It must be emphasized that this recipe only shows a basic use of the logging module.
127-
There are significantly more advanced customizations that can be made. An excellent
128-
resource for such customization is the “Logging Cookbook”.
133+
logging.getLogger().level = logging.DEBUG
129134
135+
需要强调的是本节只是演示了 ``logging`` 模块的一些基本用法。
136+
它可以做更多更高级的定制。
137+
关于日志定制化一个很好的资源是 `Logging Cookbook <https://docs.python.org/3/howto/logging-cookbook.html>`_

0 commit comments

Comments
 (0)