5
5
----------
6
6
问题
7
7
----------
8
- You want scripts and simple programs to write diagnostic information to log files.
8
+ 你希望在脚本和程序中将诊断信息写入日志文件。
9
9
10
10
|
11
11
@@ -14,69 +14,77 @@ You want scripts and simple programs to write diagnostic information to log file
14
14
----------
15
15
The easiest way to add logging to simple programs is to use the logging module. For
16
16
example:
17
+ 打印日志最简单方式是使用 ``logging `` 模块。例如:
17
18
18
- import logging
19
+ .. code-block :: python
19
20
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 `` 中的内容应该是下面这样:
50
53
51
54
CRITICAL:root: Host www.python.org unknown
52
55
ERROR:root: Could not find 'spam'
53
56
54
57
If you want to change the output or level of output, you can change the parameters to
55
58
the basicConfig() call. For example:
59
+ 如果你想改变输出等级,你可以修改 ``basicConfig() `` 调用中的参数。例如:
60
+
61
+ .. code-block :: python
56
62
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 ' )
61
67
62
- As a result, the output changes to the following:
68
+ 最后输出变成如下:
63
69
64
70
CRITICAL:2012-11-20 12:27:13,595:Host www.python.org unknown
65
71
ERROR:2012-11-20 12:27:13,595:Could not find 'spam'
66
72
WARNING:2012-11-20 12:27:13,595:Feature is deprecated
67
73
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() `` 调用:
70
76
71
- import logging
72
- import logging.config
77
+ .. code-block :: python
73
78
74
- def main():
75
- # Configure the logging system
76
- logging.config.fileConfig('logconfig.ini')
77
- ...
79
+ import logging
80
+ import logging.config
78
81
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 `` :
80
88
81
89
[loggers]
82
90
keys=root
@@ -100,30 +108,30 @@ Now make a configuration file logconfig.ini that looks like this:
100
108
[formatter_defaultFormatter]
101
109
format=%(levelname)s:%(name)s:%(message)s
102
110
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即可。
105
112
106
113
|
107
114
108
115
----------
109
116
讨论
110
117
----------
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
117
125
118
- logging.basicConfig(level=logging.INFO)
126
+ logging.basicConfig(level = logging.INFO )
119
127
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 `` ,然后直接修改它。例如:
123
130
124
- logging.getLogger().level = logging.DEBUG
131
+ .. code-block :: python
125
132
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
129
134
135
+ 需要强调的是本节只是演示了 ``logging `` 模块的一些基本用法。
136
+ 它可以做更多更高级的定制。
137
+ 关于日志定制化一个很好的资源是 `Logging Cookbook <https://docs.python.org/3/howto/logging-cookbook.html >`_
0 commit comments