-
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathlogging-cookbook.po
More file actions
7871 lines (7436 loc) · 326 KB
/
logging-cookbook.po
File metadata and controls
7871 lines (7436 loc) · 326 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001 Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# python-doc bot, 2025
# 99 <wh2099@pm.me>, 2026
# Freesand Leo <yuqinju@163.com>, 2026
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.14\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-05-07 16:02+0000\n"
"PO-Revision-Date: 2025-09-16 00:00+0000\n"
"Last-Translator: Freesand Leo <yuqinju@163.com>, 2026\n"
"Language-Team: Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: zh_CN\n"
"Plural-Forms: nplurals=1; plural=0;\n"
#: ../../howto/logging-cookbook.rst:5
msgid "Logging Cookbook"
msgstr "日志专题手册"
#: ../../howto/logging-cookbook.rst:0
msgid "Author"
msgstr "作者"
#: ../../howto/logging-cookbook.rst:7
msgid "Vinay Sajip <vinay_sajip at red-dove dot com>"
msgstr "Vinay Sajip <vinay_sajip at red-dove dot com>"
#: ../../howto/logging-cookbook.rst:9
msgid ""
"This page contains a number of recipes related to logging, which have been "
"found useful in the past. For links to tutorial and reference information, "
"please see :ref:`cookbook-ref-links`."
msgstr "本页面包含多个与日志相关的专题,历史证明它们是很有用的。教程和参考信息的链接另见 :ref:`cookbook-ref-links`。"
#: ../../howto/logging-cookbook.rst:16
msgid "Using logging in multiple modules"
msgstr "在多模块中使用日志"
#: ../../howto/logging-cookbook.rst:18
msgid ""
"Multiple calls to ``logging.getLogger('someLogger')`` return a reference to "
"the same logger object. This is true not only within the same module, but "
"also across modules as long as it is in the same Python interpreter process."
" It is true for references to the same object; additionally, application "
"code can define and configure a parent logger in one module and create (but "
"not configure) a child logger in a separate module, and all logger calls to "
"the child will pass up to the parent. Here is a main module::"
msgstr ""
"无论对 ``logging.getLogger('someLogger')`` 进行多少次调用,都会返回同一个 logger "
"对象的引用。不仅在同一个模块内如此,只要是在同一个 Python "
"解释器进程中,跨模块调用也是一样。同样是引用同一个对象,应用程序也可以在一个模块中定义和配置一个父 "
"logger,而在另一个单独的模块中创建(但不配置)子 logger,对于子 logger 的所有调用都会传给父 logger。以下是主模块::"
#: ../../howto/logging-cookbook.rst:26
msgid ""
"import logging\n"
"import auxiliary_module\n"
"\n"
"# create logger with 'spam_application'\n"
"logger = logging.getLogger('spam_application')\n"
"logger.setLevel(logging.DEBUG)\n"
"# create file handler which logs even debug messages\n"
"fh = logging.FileHandler('spam.log')\n"
"fh.setLevel(logging.DEBUG)\n"
"# create console handler with a higher log level\n"
"ch = logging.StreamHandler()\n"
"ch.setLevel(logging.ERROR)\n"
"# create formatter and add it to the handlers\n"
"formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n"
"fh.setFormatter(formatter)\n"
"ch.setFormatter(formatter)\n"
"# add the handlers to the logger\n"
"logger.addHandler(fh)\n"
"logger.addHandler(ch)\n"
"\n"
"logger.info('creating an instance of auxiliary_module.Auxiliary')\n"
"a = auxiliary_module.Auxiliary()\n"
"logger.info('created an instance of auxiliary_module.Auxiliary')\n"
"logger.info('calling auxiliary_module.Auxiliary.do_something')\n"
"a.do_something()\n"
"logger.info('finished auxiliary_module.Auxiliary.do_something')\n"
"logger.info('calling auxiliary_module.some_function()')\n"
"auxiliary_module.some_function()\n"
"logger.info('done with auxiliary_module.some_function()')"
msgstr ""
"import logging\n"
"import auxiliary_module\n"
"\n"
"# 创建 'spam_application' 日志记录器\n"
"logger = logging.getLogger('spam_application')\n"
"logger.setLevel(logging.DEBUG)\n"
"# 创建可记录调试消息的文件处理器\n"
"fh = logging.FileHandler('spam.log')\n"
"fh.setLevel(logging.DEBUG)\n"
"# 创建具有更高日志层级的控制台处理器\n"
"ch = logging.StreamHandler()\n"
"ch.setLevel(logging.ERROR)\n"
"# 创建格式化器并将其添加到处理器\n"
"formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n"
"fh.setFormatter(formatter)\n"
"ch.setFormatter(formatter)\n"
"# 将处理器添加到日志记录器\n"
"logger.addHandler(fh)\n"
"logger.addHandler(ch)\n"
"\n"
"logger.info('creating an instance of auxiliary_module.Auxiliary')\n"
"a = auxiliary_module.Auxiliary()\n"
"logger.info('created an instance of auxiliary_module.Auxiliary')\n"
"logger.info('calling auxiliary_module.Auxiliary.do_something')\n"
"a.do_something()\n"
"logger.info('finished auxiliary_module.Auxiliary.do_something')\n"
"logger.info('calling auxiliary_module.some_function()')\n"
"auxiliary_module.some_function()\n"
"logger.info('done with auxiliary_module.some_function()')"
#: ../../howto/logging-cookbook.rst:56
msgid "Here is the auxiliary module::"
msgstr "以下是辅助模块::"
#: ../../howto/logging-cookbook.rst:58
msgid ""
"import logging\n"
"\n"
"# create logger\n"
"module_logger = logging.getLogger('spam_application.auxiliary')\n"
"\n"
"class Auxiliary:\n"
" def __init__(self):\n"
" self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')\n"
" self.logger.info('creating an instance of Auxiliary')\n"
"\n"
" def do_something(self):\n"
" self.logger.info('doing something')\n"
" a = 1 + 1\n"
" self.logger.info('done doing something')\n"
"\n"
"def some_function():\n"
" module_logger.info('received a call to \"some_function\"')"
msgstr ""
"import logging\n"
"\n"
"# 创建日志记录器\n"
"module_logger = logging.getLogger('spam_application.auxiliary')\n"
"\n"
"class Auxiliary:\n"
" def __init__(self):\n"
" self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')\n"
" self.logger.info('creating an instance of Auxiliary')\n"
"\n"
" def do_something(self):\n"
" self.logger.info('doing something')\n"
" a = 1 + 1\n"
" self.logger.info('done doing something')\n"
"\n"
"def some_function():\n"
" module_logger.info('received a call to \"some_function\"')"
#: ../../howto/logging-cookbook.rst:76
msgid "The output looks like this:"
msgstr "输出结果会像这样:"
#: ../../howto/logging-cookbook.rst:78
msgid ""
"2005-03-23 23:47:11,663 - spam_application - INFO -\n"
" creating an instance of auxiliary_module.Auxiliary\n"
"2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -\n"
" creating an instance of Auxiliary\n"
"2005-03-23 23:47:11,665 - spam_application - INFO -\n"
" created an instance of auxiliary_module.Auxiliary\n"
"2005-03-23 23:47:11,668 - spam_application - INFO -\n"
" calling auxiliary_module.Auxiliary.do_something\n"
"2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -\n"
" doing something\n"
"2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -\n"
" done doing something\n"
"2005-03-23 23:47:11,670 - spam_application - INFO -\n"
" finished auxiliary_module.Auxiliary.do_something\n"
"2005-03-23 23:47:11,671 - spam_application - INFO -\n"
" calling auxiliary_module.some_function()\n"
"2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -\n"
" received a call to 'some_function'\n"
"2005-03-23 23:47:11,673 - spam_application - INFO -\n"
" done with auxiliary_module.some_function()"
msgstr ""
"2005-03-23 23:47:11,663 - spam_application - INFO -\n"
" creating an instance of auxiliary_module.Auxiliary\n"
"2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -\n"
" creating an instance of Auxiliary\n"
"2005-03-23 23:47:11,665 - spam_application - INFO -\n"
" created an instance of auxiliary_module.Auxiliary\n"
"2005-03-23 23:47:11,668 - spam_application - INFO -\n"
" calling auxiliary_module.Auxiliary.do_something\n"
"2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -\n"
" doing something\n"
"2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -\n"
" done doing something\n"
"2005-03-23 23:47:11,670 - spam_application - INFO -\n"
" finished auxiliary_module.Auxiliary.do_something\n"
"2005-03-23 23:47:11,671 - spam_application - INFO -\n"
" calling auxiliary_module.some_function()\n"
"2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -\n"
" received a call to 'some_function'\n"
"2005-03-23 23:47:11,673 - spam_application - INFO -\n"
" done with auxiliary_module.some_function()"
#: ../../howto/logging-cookbook.rst:102
msgid "Logging from multiple threads"
msgstr "在多个线程中记录日志"
#: ../../howto/logging-cookbook.rst:104
msgid ""
"Logging from multiple threads requires no special effort. The following "
"example shows logging from the main (initial) thread and another thread::"
msgstr "多线程记录日志并不需要特殊处理,以下示例演示了在主线程(起始线程)和其他线程中记录日志的过程::"
#: ../../howto/logging-cookbook.rst:107
msgid ""
"import logging\n"
"import threading\n"
"import time\n"
"\n"
"def worker(arg):\n"
" while not arg['stop']:\n"
" logging.debug('Hi from myfunc')\n"
" time.sleep(0.5)\n"
"\n"
"def main():\n"
" logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')\n"
" info = {'stop': False}\n"
" thread = threading.Thread(target=worker, args=(info,))\n"
" thread.start()\n"
" while True:\n"
" try:\n"
" logging.debug('Hello from main')\n"
" time.sleep(0.75)\n"
" except KeyboardInterrupt:\n"
" info['stop'] = True\n"
" break\n"
" thread.join()\n"
"\n"
"if __name__ == '__main__':\n"
" main()"
msgstr ""
"import logging\n"
"import threading\n"
"import time\n"
"\n"
"def worker(arg):\n"
" while not arg['stop']:\n"
" logging.debug('Hi from myfunc')\n"
" time.sleep(0.5)\n"
"\n"
"def main():\n"
" logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')\n"
" info = {'stop': False}\n"
" thread = threading.Thread(target=worker, args=(info,))\n"
" thread.start()\n"
" while True:\n"
" try:\n"
" logging.debug('Hello from main')\n"
" time.sleep(0.75)\n"
" except KeyboardInterrupt:\n"
" info['stop'] = True\n"
" break\n"
" thread.join()\n"
"\n"
"if __name__ == '__main__':\n"
" main()"
#: ../../howto/logging-cookbook.rst:133
msgid "When run, the script should print something like the following:"
msgstr "脚本会运行输出类似下面的内容:"
#: ../../howto/logging-cookbook.rst:135
msgid ""
" 0 Thread-1 Hi from myfunc\n"
" 3 MainThread Hello from main\n"
" 505 Thread-1 Hi from myfunc\n"
" 755 MainThread Hello from main\n"
"1007 Thread-1 Hi from myfunc\n"
"1507 MainThread Hello from main\n"
"1508 Thread-1 Hi from myfunc\n"
"2010 Thread-1 Hi from myfunc\n"
"2258 MainThread Hello from main\n"
"2512 Thread-1 Hi from myfunc\n"
"3009 MainThread Hello from main\n"
"3013 Thread-1 Hi from myfunc\n"
"3515 Thread-1 Hi from myfunc\n"
"3761 MainThread Hello from main\n"
"4017 Thread-1 Hi from myfunc\n"
"4513 MainThread Hello from main\n"
"4518 Thread-1 Hi from myfunc"
msgstr ""
" 0 Thread-1 Hi from myfunc\n"
" 3 MainThread Hello from main\n"
" 505 Thread-1 Hi from myfunc\n"
" 755 MainThread Hello from main\n"
"1007 Thread-1 Hi from myfunc\n"
"1507 MainThread Hello from main\n"
"1508 Thread-1 Hi from myfunc\n"
"2010 Thread-1 Hi from myfunc\n"
"2258 MainThread Hello from main\n"
"2512 Thread-1 Hi from myfunc\n"
"3009 MainThread Hello from main\n"
"3013 Thread-1 Hi from myfunc\n"
"3515 Thread-1 Hi from myfunc\n"
"3761 MainThread Hello from main\n"
"4017 Thread-1 Hi from myfunc\n"
"4513 MainThread Hello from main\n"
"4518 Thread-1 Hi from myfunc"
#: ../../howto/logging-cookbook.rst:155
msgid ""
"This shows the logging output interspersed as one might expect. This "
"approach works for more threads than shown here, of course."
msgstr "以上如期显示了不同线程的日志是交替输出的。当然更多的线程也会如此。"
#: ../../howto/logging-cookbook.rst:159
msgid "Multiple handlers and formatters"
msgstr "多个 handler 和多种 formatter"
#: ../../howto/logging-cookbook.rst:161
msgid ""
"Loggers are plain Python objects. The :meth:`~Logger.addHandler` method has"
" no minimum or maximum quota for the number of handlers you may add. "
"Sometimes it will be beneficial for an application to log all messages of "
"all severities to a text file while simultaneously logging errors or above "
"to the console. To set this up, simply configure the appropriate handlers."
" The logging calls in the application code will remain unchanged. Here is "
"a slight modification to the previous simple module-based configuration "
"example::"
msgstr ""
"日志是个普通的 Python 对象。 :meth:`~Logger.addHandler` 方法可加入不限数量的日志 "
"handler。有时候,应用程序需把严重错误信息记入文本文件,而将一般错误或其他级别的信息输出到控制台。若要进行这样的设定,只需多配置几个日志 "
"handler 即可,应用程序的日志调用代码可以保持不变。下面对之前的分模块日志示例略做修改::"
#: ../../howto/logging-cookbook.rst:169
msgid ""
"import logging\n"
"\n"
"logger = logging.getLogger('simple_example')\n"
"logger.setLevel(logging.DEBUG)\n"
"# create file handler which logs even debug messages\n"
"fh = logging.FileHandler('spam.log')\n"
"fh.setLevel(logging.DEBUG)\n"
"# create console handler with a higher log level\n"
"ch = logging.StreamHandler()\n"
"ch.setLevel(logging.ERROR)\n"
"# create formatter and add it to the handlers\n"
"formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n"
"ch.setFormatter(formatter)\n"
"fh.setFormatter(formatter)\n"
"# add the handlers to logger\n"
"logger.addHandler(ch)\n"
"logger.addHandler(fh)\n"
"\n"
"# 'application' code\n"
"logger.debug('debug message')\n"
"logger.info('info message')\n"
"logger.warning('warn message')\n"
"logger.error('error message')\n"
"logger.critical('critical message')"
msgstr ""
"import logging\n"
"\n"
"logger = logging.getLogger('simple_example')\n"
"logger.setLevel(logging.DEBUG)\n"
"# 创建可记录调试消息的文件处理器\n"
"fh = logging.FileHandler('spam.log')\n"
"fh.setLevel(logging.DEBUG)\n"
"# 创建具有更高日志层级的控制台处理器\n"
"ch = logging.StreamHandler()\n"
"ch.setLevel(logging.ERROR)\n"
"# 创建格式化器并将其添加到处理器\n"
"formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n"
"ch.setFormatter(formatter)\n"
"fh.setFormatter(formatter)\n"
"# 将处理器添加到日志记录器\n"
"logger.addHandler(ch)\n"
"logger.addHandler(fh)\n"
"\n"
"# '应用程序' 代码\n"
"logger.debug('debug message')\n"
"logger.info('info message')\n"
"logger.warning('warn message')\n"
"logger.error('error message')\n"
"logger.critical('critical message')"
#: ../../howto/logging-cookbook.rst:194
msgid ""
"Notice that the 'application' code does not care about multiple handlers. "
"All that changed was the addition and configuration of a new handler named "
"*fh*."
msgstr ""
"需要注意的是,“应用程序”内的代码并不关心是否存在多个日志 handler。示例中所做的改变,只是新加入并配置了一个名为 *fh* 的 handler。"
#: ../../howto/logging-cookbook.rst:197
msgid ""
"The ability to create new handlers with higher- or lower-severity filters "
"can be very helpful when writing and testing an application. Instead of "
"using many ``print`` statements for debugging, use ``logger.debug``: Unlike "
"the print statements, which you will have to delete or comment out later, "
"the logger.debug statements can remain intact in the source code and remain "
"dormant until you need them again. At that time, the only change that needs"
" to happen is to modify the severity level of the logger and/or handler to "
"debug."
msgstr ""
"在编写和测试应用程序时,若能创建日志 handler 对不同严重级别的日志信息进行过滤,这将十分有用。 调试时无需用多条 ``print`` "
"语句,而是采用 ``logger.debug``: print 语句以后还得注释或删掉,而 logger.debug "
"语句可以原样留在源代码中保持静默。 当需要再次调试时,只要改变日志对象或 handler 的严重级别即可。"
#: ../../howto/logging-cookbook.rst:208
msgid "Logging to multiple destinations"
msgstr "在多个地方记录日志"
#: ../../howto/logging-cookbook.rst:210
msgid ""
"Let's say you want to log to console and file with different message formats"
" and in differing circumstances. Say you want to log messages with levels of"
" DEBUG and higher to file, and those messages at level INFO and higher to "
"the console. Let's also assume that the file should contain timestamps, but "
"the console messages should not. Here's how you can achieve this::"
msgstr ""
"假定要根据不同的情况将日志以不同的格式写入控制台和文件。比如把 DEBUG 以上级别的日志信息写于文件,并且把 INFO "
"以上的日志信息输出到控制台。再假设日志文件需要包含时间戳,控制台信息则不需要。以下演示了做法::"
#: ../../howto/logging-cookbook.rst:216
msgid ""
"import logging\n"
"\n"
"# set up logging to file - see previous section for more details\n"
"logging.basicConfig(level=logging.DEBUG,\n"
" format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',\n"
" datefmt='%m-%d %H:%M',\n"
" filename='/tmp/myapp.log',\n"
" filemode='w')\n"
"# define a Handler which writes INFO messages or higher to the sys.stderr\n"
"console = logging.StreamHandler()\n"
"console.setLevel(logging.INFO)\n"
"# set a format which is simpler for console use\n"
"formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')\n"
"# tell the handler to use this format\n"
"console.setFormatter(formatter)\n"
"# add the handler to the root logger\n"
"logging.getLogger().addHandler(console)\n"
"\n"
"# Now, we can log to the root logger, or any other logger. First the root...\n"
"logging.info('Jackdaws love my big sphinx of quartz.')\n"
"\n"
"# Now, define a couple of other loggers which might represent areas in your\n"
"# application:\n"
"\n"
"logger1 = logging.getLogger('myapp.area1')\n"
"logger2 = logging.getLogger('myapp.area2')\n"
"\n"
"logger1.debug('Quick zephyrs blow, vexing daft Jim.')\n"
"logger1.info('How quickly daft jumping zebras vex.')\n"
"logger2.warning('Jail zesty vixen who grabbed pay from quack.')\n"
"logger2.error('The five boxing wizards jump quickly.')"
msgstr ""
"import logging\n"
"\n"
"# 设置日志记录到文件 —— 参阅前一节了解详情\n"
"logging.basicConfig(level=logging.DEBUG,\n"
" format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',\n"
" datefmt='%m-%d %H:%M',\n"
" filename='/tmp/myapp.log',\n"
" filemode='w')\n"
"# 定义一个将 INFO 或更高层级消息写到 sys.stderr 的处理器\n"
"console = logging.StreamHandler()\n"
"console.setLevel(logging.INFO)\n"
"# 设置一个适用于控制台的更简单格式\n"
"formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')\n"
"# 告诉处理器使用此格式\n"
"console.setFormatter(formatter)\n"
"# 将处理器添加到根日志记录器\n"
"logging.getLogger().addHandler(console)\n"
"\n"
"# 现在我们可以写入根记录器或任何其他记录器。首先是根记录器...\n"
"logging.info('Jackdaws love my big sphinx of quartz.')\n"
"\n"
"# 现在,定义几个可以代表你的应用程序中不同组成部分的\n"
"# 其他日志记录器:\n"
"\n"
"logger1 = logging.getLogger('myapp.area1')\n"
"logger2 = logging.getLogger('myapp.area2')\n"
"\n"
"logger1.debug('Quick zephyrs blow, vexing daft Jim.')\n"
"logger1.info('How quickly daft jumping zebras vex.')\n"
"logger2.warning('Jail zesty vixen who grabbed pay from quack.')\n"
"logger2.error('The five boxing wizards jump quickly.')"
#: ../../howto/logging-cookbook.rst:248
msgid "When you run this, on the console you will see"
msgstr "当运行后,你会看到控制台如下所示"
#: ../../howto/logging-cookbook.rst:250
msgid ""
"root : INFO Jackdaws love my big sphinx of quartz.\n"
"myapp.area1 : INFO How quickly daft jumping zebras vex.\n"
"myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.\n"
"myapp.area2 : ERROR The five boxing wizards jump quickly."
msgstr ""
"root : INFO Jackdaws love my big sphinx of quartz.\n"
"myapp.area1 : INFO How quickly daft jumping zebras vex.\n"
"myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.\n"
"myapp.area2 : ERROR The five boxing wizards jump quickly."
#: ../../howto/logging-cookbook.rst:257
msgid "and in the file you will see something like"
msgstr "而日志文件将如下所示:"
#: ../../howto/logging-cookbook.rst:259
msgid ""
"10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.\n"
"10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.\n"
"10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.\n"
"10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.\n"
"10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly."
msgstr ""
"10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.\n"
"10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.\n"
"10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.\n"
"10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.\n"
"10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly."
#: ../../howto/logging-cookbook.rst:267
msgid ""
"As you can see, the DEBUG message only shows up in the file. The other "
"messages are sent to both destinations."
msgstr "如您所见,DEBUG 级别的日志信息只出现在了文件中,而其他信息则两个地方都会输出。"
#: ../../howto/logging-cookbook.rst:270
msgid ""
"This example uses console and file handlers, but you can use any number and "
"combination of handlers you choose."
msgstr "上述示例只用到了控制台和文件 handler,当然还可以自由组合任意数量的日志 handler。"
#: ../../howto/logging-cookbook.rst:273
msgid ""
"Note that the above choice of log filename ``/tmp/myapp.log`` implies use of"
" a standard location for temporary files on POSIX systems. On Windows, you "
"may need to choose a different directory name for the log - just ensure that"
" the directory exists and that you have the permissions to create and update"
" files in it."
msgstr ""
"请注意上面选择的日志文件名 ``/tmp/myapp.log`` 表示在 POSIX 系统上使用临时文件的标准位置。在 Windows "
"上,你可能需要为日志选择不同的目录名称 —— 只要确保该目录存在并且你有在其中创建和更新文件的权限。"
#: ../../howto/logging-cookbook.rst:282
msgid "Custom handling of levels"
msgstr "自定义处理级别"
#: ../../howto/logging-cookbook.rst:284
msgid ""
"Sometimes, you might want to do something slightly different from the "
"standard handling of levels in handlers, where all levels above a threshold "
"get processed by a handler. To do this, you need to use filters. Let's look "
"at a scenario where you want to arrange things as follows:"
msgstr ""
"有时,你想要做的可能略微不同于处理器中标准的级别处理方式,即某个界限以上的所有级别都会被处理器所处理。要做到这一点,你需要使用过滤器。 "
"让我们来看一个假设你想要执行如下安排的场景:"
#: ../../howto/logging-cookbook.rst:289
msgid "Send messages of severity ``INFO`` and ``WARNING`` to ``sys.stdout``"
msgstr "将严重级别为 ``INFO`` 和 ``WARNING`` 的消息发送到 ``sys.stdout``"
#: ../../howto/logging-cookbook.rst:290
msgid "Send messages of severity ``ERROR`` and above to ``sys.stderr``"
msgstr "将严重级别为 ``ERROR`` 及以上的消息发送到 ``sys.stderr``"
#: ../../howto/logging-cookbook.rst:291
msgid "Send messages of severity ``DEBUG`` and above to file ``app.log``"
msgstr "将严重级别为 ``DEBUG`` 及以上的消息发送到文件 ``app.log``"
#: ../../howto/logging-cookbook.rst:293
msgid "Suppose you configure logging with the following JSON:"
msgstr "假定你使用以下 JSON 来配置日志记录:"
#: ../../howto/logging-cookbook.rst:295
msgid ""
"{\n"
" \"version\": 1,\n"
" \"disable_existing_loggers\": false,\n"
" \"formatters\": {\n"
" \"simple\": {\n"
" \"format\": \"%(levelname)-8s - %(message)s\"\n"
" }\n"
" },\n"
" \"handlers\": {\n"
" \"stdout\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"INFO\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stdout\"\n"
" },\n"
" \"stderr\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"ERROR\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stderr\"\n"
" },\n"
" \"file\": {\n"
" \"class\": \"logging.FileHandler\",\n"
" \"formatter\": \"simple\",\n"
" \"filename\": \"app.log\",\n"
" \"mode\": \"w\"\n"
" }\n"
" },\n"
" \"root\": {\n"
" \"level\": \"DEBUG\",\n"
" \"handlers\": [\n"
" \"stderr\",\n"
" \"stdout\",\n"
" \"file\"\n"
" ]\n"
" }\n"
"}"
msgstr ""
"{\n"
" \"version\": 1,\n"
" \"disable_existing_loggers\": false,\n"
" \"formatters\": {\n"
" \"simple\": {\n"
" \"format\": \"%(levelname)-8s - %(message)s\"\n"
" }\n"
" },\n"
" \"handlers\": {\n"
" \"stdout\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"INFO\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stdout\"\n"
" },\n"
" \"stderr\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"ERROR\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stderr\"\n"
" },\n"
" \"file\": {\n"
" \"class\": \"logging.FileHandler\",\n"
" \"formatter\": \"simple\",\n"
" \"filename\": \"app.log\",\n"
" \"mode\": \"w\"\n"
" }\n"
" },\n"
" \"root\": {\n"
" \"level\": \"DEBUG\",\n"
" \"handlers\": [\n"
" \"stderr\",\n"
" \"stdout\",\n"
" \"file\"\n"
" ]\n"
" }\n"
"}"
#: ../../howto/logging-cookbook.rst:335
msgid ""
"This configuration does *almost* what we want, except that ``sys.stdout`` "
"would show messages of severity ``ERROR`` and only events of this severity "
"and higher will be tracked as well as ``INFO`` and ``WARNING`` messages. To "
"prevent this, we can set up a filter which excludes those messages and add "
"it to the relevant handler. This can be configured by adding a ``filters`` "
"section parallel to ``formatters`` and ``handlers``:"
msgstr ""
"这个配置 *几乎* 能做到我们想要的,但是除了 ``sys.stdout`` 在 ``INFO`` 和 ``WARNING`` 消息之外会只显示严重程度"
" ``ERROR`` 及以上的消息。为了防止这种情况,我们可以设置一个排除掉这些消息的过滤器并将其添加到相应的处理器中。这可以通过添加一个平行于 "
"``formatters`` 和 ``handlers`` 的 ``filters`` 节来配置:"
#: ../../howto/logging-cookbook.rst:341
msgid ""
"{\n"
" \"filters\": {\n"
" \"warnings_and_below\": {\n"
" \"()\" : \"__main__.filter_maker\",\n"
" \"level\": \"WARNING\"\n"
" }\n"
" }\n"
"}"
msgstr ""
"{\n"
" \"filters\": {\n"
" \"warnings_and_below\": {\n"
" \"()\" : \"__main__.filter_maker\",\n"
" \"level\": \"WARNING\"\n"
" }\n"
" }\n"
"}"
#: ../../howto/logging-cookbook.rst:352
msgid "and changing the section on the ``stdout`` handler to add it:"
msgstr "并修改 ``stdout`` 处理器上的节来添加它:"
#: ../../howto/logging-cookbook.rst:354
msgid ""
"{\n"
" \"stdout\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"INFO\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stdout\",\n"
" \"filters\": [\"warnings_and_below\"]\n"
" }\n"
"}"
msgstr ""
"{\n"
" \"stdout\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"INFO\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stdout\",\n"
" \"filters\": [\"warnings_and_below\"]\n"
" }\n"
"}"
#: ../../howto/logging-cookbook.rst:366
msgid ""
"A filter is just a function, so we can define the ``filter_maker`` (a "
"factory function) as follows:"
msgstr "过滤器就是一个函数,因此我们可以定义 ``filter_maker`` (工厂函数) 如下:"
#: ../../howto/logging-cookbook.rst:369
msgid ""
"def filter_maker(level):\n"
" level = getattr(logging, level)\n"
"\n"
" def filter(record):\n"
" return record.levelno <= level\n"
"\n"
" return filter"
msgstr ""
"def filter_maker(level):\n"
" level = getattr(logging, level)\n"
"\n"
" def filter(record):\n"
" return record.levelno <= level\n"
"\n"
" return filter"
#: ../../howto/logging-cookbook.rst:379
msgid ""
"This converts the string argument passed in to a numeric level, and returns "
"a function which only returns ``True`` if the level of the passed in record "
"is at or below the specified level. Note that in this example I have defined"
" the ``filter_maker`` in a test script ``main.py`` that I run from the "
"command line, so its module will be ``__main__`` - hence the "
"``__main__.filter_maker`` in the filter configuration. You will need to "
"change that if you define it in a different module."
msgstr ""
"此函数将传入的字符串参数转换为数字级别,并返回一个仅在传入等于或低于指定数字级别的级别时返回 ``True`` 的函数。请注意在这个示例中我是将 "
"``filter_maker`` 定义在一个从命令行运行的测试脚本 ``main.py`` 中,因此其所属模块将为 ``__main__`` —— "
"即在过滤器配置中写作 ``__main__.filter_maker``。如果你在不同的模块中定义它则需要加以修改。"
#: ../../howto/logging-cookbook.rst:387
msgid "With the filter added, we can run ``main.py``, which in full is:"
msgstr "在添加该过滤器后,我们就可以运行 ``main.py``,完整代码如下:"
#: ../../howto/logging-cookbook.rst:389
msgid ""
"import json\n"
"import logging\n"
"import logging.config\n"
"\n"
"CONFIG = '''\n"
"{\n"
" \"version\": 1,\n"
" \"disable_existing_loggers\": false,\n"
" \"formatters\": {\n"
" \"simple\": {\n"
" \"format\": \"%(levelname)-8s - %(message)s\"\n"
" }\n"
" },\n"
" \"filters\": {\n"
" \"warnings_and_below\": {\n"
" \"()\" : \"__main__.filter_maker\",\n"
" \"level\": \"WARNING\"\n"
" }\n"
" },\n"
" \"handlers\": {\n"
" \"stdout\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"INFO\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stdout\",\n"
" \"filters\": [\"warnings_and_below\"]\n"
" },\n"
" \"stderr\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"ERROR\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stderr\"\n"
" },\n"
" \"file\": {\n"
" \"class\": \"logging.FileHandler\",\n"
" \"formatter\": \"simple\",\n"
" \"filename\": \"app.log\",\n"
" \"mode\": \"w\"\n"
" }\n"
" },\n"
" \"root\": {\n"
" \"level\": \"DEBUG\",\n"
" \"handlers\": [\n"
" \"stderr\",\n"
" \"stdout\",\n"
" \"file\"\n"
" ]\n"
" }\n"
"}\n"
"'''\n"
"\n"
"def filter_maker(level):\n"
" level = getattr(logging, level)\n"
"\n"
" def filter(record):\n"
" return record.levelno <= level\n"
"\n"
" return filter\n"
"\n"
"logging.config.dictConfig(json.loads(CONFIG))\n"
"logging.debug('A DEBUG message')\n"
"logging.info('An INFO message')\n"
"logging.warning('A WARNING message')\n"
"logging.error('An ERROR message')\n"
"logging.critical('A CRITICAL message')"
msgstr ""
"import json\n"
"import logging\n"
"import logging.config\n"
"\n"
"CONFIG = '''\n"
"{\n"
" \"version\": 1,\n"
" \"disable_existing_loggers\": false,\n"
" \"formatters\": {\n"
" \"simple\": {\n"
" \"format\": \"%(levelname)-8s - %(message)s\"\n"
" }\n"
" },\n"
" \"filters\": {\n"
" \"warnings_and_below\": {\n"
" \"()\" : \"__main__.filter_maker\",\n"
" \"level\": \"WARNING\"\n"
" }\n"
" },\n"
" \"handlers\": {\n"
" \"stdout\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"INFO\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stdout\",\n"
" \"filters\": [\"warnings_and_below\"]\n"
" },\n"
" \"stderr\": {\n"
" \"class\": \"logging.StreamHandler\",\n"
" \"level\": \"ERROR\",\n"
" \"formatter\": \"simple\",\n"
" \"stream\": \"ext://sys.stderr\"\n"
" },\n"
" \"file\": {\n"
" \"class\": \"logging.FileHandler\",\n"
" \"formatter\": \"simple\",\n"
" \"filename\": \"app.log\",\n"
" \"mode\": \"w\"\n"
" }\n"
" },\n"
" \"root\": {\n"
" \"level\": \"DEBUG\",\n"
" \"handlers\": [\n"
" \"stderr\",\n"
" \"stdout\",\n"
" \"file\"\n"
" ]\n"
" }\n"
"}\n"
"'''\n"
"\n"
"def filter_maker(level):\n"
" level = getattr(logging, level)\n"
"\n"
" def filter(record):\n"
" return record.levelno <= level\n"
"\n"
" return filter\n"
"\n"
"logging.config.dictConfig(json.loads(CONFIG))\n"
"logging.debug('A DEBUG message')\n"
"logging.info('An INFO message')\n"
"logging.warning('A WARNING message')\n"
"logging.error('An ERROR message')\n"
"logging.critical('A CRITICAL message')"
#: ../../howto/logging-cookbook.rst:457
msgid "And after running it like this:"
msgstr "使用这样的命令运行它之后:"
#: ../../howto/logging-cookbook.rst:459
msgid "python main.py 2>stderr.log >stdout.log"
msgstr "python main.py 2>stderr.log >stdout.log"
#: ../../howto/logging-cookbook.rst:463
msgid "We can see the results are as expected:"
msgstr "我们可以看到结果是符合预期的:"
#: ../../howto/logging-cookbook.rst:465
msgid ""
"$ more *.log\n"
"::::::::::::::\n"
"app.log\n"
"::::::::::::::\n"
"DEBUG - A DEBUG message\n"
"INFO - An INFO message\n"
"WARNING - A WARNING message\n"
"ERROR - An ERROR message\n"
"CRITICAL - A CRITICAL message\n"
"::::::::::::::\n"
"stderr.log\n"
"::::::::::::::\n"
"ERROR - An ERROR message\n"
"CRITICAL - A CRITICAL message\n"
"::::::::::::::\n"
"stdout.log\n"
"::::::::::::::\n"
"INFO - An INFO message\n"
"WARNING - A WARNING message"
msgstr ""
"$ more *.log\n"
"::::::::::::::\n"
"app.log\n"
"::::::::::::::\n"
"DEBUG - A DEBUG message\n"
"INFO - An INFO message\n"
"WARNING - A WARNING message\n"
"ERROR - An ERROR message\n"
"CRITICAL - A CRITICAL message\n"
"::::::::::::::\n"
"stderr.log\n"
"::::::::::::::\n"
"ERROR - An ERROR message\n"
"CRITICAL - A CRITICAL message\n"
"::::::::::::::\n"
"stdout.log\n"
"::::::::::::::\n"
"INFO - An INFO message\n"
"WARNING - A WARNING message"
#: ../../howto/logging-cookbook.rst:489
msgid "Configuration server example"
msgstr "日志配置服务器示例"
#: ../../howto/logging-cookbook.rst:491
msgid ""
"Here is an example of a module using the logging configuration server::"
msgstr "以下是一个用到了日志配置服务器的模块示例::"
#: ../../howto/logging-cookbook.rst:493
msgid ""
"import logging\n"
"import logging.config\n"
"import time\n"
"import os\n"
"\n"
"# read initial config file\n"
"logging.config.fileConfig('logging.conf')\n"
"\n"
"# create and start listener on port 9999\n"
"t = logging.config.listen(9999)\n"
"t.start()\n"
"\n"
"logger = logging.getLogger('simpleExample')\n"
"\n"
"try:\n"
" # loop through logging calls to see the difference\n"
" # new configurations make, until Ctrl+C is pressed\n"
" while True:\n"
" logger.debug('debug message')\n"
" logger.info('info message')\n"
" logger.warning('warn message')\n"
" logger.error('error message')\n"
" logger.critical('critical message')\n"
" time.sleep(5)\n"
"except KeyboardInterrupt:\n"
" # cleanup\n"
" logging.config.stopListening()\n"
" t.join()"
msgstr ""
"import logging\n"
"import logging.config\n"
"import time\n"
"import os\n"