- Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathlogging-cookbook.po
1781 lines (1565 loc) · 101 KB
/
logging-cookbook.po
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-2021, Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# ww song <sww4718168@gmail.com>, 2019
# 欢 王 <whthu168@163.com>, 2019
# 非法操作 <ultrahe@gmail.com>, 2019
# MuSheng Chen <sheng.2179@gmail.com>, 2019
# 浩听 王 <wht_andrew@163.com>, 2019
# Shengjing Zhu <zsj950618@gmail.com>, 2019
# Alpha Du <alphanow@gmail.com>, 2020
# Dai Xu <daixu61@hotmail.com>, 2021
# Freesand Leo <yuqinju@163.com>, 2021
#
#,fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Python 3.7\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-01-01 16:02+0000\n"
"PO-Revision-Date: 2019-09-01 03:37+0000\n"
"Last-Translator: Freesand Leo <yuqinju@163.com>, 2021\n"
"Language-Team: Chinese (China) (https://www.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."
msgstr"本页包含了许多日志记录相关的概念,这些概念在过去一直被认为很有用。"
#:../../howto/logging-cookbook.rst:15
msgid"Using logging in multiple modules"
msgstr"在多个模块中记录日志"
#:../../howto/logging-cookbook.rst:17
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')``会返回对同一个日志记录器对象的引用。不仅在同一个模块中是这样的,而且在不同模块之间,只要是在同一个Python解释器进程中,也是如此。"
" "
"这就是对同一个对象的多个引用;此外,应用程序代码也可以在一个模块中定义和配置父日志记录器,在单独的模块中创建(但不配置)一个子日志记录器,并且对子日志记录器的所有调用都将传递给父日志记录器。"
" 这里是一个主要模块::"
#:../../howto/logging-cookbook.rst:55
msgid"Here is the auxiliary module::"
msgstr"这里是辅助模块::"
#:../../howto/logging-cookbook.rst:75
msgid"The output looks like this:"
msgstr"输出结果会像这样:"
#:../../howto/logging-cookbook.rst:101
msgid"Logging from multiple threads"
msgstr"在多个线程中记录日志"
#:../../howto/logging-cookbook.rst:103
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:132
msgid"When run, the script should print something like the following:"
msgstr"脚本会运行输出类似下面的内容:"
#:../../howto/logging-cookbook.rst:154
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:158
msgid"Multiple handlers and formatters"
msgstr"多个日志处理器以及多种格式化器"
#:../../howto/logging-cookbook.rst:160
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` "
"方法对可以添加的日志处理器的数量没有限制。有时候,应用程序需要将所有严重性的所有消息记录到一个文本文件,而将错误或更高等级的消息输出到控制台。要进行这样的设定,只需配置适当的日志处理器即可。在应用程序代码中,记录日志的调用将保持不变。以下是对之前基于模块的简单配置示例的略微修改::"
#:../../howto/logging-cookbook.rst:193
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"需要注意的是,'应用程序' 代码并不关心是否有多个日志处理器。所有的改变的只是添加和配置了一个新的名为*fh*的日志处理器。"
#:../../howto/logging-cookbook.rst:196
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""
"在编写和测试应用程序时,能够创建带有更高或更低消息等级的过滤器的日志处理器是非常有用的。为了避免过多地使用 ``print`` 语句去调试,请使用 "
"``logger.debug`` :它不像 ``print`` 语句需要你不得不在调试结束后注释或删除掉,logger.debug "
"语句可以在源代码中保持不变,在你再一次需要它之前保持无效。那时,唯一需要改变的是修改日志记录器和/或日志处理器的消息等级,以进行调试。"
#:../../howto/logging-cookbook.rst:207
msgid"Logging to multiple destinations"
msgstr"在多个地方记录日志"
#:../../howto/logging-cookbook.rst:209
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:247
msgid"When you run this, on the console you will see"
msgstr"当运行后,你会看到控制台如下所示"
#:../../howto/logging-cookbook.rst:256
msgid"and in the file you will see something like"
msgstr"而在文件中会看到像这样"
#:../../howto/logging-cookbook.rst:266
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:269
msgid""
"This example uses console and file handlers, but you can use any number and "
"combination of handlers you choose."
msgstr"这个示例只演示了在控制台和文件中去记录日志,但你也可以自由组合任意数量的日志处理器。"
#:../../howto/logging-cookbook.rst:274
msgid"Configuration server example"
msgstr"日志服务器配置示例"
#:../../howto/logging-cookbook.rst:276
msgid""
"Here is an example of a module using the logging configuration server::"
msgstr"以下是在一个模块中使用日志服务器配置的示例::"
#:../../howto/logging-cookbook.rst:307
msgid""
"And here is a script that takes a filename and sends that file to the "
"server, properly preceded with the binary-encoded length, as the new logging"
" configuration::"
msgstr"然后如下的脚本,它接收文件名做为命令行参数,并将该文件以二进制编码的方式传给服务器,做为新的日志服务器配置::"
#:../../howto/logging-cookbook.rst:330
msgid"Dealing with handlers that block"
msgstr"处理日志处理器的阻塞"
#:../../howto/logging-cookbook.rst:334
msgid""
"Sometimes you have to get your logging handlers to do their work without "
"blocking the thread you're logging from. This is common in Web applications,"
" though of course it also occurs in other scenarios."
msgstr"有时候需要让日志处理程序在不阻塞当前正在记录线程的情况下完成工作。 这在Web应用程序中很常见,当然也会在其他场景中出现。"
#:../../howto/logging-cookbook.rst:338
msgid""
"A common culprit which demonstrates sluggish behaviour is the "
":class:`SMTPHandler`: sending emails can take a long time, for a number of "
"reasons outside the developer's control (for example, a poorly performing "
"mail or network infrastructure). But almost any network-based handler can "
"block: Even a :class:`SocketHandler` operation may do a DNS query under the "
"hood which is too slow (and this query can be deep in the socket library "
"code, below the Python layer, and outside your control)."
msgstr""
"一个常见的缓慢行为是 :class:`SMTPHandler`: "
"由于开发者无法控制的多种原因(例如,性能不佳的邮件或网络基础架构),发送电子邮件可能需要很长时间。 其实几乎所有基于网络的处理程序都可能造成阻塞:即便是"
" :class:`SocketHandler` 也可能在底层进行 DNS 查询,这太慢了(这个查询会深入至套接字代码,位于 Python "
"层之下,这是不受开发者控制的)。"
#:../../howto/logging-cookbook.rst:346
msgid""
"One solution is to use a two-part approach. For the first part, attach only "
"a :class:`QueueHandler` to those loggers which are accessed from "
"performance-critical threads. They simply write to their queue, which can be"
" sized to a large enough capacity or initialized with no upper bound to "
"their size. The write to the queue will typically be accepted quickly, "
"though you will probably need to catch the :exc:`queue.Full` exception as a "
"precaution in your code. If you are a library developer who has performance-"
"critical threads in their code, be sure to document this (together with a "
"suggestion to attach only ``QueueHandlers`` to your loggers) for the benefit"
" of other developers who will use your code."
msgstr""
"一种解决方案是分成两部分去处理。第一部分,针对那些对性能有要求的关键线程的日志记录附加一个 :class:`QueueHandler`。 "
"日志记录器只需简单写入队列,该队列可以设置一个足够大的容量甚至不设置容量上限。通常写入队列是一个快速的操作,即使可能需要在代码中去捕获例如 "
":exc:`queue.Full` 等异常。 如果你是一名处理关键线程的开发者,请务必记录这些信息 (包括建议只为日志处理器附加 "
"``QueueHandlers``) 以便于其他开发者使用你的代码。"
#:../../howto/logging-cookbook.rst:357
msgid""
"The second part of the solution is :class:`QueueListener`, which has been "
"designed as the counterpart to :class:`QueueHandler`. A "
":class:`QueueListener` is very simple: it's passed a queue and some "
"handlers, and it fires up an internal thread which listens to its queue for "
"LogRecords sent from ``QueueHandlers`` (or any other source of "
"``LogRecords``, for that matter). The ``LogRecords`` are removed from the "
"queue and passed to the handlers for processing."
msgstr""
"解决方案的另一部分是 :class:`QueueListener`,它被设计用来作为 :class:`QueueHandler` 的对应。 "
":class:`QueueListener` 非常简单:向其传入一个队列和一些处理句柄,它会启动一个内部线程来监听从 ``QueueHandlers``"
" (或任何其他可用的 ``LogRecords`` 源) 发送过来的 LogRecords 队列。 ``LogRecords`` "
"会从队列中被移除,并被传递给句柄进行处理。"
#:../../howto/logging-cookbook.rst:365
msgid""
"The advantage of having a separate :class:`QueueListener` class is that you "
"can use the same instance to service multiple ``QueueHandlers``. This is "
"more resource-friendly than, say, having threaded versions of the existing "
"handler classes, which would eat up one thread per handler for no particular"
" benefit."
msgstr""
"使用一个单独的类 "
":class:`QueueListener` 优点是可以使用同一个实例去服务于多个``QueueHandlers``。这样会更节省资源,否则每个处理程序都占用一个线程没有任何益处。"
#:../../howto/logging-cookbook.rst:370
msgid"An example of using these two classes follows (imports omitted)::"
msgstr"以下是使用了这样两个类的示例(省略了导入语句)::"
#:../../howto/logging-cookbook.rst:388
msgid"which, when run, will produce:"
msgstr"在运行后会产生:"
#:../../howto/logging-cookbook.rst:394
msgid""
"Prior to Python 3.5, the :class:`QueueListener` always passed every message "
"received from the queue to every handler it was initialized with. (This was "
"because it was assumed that level filtering was all done on the other side, "
"where the queue is filled.) From 3.5 onwards, this behaviour can be changed "
"by passing a keyword argument ``respect_handler_level=True`` to the "
"listener's constructor. When this is done, the listener compares the level "
"of each message with the handler's level, and only passes a message to a "
"handler if it's appropriate to do so."
msgstr""
"在Python "
"3.5之前,:class:`QueueListener` 总是把从队列中接收的每个消息都传给它初始化的日志处理程序。(这是因为它会假设过滤级别总是在队列的另一侧去设置的。)"
" 从Python "
"3.5开始,可以通过在监听器构造函数中添加一个参数``respect_handler_level=True``改变这种情况。当这样设置时,监听器会比较每条消息的等级和日志处理器中设置的等级,只把需要传递的消息传给对应的日志处理器。"
#:../../howto/logging-cookbook.rst:407
msgid"Sending and receiving logging events across a network"
msgstr"通过网络发送和接收日志"
#:../../howto/logging-cookbook.rst:409
msgid""
"Let's say you want to send logging events across a network, and handle them "
"at the receiving end. A simple way of doing this is attaching a "
":class:`SocketHandler` instance to the root logger at the sending end::"
msgstr""
"如果你想在网络上发送日志,并在接收端处理它们。一个简单的方式是通过附加一个 :class:`SocketHandler` "
"的实例在发送端的根日志处理器中::"
#:../../howto/logging-cookbook.rst:437
msgid""
"At the receiving end, you can set up a receiver using the "
":mod:`socketserver` module. Here is a basic working example::"
msgstr"在接收端,你可以使用 :mod:`socketserver` 模块设置一个接收器。这里是一个基础示例::"
#:../../howto/logging-cookbook.rst:525
msgid""
"First run the server, and then the client. On the client side, nothing is "
"printed on the console; on the server side, you should see something like:"
msgstr"首先运行服务端,然后是客户端。在客户端,没有什么内容会打印在控制台中;在服务端,你应该会看到如下内容:"
#:../../howto/logging-cookbook.rst:537
msgid""
"Note that there are some security issues with pickle in some scenarios. If "
"these affect you, you can use an alternative serialization scheme by "
"overriding the :meth:`~handlers.SocketHandler.makePickle` method and "
"implementing your alternative there, as well as adapting the above script to"
" use your alternative serialization."
msgstr""
"请注意,在某些情况下序列化会存在一些安全。如果这影响到你,那么你可以通过覆盖 "
":meth:`~handlers.SocketHandler.makePickle` "
"方法,使用自己的实现来解决,并调整上述脚本也使用覆盖后的序列化方法。"
#:../../howto/logging-cookbook.rst:547
msgid"Adding contextual information to your logging output"
msgstr"在日志记录中添加上下文信息"
#:../../howto/logging-cookbook.rst:549
msgid""
"Sometimes you want logging output to contain contextual information in "
"addition to the parameters passed to the logging call. For example, in a "
"networked application, it may be desirable to log client-specific "
"information in the log (e.g. remote client's username, or IP address). "
"Although you could use the *extra* parameter to achieve this, it's not "
"always convenient to pass the information in this way. While it might be "
"tempting to create :class:`Logger` instances on a per-connection basis, this"
" is not a good idea because these instances are not garbage collected. While"
" this is not a problem in practice, when the number of :class:`Logger` "
"instances is dependent on the level of granularity you want to use in "
"logging an application, it could be hard to manage if the number of "
":class:`Logger` instances becomes effectively unbounded."
msgstr""
"有时,除了传递给日志记录器调用的参数外,我们还希望日志记录中包含上下文信息。例如,有一个网络应用,可能需要记录一些特殊的客户端信息在日志中(比如客户端的用户名、IP地址等)。虽然你可以通过设置额外的参数去达到这个目的,但这种方式不一定方便。或者你可能想到在每个连接的基础上创建一个"
" :class:`Logger` 的实例,但这些实例是不会被垃圾回收的,这在练习中也许不是问题,但当 :class:`Logger` "
"的实例数量取决于你应用程序中想记录的细致程度时,如果 :class:`Logger` 的实例数量不受限制的话,将会变得难以管理。"
#:../../howto/logging-cookbook.rst:564
msgid"Using LoggerAdapters to impart contextual information"
msgstr"使用日志适配器传递上下文信息"
#:../../howto/logging-cookbook.rst:566
msgid""
"An easy way in which you can pass contextual information to be output along "
"with logging event information is to use the :class:`LoggerAdapter` class. "
"This class is designed to look like a :class:`Logger`, so that you can call "
":meth:`debug`, :meth:`info`, :meth:`warning`, :meth:`error`, "
":meth:`exception`, :meth:`critical` and :meth:`log`. These methods have the "
"same signatures as their counterparts in :class:`Logger`, so you can use the"
" two types of instances interchangeably."
msgstr""
"一个传递上下文信息和日志事件信息的简单办法是使用类 :class:`LoggerAdapter`。 这个类设计的像 "
":class:`Logger`,所以可以直接调用 :meth:`debug`、:meth:`info`、 :meth:`warning`、 "
":meth:`error`、:meth:`exception`、 :meth:`critical` 和 :meth:`log`。 这些方法在对应的 "
":class:`Logger` 中使用相同的签名,所以可以交替使用两种类型的实例。"
#:../../howto/logging-cookbook.rst:574
msgid""
"When you create an instance of :class:`LoggerAdapter`, you pass it a "
":class:`Logger` instance and a dict-like object which contains your "
"contextual information. When you call one of the logging methods on an "
"instance of :class:`LoggerAdapter`, it delegates the call to the underlying "
"instance of :class:`Logger` passed to its constructor, and arranges to pass "
"the contextual information in the delegated call. Here's a snippet from the "
"code of :class:`LoggerAdapter`::"
msgstr""
"当你创建一个 :class:`LoggerAdapter` 的实例时,你会传入一个 :class:`Logger` "
"的实例和一个包含了上下文信息的字典对象。当你调用一个 :class:`LoggerAdapter` 实例的方法时,它会把调用委托给内部的 "
":class:`Logger` 的实例,并为其整理相关的上下文信息。这是 :class:`LoggerAdapter` 的一个代码片段::"
#:../../howto/logging-cookbook.rst:590
msgid""
"The :meth:`~LoggerAdapter.process` method of :class:`LoggerAdapter` is where"
" the contextual information is added to the logging output. It's passed the "
"message and keyword arguments of the logging call, and it passes back "
"(potentially) modified versions of these to use in the call to the "
"underlying logger. The default implementation of this method leaves the "
"message alone, but inserts an 'extra' key in the keyword argument whose "
"value is the dict-like object passed to the constructor. Of course, if you "
"had passed an 'extra' keyword argument in the call to the adapter, it will "
"be silently overwritten."
msgstr""
":class:`LoggerAdapter` 的 :meth:`~LoggerAdapter.process` 方法是将上下文信息添加到日志的输出中。 "
"它传入日志消息和日志调用的关键字参数,并传回(隐式的)这些修改后的内容去调用底层的日志记录器。此方法的默认参数只是一个消息字段,但留有一个 "
"'extra' 的字段作为关键字参数传给构造器。当然,如果你在调用适配器时传入了一个 'extra' 字段的参数,它会被静默覆盖。"
#:../../howto/logging-cookbook.rst:599
msgid""
"The advantage of using 'extra' is that the values in the dict-like object "
"are merged into the :class:`LogRecord` instance's __dict__, allowing you to "
"use customized strings with your :class:`Formatter` instances which know "
"about the keys of the dict-like object. If you need a different method, e.g."
" if you want to prepend or append the contextual information to the message "
"string, you just need to subclass :class:`LoggerAdapter` and override "
":meth:`~LoggerAdapter.process` to do what you need. Here is a simple "
"example::"
msgstr""
"使用 'extra' 的优点是这些键值对会被传入 :class:`LogRecord` 实例的 __dict__ 中,让你通过 "
":class:`Formatter` 的实例直接使用定制的字符串,实例能找到这个字典类对象的键。 "
"如果你需要一个其他的方法,比如说,想要在消息字符串前后增加上下文信息,你只需要创建一个 :class:`LoggerAdapter` 的子类,并覆盖它的"
" :meth:`~LoggerAdapter.process` 方法来做你想做的事情,以下是一个简单的示例::"
#:../../howto/logging-cookbook.rst:615
msgid"which you can use like this::"
msgstr"你可以这样使用::"
#:../../howto/logging-cookbook.rst:620
msgid""
"Then any events that you log to the adapter will have the value of "
"``some_conn_id`` prepended to the log messages."
msgstr"然后,你记录在适配器中的任何事件消息前将添加``some_conn_id``的值。"
#:../../howto/logging-cookbook.rst:624
msgid"Using objects other than dicts to pass contextual information"
msgstr"使用除字典之外的其它对象传递上下文信息"
#:../../howto/logging-cookbook.rst:626
msgid""
"You don't need to pass an actual dict to a :class:`LoggerAdapter` - you "
"could pass an instance of a class which implements ``__getitem__`` and "
"``__iter__`` so that it looks like a dict to logging. This would be useful "
"if you want to generate values dynamically (whereas the values in a dict "
"would be constant)."
msgstr""
"你不需要将一个实际的字典传递给 :class:`LoggerAdapter`-你可以传入一个实现了``__getitem__`` "
"和``__iter__``的类的实例,这样它就像是一个字典。这对于你想动态生成值(而字典中的值往往是常量)将很有帮助。"
#:../../howto/logging-cookbook.rst:635
msgid"Using Filters to impart contextual information"
msgstr"使用过滤器传递上下文信息"
#:../../howto/logging-cookbook.rst:637
msgid""
"You can also add contextual information to log output using a user-defined "
":class:`Filter`. ``Filter`` instances are allowed to modify the "
"``LogRecords`` passed to them, including adding additional attributes which "
"can then be output using a suitable format string, or if needed a custom "
":class:`Formatter`."
msgstr""
"你也可以使用一个用户定义的类 :class:`Filter` 在日志输出中添加上下文信息。``Filter`` 的实例是被允许修改传入的 "
"``LogRecords``,包括添加其他的属性,然后可以使用合适的格式化字符串输出,或者可以使用一个自定义的类 :class:`Formatter`。"
#:../../howto/logging-cookbook.rst:642
msgid""
"For example in a web application, the request being processed (or at least, "
"the interesting parts of it) can be stored in a threadlocal "
"(:class:`threading.local`) variable, and then accessed from a ``Filter`` to "
"add, say, information from the request - say, the remote IP address and "
"remote user's username - to the ``LogRecord``, using the attribute names "
"'ip' and 'user' as in the ``LoggerAdapter`` example above. In that case, the"
" same format string can be used to get similar output to that shown above. "
"Here's an example script::"
msgstr""
"例如,在一个web应用程序中,正在处理的请求(或者至少是请求的一部分),可以存储在一个线程本地(:class:`threading.local`) "
"变量中,然后从``Filter``中去访问。请求中的信息,如IP地址和用户名将被存储在``LogRecord``中,使用上例``LoggerAdapter``中的"
" 'ip' 和 'user' 属性名。在这种情况下,可以使用相同的格式化字符串来得到上例中类似的输出结果。这是一段示例代码::"
#:../../howto/logging-cookbook.rst:688
msgid"which, when run, produces something like:"
msgstr"在运行时,产生如下内容:"
#:../../howto/logging-cookbook.rst:709
msgid"Logging to a single file from multiple processes"
msgstr"从多个进程记录至单个文件"
#:../../howto/logging-cookbook.rst:711
msgid""
"Although logging is thread-safe, and logging to a single file from multiple "
"threads in a single process *is* supported, logging to a single file from "
"*multiple processes* is *not* supported, because there is no standard way to"
" serialize access to a single file across multiple processes in Python. If "
"you need to log to a single file from multiple processes, one way of doing "
"this is to have all the processes log to a :class:`~handlers.SocketHandler`,"
" and have a separate process which implements a socket server which reads "
"from the socket and logs to file. (If you prefer, you can dedicate one "
"thread in one of the existing processes to perform this function.) "
":ref:`This section <network-logging>` documents this approach in more detail"
" and includes a working socket receiver which can be used as a starting "
"point for you to adapt in your own applications."
msgstr""
"尽管 logging 是线程安全的,将单个进程中的多个线程日志记录至单个文件也 *是* 受支持的,但将 *多个进程* 中的日志记录至单个文件则 *不是*"
" 受支持的,因为在 Python 中并没有在多个进程中实现对单个文件访问的序列化的标准方案。 "
"如果你需要将多个进程中的日志记录至单个文件,有一个方案是让所有进程都将日志记录至一个 "
":class:`~handlers.SocketHandler`,然后用一个实现了套接字服务器的单独进程一边从套接字中读取一边将日志记录至文件。 "
"(如果愿意的话,你可以在一个现有进程中专门开一个线程来执行此项功能。) :ref:`这一部分 <network-logging>` "
"文档对此方式有更详细的介绍,并包含一个可用的套接字接收器,你自己的应用可以在此基础上进行适配。"
#:../../howto/logging-cookbook.rst:724
msgid""
"If you are using a recent version of Python which includes the "
":mod:`multiprocessing` module, you could write your own handler which uses "
"the :class:`~multiprocessing.Lock` class from this module to serialize "
"access to the file from your processes. The existing :class:`FileHandler` "
"and subclasses do not make use of :mod:`multiprocessing` at present, though "
"they may do so in the future. Note that at present, the "
":mod:`multiprocessing` module does not provide working lock functionality on"
" all platforms (see https://bugs.python.org/issue3770)."
msgstr""
"如果你使用的是包含了 :mod:`multiprocessing` 模块的较新版本的 Python,你也可以使用 "
":class:`~multiprocessing.Lock` 来编写自己的处理程序让其从多个进程中按顺序记录至文件。 现有的 "
":class:`FileHandler` 和它的子类目前没有使用 :mod:`multiprocessing`,尽管将来可能会这样做。 请注意目前 "
":mod:`multiprocessing` 模块并非在所有平台上提供可用的锁功能 (参见 "
"https://bugs.python.org/issue3770)。"
#:../../howto/logging-cookbook.rst:735
msgid""
"Alternatively, you can use a ``Queue`` and a :class:`QueueHandler` to send "
"all logging events to one of the processes in your multi-process "
"application. The following example script demonstrates how you can do this; "
"in the example a separate listener process listens for events sent by other "
"processes and logs them according to its own logging configuration. Although"
" the example only demonstrates one way of doing it (for example, you may "
"want to use a listener thread rather than a separate listener process -- the"
" implementation would be analogous) it does allow for completely different "
"logging configurations for the listener and the other processes in your "
"application, and can be used as the basis for code meeting your own specific"
" requirements::"
msgstr""
"或者,你也可以使用 ``Queue`` 和 :class:`QueueHandler` 将所有的日志事件发送至你的多进程应用的一个进程中。 "
"以下示例脚本演示了如何执行此操作。 在示例中,一个单独的监听进程负责监听其他进程的日志事件,并根据自己的配置记录。 "
"尽管示例只演示了这种方法(例如你可能希望使用单独的监听线程而非监听进程 —— "
"它们的实现是类似的),但你也可以在应用程序的监听进程和其他进程使用不同的配置,它可以作为满足你特定需求的一个基础::"
#:../../howto/logging-cookbook.rst:851
msgid""
"A variant of the above script keeps the logging in the main process, in a "
"separate thread::"
msgstr"上面脚本的一个变种,仍然在主进程中记录日志,但使用一个单独的线程::"
#:../../howto/logging-cookbook.rst:946
msgid""
"This variant shows how you can e.g. apply configuration for particular "
"loggers - e.g. the ``foo`` logger has a special handler which stores all "
"events in the ``foo`` subsystem in a file ``mplog-foo.log``. This will be "
"used by the logging machinery in the main process (even though the logging "
"events are generated in the worker processes) to direct the messages to the "
"appropriate destinations."
msgstr""
"这段变种的代码展示了如何使用特定的日志记录配置 - 例如``foo``记录器使用了特殊的处理程序,将 ``foo`` 子系统中所有的事件记录至一个文件 "
"``mplog-foo.log``。在主进程(即使是在工作进程中产生的日志事件)的日志记录机制中将直接使用恰当的配置。"
#:../../howto/logging-cookbook.rst:953
msgid"Using concurrent.futures.ProcessPoolExecutor"
msgstr"concurrent.futures.ProcessPoolExecutor 的用法"
#:../../howto/logging-cookbook.rst:955
msgid""
"If you want to use :class:`concurrent.futures.ProcessPoolExecutor` to start "
"your worker processes, you need to create the queue slightly differently. "
"Instead of"
msgstr""
"若要利用 :class:`concurrent.futures.ProcessPoolExecutor` "
"启动工作进程,创建队列的方式应稍有不同。不能是:"
#:../../howto/logging-cookbook.rst:963
msgid"you should use"
msgstr"而应是:"
#:../../howto/logging-cookbook.rst:969
msgid"and you can then replace the worker creation from this::"
msgstr"然后就可以将以下工作进程的创建过程:"
#:../../howto/logging-cookbook.rst:980
msgid"to this (remembering to first import :mod:`concurrent.futures`)::"
msgstr"改为 (记得要先导入 :mod:`concurrent.futures`)::"
#:../../howto/logging-cookbook.rst:988
msgid"Using file rotation"
msgstr"轮换日志文件"
#:../../howto/logging-cookbook.rst:993
msgid""
"Sometimes you want to let a log file grow to a certain size, then open a new"
" file and log to that. You may want to keep a certain number of these files,"
" and when that many files have been created, rotate the files so that the "
"number of files and the size of the files both remain bounded. For this "
"usage pattern, the logging package provides a "
":class:`~handlers.RotatingFileHandler`::"
msgstr""
"有时,你希望当日志文件不断记录增长至一定大小时,打开一个新的文件接着记录。 "
"你可能希望只保留一定数量的日志文件,当不断的创建文件到达该数量时,又覆盖掉最开始的文件形成循环。 对于这种使用场景,日志包提供了 "
":class:`~handlers.RotatingFileHandler`::"
#:../../howto/logging-cookbook.rst:1025
msgid""
"The result should be 6 separate files, each with part of the log history for"
" the application:"
msgstr"结果应该是6个单独的文件,每个文件都包含了应用程序的部分历史日志:"
#:../../howto/logging-cookbook.rst:1037
msgid""
"The most current file is always :file:`logging_rotatingfile_example.out`, "
"and each time it reaches the size limit it is renamed with the suffix "
"``.1``. Each of the existing backup files is renamed to increment the suffix"
" (``.1`` becomes ``.2``, etc.) and the ``.6`` file is erased."
msgstr""
"最新的文件始终是:file:`logging_rotatingfile_example.out`,每次到达大小限制时,都会使用后缀``.1``重命名。每个现有的备份文件都会被重命名并增加其后缀(例如``.1``"
" 变为``.2``),而``.6``文件会被删除掉。"
#:../../howto/logging-cookbook.rst:1042
msgid""
"Obviously this example sets the log length much too small as an extreme "
"example. You would want to set *maxBytes* to an appropriate value."
msgstr"显然,这个例子将日志长度设置得太小,这是一个极端的例子。 你可能希望将*maxBytes*设置为一个合适的值。"
#:../../howto/logging-cookbook.rst:1048
msgid"Use of alternative formatting styles"
msgstr"使用其他日志格式化方式"
#:../../howto/logging-cookbook.rst:1050
msgid""
"When logging was added to the Python standard library, the only way of "
"formatting messages with variable content was to use the %-formatting "
"method. Since then, Python has gained two new formatting approaches: "
":class:`string.Template` (added in Python 2.4) and :meth:`str.format` (added"
" in Python 2.6)."
msgstr""
"当日志模块被添加至 Python 标准库时,只有一种格式化消息内容的方法即 %-formatting。 在那之后,Python 又增加了两种格式化方法:"
" :class:`string.Template` (在 Python 2.4 中新增) 和 :meth:`str.format` (在 Python "
"2.6 中新增)。"
#:../../howto/logging-cookbook.rst:1056
msgid""
"Logging (as of 3.2) provides improved support for these two additional "
"formatting styles. The :class:`Formatter` class been enhanced to take an "
"additional, optional keyword parameter named ``style``. This defaults to "
"``'%'``, but other possible values are ``'{'`` and ``'$'``, which correspond"
" to the other two formatting styles. Backwards compatibility is maintained "
"by default (as you would expect), but by explicitly specifying a style "
"parameter, you get the ability to specify format strings which work with "
":meth:`str.format` or :class:`string.Template`. Here's an example console "
"session to show the possibilities:"
msgstr""
"日志(从 3.2 开始)为这两种格式化方式提供了更多支持。:class:`Formatter` 类可以添加一个额外的可选关键字参数 "
"``style``。它的默认值是 ``'%'``,其他的值 ``'{'`` 和 ``'$'`` "
"也支持,对应了其他两种格式化样式。其保持了向后兼容(如您所愿),但通过显示指定样式参数,你可以指定格式化字符串的方式是使用 "
":meth:`str.format` 或 :class:`string.Template`。 这里是一个控制台会话的示例,展示了这些方式:"
#:../../howto/logging-cookbook.rst:1090
msgid""
"Note that the formatting of logging messages for final output to logs is "
"completely independent of how an individual logging message is constructed. "
"That can still use %-formatting, as shown here::"
msgstr"请注意最终输出到日志的消息格式完全独立于单条日志消息的构造方式。 它仍然可以使用 %-formatting,如下所示::"
#:../../howto/logging-cookbook.rst:1098
msgid""
"Logging calls (``logger.debug()``, ``logger.info()`` etc.) only take "
"positional parameters for the actual logging message itself, with keyword "
"parameters used only for determining options for how to handle the actual "
"logging call (e.g. the ``exc_info`` keyword parameter to indicate that "
"traceback information should be logged, or the ``extra`` keyword parameter "
"to indicate additional contextual information to be added to the log). So "
"you cannot directly make logging calls using :meth:`str.format` or "
":class:`string.Template` syntax, because internally the logging package uses"
" %-formatting to merge the format string and the variable arguments. There "
"would be no changing this while preserving backward compatibility, since all"
" logging calls which are out there in existing code will be using %-format "
"strings."
msgstr""
"日志调用(``logger.debug()`` 、``logger.info()`` "
"等)接受的位置参数只会用于日志信息本身,而关键字参数仅用于日志调用的可选处理参数(如关键字参数 ``exc_info`` 表示应记录跟踪信息, "
"``extra`` 则标识了需要加入日志的额外上下文信息)。所以不能直接用 :meth:`str.format` 或 "
":class:`string.Template` 语法进行日志调用,因为日志包在内部使用 %-f "
"格式来合并格式串和参数变量。在保持向下兼容性时,这一点不会改变,因为已有代码中的所有日志调用都会使用%-f 格式串。"
#:../../howto/logging-cookbook.rst:1111
msgid""
"There is, however, a way that you can use {}- and $- formatting to construct"
" your individual log messages. Recall that for a message you can use an "
"arbitrary object as a message format string, and that the logging package "
"will call ``str()`` on that object to get the actual format string. Consider"
" the following two classes::"
msgstr""
"还有一种方法可以构建自己的日志信息,就是利用 {}- 和 $- 格式。回想一下,任意对象都可用为日志信息的格式串,日志包将会调用该对象的 "
"``str()`` 方法,以获取最终的格式串。不妨看下一下两个类:"
#:../../howto/logging-cookbook.rst:1135
msgid""
"Either of these can be used in place of a format string, to allow {}- or "
"$-formatting to be used to build the actual \"message\" part which appears "
"in the formatted log output in place of \"%(message)s\" or \"{message}\" or "
"\"$message\". It's a little unwieldy to use the class names whenever you "
"want to log something, but it's quite palatable if you use an alias such as "
"__ (double underscore --- not to be confused with _, the single underscore "
"used as a synonym/alias for :func:`gettext.gettext` or its brethren)."
msgstr""
"上述两个类均可代替格式串,使得能用 {}- 或 $-formatting 构建最终的“日志信息”部分,这些信息将出现在格式化后的日志输出中,替换 "
"%(message)s 或“{message}”或“$message”。每次写入日志时都要使用类名,有点不大实用,但如果用上 __ "
"之类的别名就相当合适了(双下划线 --- 不要与 _ 混淆,单下划线用作 :func:`gettext.gettext` 或相关函数的同义词/别名 )。"
#:../../howto/logging-cookbook.rst:1143
msgid""
"The above classes are not included in Python, though they're easy enough to "
"copy and paste into your own code. They can be used as follows (assuming "
"that they're declared in a module called ``wherever``):"
msgstr"Python 并没有上述两个类,当然复制粘贴到自己的代码中也很容易。用法可如下所示(假定在名为 ``wherever`` 的模块中声明):"
#:../../howto/logging-cookbook.rst:1165
msgid""
"While the above examples use ``print()`` to show how the formatting works, "
"you would of course use ``logger.debug()`` or similar to actually log using "
"this approach."
msgstr""
"上述示例用了 ``print()`` 演示格式化输出的过程,实际记录日志时当然会用类似 ``logger.debug()`` 的方法来应用。 "
#:../../howto/logging-cookbook.rst:1169
msgid""
"One thing to note is that you pay no significant performance penalty with "
"this approach: the actual formatting happens not when you make the logging "
"call, but when (and if) the logged message is actually about to be output to"
" a log by a handler. So the only slightly unusual thing which might trip you"
" up is that the parentheses go around the format string and the arguments, "
"not just the format string. That's because the __ notation is just syntax "
"sugar for a constructor call to one of the XXXMessage classes."
msgstr""
"值得注意的是,上述做法对性能并没什么影响:格式化过程其实不是在日志记录调用时发生的,而是在日志信息即将由 handler "
"输出到日志时发生。因此,唯一可能让人困惑的稍不寻常的地方,就是包裹在格式串和参数外面的括号,而不是格式串。因为 __ 符号只是对 XXXMessage "
"类的构造函数调用的语法糖。"
#:../../howto/logging-cookbook.rst:1177
msgid""
"If you prefer, you can use a :class:`LoggerAdapter` to achieve a similar "
"effect to the above, as in the following example::"
msgstr"只要愿意,上述类似的效果即可用 :class:`LoggerAdapter` 实现,如下例所示:"
#:../../howto/logging-cookbook.rst:1208
msgid""
"The above script should log the message ``Hello, world!`` when run with "
"Python 3.2 or later."
msgstr"在用 Python 3.2 以上版本运行时,上述代码应该会把 ``Hello, world!`` 写入日志。"
#:../../howto/logging-cookbook.rst:1217
msgid"Customizing ``LogRecord``"
msgstr"自定义 ``LogRecord``"
#:../../howto/logging-cookbook.rst:1219
msgid""
"Every logging event is represented by a :class:`LogRecord` instance. When an"
" event is logged and not filtered out by a logger's level, a "
":class:`LogRecord` is created, populated with information about the event "
"and then passed to the handlers for that logger (and its ancestors, up to "
"and including the logger where further propagation up the hierarchy is "
"disabled). Before Python 3.2, there were only two places where this creation"
" was done:"
msgstr""
"每条日志事件都由一个 :class:`LogRecord` 实例表示。当某事件要记入日志并且没有被某级别过滤掉时,就会创建一个 "
":class:`LogRecord` 对象,并将有关事件的信息填入,传给该日志对象的 handler(及其祖先,直至对象禁止向上传播为止)。在 "
"Python 3.2 之前,只有两个地方会进行事件的创建:"
#:../../howto/logging-cookbook.rst:1226
msgid""
":meth:`Logger.makeRecord`, which is called in the normal process of logging "
"an event. This invoked :class:`LogRecord` directly to create an instance."
msgstr""
":meth:`Logger.makeRecord`,在事件正常记入日志的过程中调用。这会直接调用 :class:`LogRecord` 来创建一个实例。"
#:../../howto/logging-cookbook.rst:1229
msgid""
":func:`makeLogRecord`, which is called with a dictionary containing "
"attributes to be added to the LogRecord. This is typically invoked when a "
"suitable dictionary has been received over the network (e.g. in pickle form "
"via a :class:`~handlers.SocketHandler`, or in JSON form via an "
":class:`~handlers.HTTPHandler`)."
msgstr""
":func:`makeLogRecord`,调用时会带上一个字典参数,其中存放着要加入 LogRecord "
"的属性。这通常在通过网络接收到合适的字典时调用(如通过 :class:`~handlers.SocketHandler` 以 pickle 形式,或通过"
" :class:`~handlers.HTTPHandler` 以 JSON 形式)。"
#:../../howto/logging-cookbook.rst:1235
msgid""
"This has usually meant that if you need to do anything special with a "
":class:`LogRecord`, you've had to do one of the following."
msgstr"于是这意味着若要对 :class:`LogRecord` 进行定制,必须进行下述某种操作。"
#:../../howto/logging-cookbook.rst:1238
msgid""
"Create your own :class:`Logger` subclass, which overrides "
":meth:`Logger.makeRecord`, and set it using :func:`~logging.setLoggerClass` "
"before any loggers that you care about are instantiated."
msgstr""
"创建 :class:`Logger` 自定义子类,重写 :meth:`Logger.makeRecord`,并在实例化所需日志对象之前用 "
":func:`~logging.setLoggerClass` 进行设置。"
#:../../howto/logging-cookbook.rst:1241
msgid""
"Add a :class:`Filter` to a logger or handler, which does the necessary "
"special manipulation you need when its :meth:`~Filter.filter` method is "
"called."
msgstr""
"为日志对象添加 :class:`Filter` 或 handler,当其 :meth:`~Filter.filter` "
"方法被调用时,会执行必要的定制操作。"
#:../../howto/logging-cookbook.rst:1245
msgid""
"The first approach would be a little unwieldy in the scenario where (say) "
"several different libraries wanted to do different things. Each would "
"attempt to set its own :class:`Logger` subclass, and the one which did this "
"last would win."
msgstr""
"比如说在有多个不同库要完成不同操作的场景下,第一种方式会有点笨拙。 每次都要尝试设置自己的 :class:`Logger` "
"子类,而起作用的是最后一次尝试。"
#:../../howto/logging-cookbook.rst:1250
msgid""
"The second approach works reasonably well for many cases, but does not allow"
" you to e.g. use a specialized subclass of :class:`LogRecord`. Library "
"developers can set a suitable filter on their loggers, but they would have "
"to remember to do this every time they introduced a new logger (which they "
"would do simply by adding new packages or modules and doing ::"
msgstr""
"第二种方式在多数情况下效果都比较良好,但不允许你使用特殊化的 :class:`LogRecord` 子类。 "
"库开发者可以为他们的日志记录器设置合适的过滤器,但他们应当要记得每次引入新的日志记录器时都需如此(他们只需通过添加新的包或模块并执行以下操作即可)::"
#:../../howto/logging-cookbook.rst:1258
msgid""
"at module level). It's probably one too many things to think about. "
"Developers could also add the filter to a :class:`~logging.NullHandler` "
"attached to their top-level logger, but this would not be invoked if an "
"application developer attached a handler to a lower-level library logger ---"
" so output from that handler would not reflect the intentions of the library"
" developer."
msgstr""
"或许这样要顾及太多事情。开发人员还可以将过滤器附加到其顶级日志对象的 :class:`~logging.NullHandler` "
"中,但如果应用程序开发人员将 handler 附加到较底层库的日志对象,则不会调用该过滤器 --- 所以 handler "
"输出的内容不会符合库开发人员的预期。 "
#:../../howto/logging-cookbook.rst:1264
msgid""
"In Python 3.2 and later, :class:`~logging.LogRecord` creation is done "
"through a factory, which you can specify. The factory is just a callable you"
" can set with :func:`~logging.setLogRecordFactory`, and interrogate with "
":func:`~logging.getLogRecordFactory`. The factory is invoked with the same "
"signature as the :class:`~logging.LogRecord` constructor, as "
":class:`LogRecord` is the default setting for the factory."
msgstr""
"在 Python 3.2 以上版本中,:class:`~logging.LogRecord` "
"的创建是通过工厂对象完成的,工厂对象可以指定。工厂对象只是一个可调用对象,可以用 "
":func:`~logging.setLogRecordFactory` 进行设置,并用 "
":func:`~logging.getLogRecordFactory` 进行查询。工厂对象的调用参数与 "
":class:`~logging.LogRecord` 的构造函数相同,因为 :class:`LogRecord` 是工厂对象的默认设置。"
#:../../howto/logging-cookbook.rst:1271
msgid""
"This approach allows a custom factory to control all aspects of LogRecord "
"creation. For example, you could return a subclass, or just add some "
"additional attributes to the record once created, using a pattern similar to"
" this::"
msgstr""
"这种方式可以让自定义工厂对象完全控制 LogRecord 的创建过程。比如可以返回一个子类,或者在创建的日志对象中加入一些额外的属性,使用方式如下所示:"
#:../../howto/logging-cookbook.rst:1284
msgid""
"This pattern allows different libraries to chain factories together, and as "
"long as they don't overwrite each other's attributes or unintentionally "
"overwrite the attributes provided as standard, there should be no surprises."
" However, it should be borne in mind that each link in the chain adds run-"
"time overhead to all logging operations, and the technique should only be "
"used when the use of a :class:`Filter` does not provide the desired result."
msgstr""
"这种模式允许不同的库将多个工厂对象链在一起,只要不会覆盖彼此的属性或标准属性,就不会出现意外。但应记住,工厂链中的每个节点都会增加日志操作的运行开销,本技术仅在采用"
" :class:`Filter` 无法达到目标时才应使用。 "
#:../../howto/logging-cookbook.rst:1295
msgid"Subclassing QueueHandler - a ZeroMQ example"
msgstr"子类化 QueueHandler - ZeroMQ 示例 "
#:../../howto/logging-cookbook.rst:1297
msgid""
"You can use a :class:`QueueHandler` subclass to send messages to other kinds"
" of queues, for example a ZeroMQ 'publish' socket. In the example below,the "
"socket is created separately and passed to the handler (as its 'queue')::"
msgstr""
"你可以使用 :class:`QueueHandler` 子类将消息发送给其他类型的队列 ,比如 ZeroMQ 'publish' 套接字。 "
"在以下示例中,套接字将单独创建并传给处理句柄 (作为它的 'queue')::"
#:../../howto/logging-cookbook.rst:1316
msgid""
"Of course there are other ways of organizing this, for example passing in "
"the data needed by the handler to create the socket::"
msgstr"当然还有其他方案,比如通过 hander 传入所需数据,以创建 socket:"
#:../../howto/logging-cookbook.rst:1334
msgid"Subclassing QueueListener - a ZeroMQ example"
msgstr"子类化 QueueListener —— ZeroMQ 示例"
#:../../howto/logging-cookbook.rst:1336
msgid""
"You can also subclass :class:`QueueListener` to get messages from other "
"kinds of queues, for example a ZeroMQ 'subscribe' socket. Here's an "
"example::"
msgstr""
"你还可以子类化 :class:`QueueListener` 来从其他类型的队列中获取消息,比如从 ZeroMQ 'subscribe' 套接字。 "
"下面是一个例子::"
#:../../howto/logging-cookbook.rst:1355
msgid"Module :mod:`logging`"
msgstr"模块 :mod:`logging`"
#:../../howto/logging-cookbook.rst:1355
msgid"API reference for the logging module."
msgstr"日志记录模块的 API 参考。"
#:../../howto/logging-cookbook.rst:1358
msgid"Module :mod:`logging.config`"
msgstr"模块 :mod:`logging.config`"
#:../../howto/logging-cookbook.rst:1358
msgid"Configuration API for the logging module."
msgstr"日志记录模块的配置 API 。"
#:../../howto/logging-cookbook.rst:1361
msgid"Module :mod:`logging.handlers`"
msgstr"模块 :mod:`logging.handlers`"
#:../../howto/logging-cookbook.rst:1361
msgid"Useful handlers included with the logging module."
msgstr"日志记录模块附带的有用处理程序。"
#:../../howto/logging-cookbook.rst:1363
msgid":ref:`A basic logging tutorial <logging-basic-tutorial>`"
msgstr":ref:`日志操作基础教程 <logging-basic-tutorial>`"
#:../../howto/logging-cookbook.rst:1365
msgid":ref:`A more advanced logging tutorial <logging-advanced-tutorial>`"
msgstr":ref:`日志操作的高级教程 <logging-advanced-tutorial>`"
#:../../howto/logging-cookbook.rst:1369
msgid"An example dictionary-based configuration"
msgstr"基于字典进行日志配置的示例"
#:../../howto/logging-cookbook.rst:1371
msgid""
"Below is an example of a logging configuration dictionary - it's taken from "
"the `documentation on the Django project "
"<https://docs.djangoproject.com/en/1.9/topics/logging/#configuring-"
"logging>`_. This dictionary is passed to :func:`~config.dictConfig` to put "
"the configuration into effect::"
msgstr""
#:../../howto/logging-cookbook.rst:1427
msgid""
"For more information about this configuration, you can see the `relevant "
"section <https://docs.djangoproject.com/en/1.9/topics/logging/#configuring-"
"logging>`_ of the Django documentation."
msgstr""
#:../../howto/logging-cookbook.rst:1434
msgid"Using a rotator and namer to customize log rotation processing"
msgstr"利用 rotator 和 namer 自定义日志轮换操作"
#:../../howto/logging-cookbook.rst:1436
msgid""
"An example of how you can define a namer and rotator is given in the "
"following snippet, which shows zlib-based compression of the log file::"
msgstr"以下代码给出了定义 namer 和 rotator 的示例,其中演示了基于 zlib 的日志文件压缩过程:"
#:../../howto/logging-cookbook.rst:1454
msgid""
"These are not \"true\" .gz files, as they are bare compressed data, with no "
"\"container\" such as you’d find in an actual gzip file. This snippet is "
"just for illustration purposes."
msgstr"这些不是“真正的” .gz 文件,因为他们只是纯压缩数据,缺少真正 gzip 文件中的“容器”。此段代码只是用于演示。 "
#:../../howto/logging-cookbook.rst:1459
msgid"A more elaborate multiprocessing example"
msgstr"更加详细的多道处理示例"
#:../../howto/logging-cookbook.rst:1461
msgid""
"The following working example shows how logging can be used with "
"multiprocessing using configuration files. The configurations are fairly "
"simple, but serve to illustrate how more complex ones could be implemented "
"in a real multiprocessing scenario."
msgstr"以下可运行的示例显示了如何利用配置文件在多进程中应用日志。这些配置相当简单,但足以说明如何在真实的多进程场景中实现较为复杂的配置。 "
#:../../howto/logging-cookbook.rst:1466
msgid""
"In the example, the main process spawns a listener process and some worker "
"processes. Each of the main process, the listener and the workers have three"
" separate configurations (the workers all share the same configuration). We "
"can see logging in the main process, how the workers log to a QueueHandler "
"and how the listener implements a QueueListener and a more complex logging "
"configuration, and arranges to dispatch events received via the queue to the"
" handlers specified in the configuration. Note that these configurations are"
" purely illustrative, but you should be able to adapt this example to your "
"own scenario."
msgstr""
"上述示例中,主进程产生一个侦听器进程和一些工作进程。每个主进程、侦听器进程和工作进程都有三种独立的日志配置(工作进程共享同一套配置)。大家可以看到主进程的日志记录过程、工作线程向"
" QueueHandler 写入日志的过程,以及侦听器实现 QueueListener 和较为复杂的日志配置,如何将由队列接收到的事件分发给配置指定的 "
"handler。请注意,这些配置纯粹用于演示,但应该能调整代码以适用于自己的场景。"
#:../../howto/logging-cookbook.rst:1476
msgid""
"Here's the script - the docstrings and the comments hopefully explain how it"
" works::"
msgstr"以下是代码——但愿文档字符串和注释能有助于理解其工作原理:"