@@ -12,7 +12,7 @@ msgid ""
12
12
msgstr ""
13
13
"Project-Id-Version : Python 3.13\n "
14
14
"Report-Msgid-Bugs-To : \n "
15
- "POT-Creation-Date : 2025-05-30 14:58 +0000\n "
15
+ "POT-Creation-Date : 2025-07-11 15:02 +0000\n "
16
16
"PO-Revision-Date : 2025-05-08 05:09+0000\n "
17
17
"Last-Translator : Freesand Leo <yuqinju@163.com>, 2025\n "
18
18
"Language-Team : Chinese (China) (https://app.transifex.com/python-doc/teams/5390/zh_CN/)\n "
@@ -7788,11 +7788,132 @@ msgstr ""
7788
7788
"WARNING:demo: 1/0\n"
7789
7789
"WARNING:demo:ZeroDivisionError: division by zero"
7790
7790
7791
- #: ../../howto/logging-cookbook.rst:4072
7791
+ #: ../../howto/logging-cookbook.rst:4069
7792
+ msgid "How to uniformly handle newlines in logging output"
7793
+ msgstr "如何统一地处理日志记录输出中的换行符"
7794
+
7795
+ #: ../../howto/logging-cookbook.rst:4071
7796
+ msgid ""
7797
+ "Usually, messages that are logged (say to console or file) consist of a "
7798
+ "single line of text. However, sometimes there is a need to handle messages "
7799
+ "with multiple lines - whether because a logging format string contains "
7800
+ "newlines, or logged data contains newlines. If you want to handle such "
7801
+ "messages uniformly, so that each line in the logged message appears "
7802
+ "uniformly formatted as if it was logged separately, you can do this using a "
7803
+ "handler mixin, as in the following snippet:"
7804
+ msgstr ""
7805
+
7806
+ #: ../../howto/logging-cookbook.rst:4079
7807
+ msgid ""
7808
+ "# Assume this is in a module mymixins.py\n"
7809
+ "import copy\n"
7810
+ "\n"
7811
+ "class MultilineMixin:\n"
7812
+ " def emit(self, record):\n"
7813
+ " s = record.getMessage()\n"
7814
+ " if '\\ n' not in s:\n"
7815
+ " super().emit(record)\n"
7816
+ " else:\n"
7817
+ " lines = s.splitlines()\n"
7818
+ " rec = copy.copy(record)\n"
7819
+ " rec.args = None\n"
7820
+ " for line in lines:\n"
7821
+ " rec.msg = line\n"
7822
+ " super().emit(rec)"
7823
+ msgstr ""
7824
+
7825
+ #: ../../howto/logging-cookbook.rst:4097
7826
+ msgid "You can use the mixin as in the following script:"
7827
+ msgstr ""
7828
+
7829
+ #: ../../howto/logging-cookbook.rst:4099
7830
+ msgid ""
7831
+ "import logging\n"
7832
+ "\n"
7833
+ "from mymixins import MultilineMixin\n"
7834
+ "\n"
7835
+ "logger = logging.getLogger(__name__)\n"
7836
+ "\n"
7837
+ "class StreamHandler(MultilineMixin, logging.StreamHandler):\n"
7838
+ " pass\n"
7839
+ "\n"
7840
+ "if __name__ == '__main__':\n"
7841
+ " logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-9s %(message)s',\n"
7842
+ " handlers = [StreamHandler()])\n"
7843
+ " logger.debug('Single line')\n"
7844
+ " logger.debug('Multiple lines:\\ nfool me once ...')\n"
7845
+ " logger.debug('Another single line')\n"
7846
+ " logger.debug('Multiple lines:\\ n%s', 'fool me ...\\ ncan\\ 't get fooled again')"
7847
+ msgstr ""
7848
+
7849
+ #: ../../howto/logging-cookbook.rst:4118
7850
+ msgid "The script, when run, prints something like:"
7851
+ msgstr ""
7852
+
7853
+ #: ../../howto/logging-cookbook.rst:4120
7854
+ msgid ""
7855
+ "2025-07-02 13:54:47,234 DEBUG Single line\n"
7856
+ "2025-07-02 13:54:47,234 DEBUG Multiple lines:\n"
7857
+ "2025-07-02 13:54:47,234 DEBUG fool me once ...\n"
7858
+ "2025-07-02 13:54:47,234 DEBUG Another single line\n"
7859
+ "2025-07-02 13:54:47,234 DEBUG Multiple lines:\n"
7860
+ "2025-07-02 13:54:47,234 DEBUG fool me ...\n"
7861
+ "2025-07-02 13:54:47,234 DEBUG can't get fooled again"
7862
+ msgstr ""
7863
+
7864
+ #: ../../howto/logging-cookbook.rst:4130
7865
+ msgid ""
7866
+ "If, on the other hand, you are concerned about `log injection "
7867
+ "<https://owasp.org/www-community/attacks/Log_Injection>`_, you can use a "
7868
+ "formatter which escapes newlines, as per the following example:"
7869
+ msgstr ""
7870
+
7871
+ #: ../../howto/logging-cookbook.rst:4134
7872
+ msgid ""
7873
+ "import logging\n"
7874
+ "\n"
7875
+ "logger = logging.getLogger(__name__)\n"
7876
+ "\n"
7877
+ "class EscapingFormatter(logging.Formatter):\n"
7878
+ " def format(self, record):\n"
7879
+ " s = super().format(record)\n"
7880
+ " return s.replace('\\ n', r'\\ n')\n"
7881
+ "\n"
7882
+ "if __name__ == '__main__':\n"
7883
+ " h = logging.StreamHandler()\n"
7884
+ " h.setFormatter(EscapingFormatter('%(asctime)s %(levelname)-9s %(message)s'))\n"
7885
+ " logging.basicConfig(level=logging.DEBUG, handlers = [h])\n"
7886
+ " logger.debug('Single line')\n"
7887
+ " logger.debug('Multiple lines:\\ nfool me once ...')\n"
7888
+ " logger.debug('Another single line')\n"
7889
+ " logger.debug('Multiple lines:\\ n%s', 'fool me ...\\ ncan\\ 't get fooled again')"
7890
+ msgstr ""
7891
+
7892
+ #: ../../howto/logging-cookbook.rst:4154
7893
+ msgid ""
7894
+ "You can, of course, use whatever escaping scheme makes the most sense for "
7895
+ "you. The script, when run, should produce output like this:"
7896
+ msgstr ""
7897
+
7898
+ #: ../../howto/logging-cookbook.rst:4157
7899
+ msgid ""
7900
+ "2025-07-09 06:47:33,783 DEBUG Single line\n"
7901
+ "2025-07-09 06:47:33,783 DEBUG Multiple lines:\\ nfool me once ...\n"
7902
+ "2025-07-09 06:47:33,783 DEBUG Another single line\n"
7903
+ "2025-07-09 06:47:33,783 DEBUG Multiple lines:\\ nfool me ...\\ ncan't get fooled again"
7904
+ msgstr ""
7905
+
7906
+ #: ../../howto/logging-cookbook.rst:4164
7907
+ msgid ""
7908
+ "Escaping behaviour can't be the stdlib default , as it would break backwards"
7909
+ " compatibility."
7910
+ msgstr "转义行为不能是标准库默认设置,因为它会破坏向下兼容性。"
7911
+
7912
+ #: ../../howto/logging-cookbook.rst:4170
7792
7913
msgid "Patterns to avoid"
7793
7914
msgstr "理应避免的用法"
7794
7915
7795
- #: ../../howto/logging-cookbook.rst:4074
7916
+ #: ../../howto/logging-cookbook.rst:4172
7796
7917
msgid ""
7797
7918
"Although the preceding sections have described ways of doing things you "
7798
7919
"might need to do or deal with, it is worth mentioning some usage patterns "
@@ -7801,11 +7922,11 @@ msgid ""
7801
7922
msgstr ""
7802
7923
"前几节虽介绍了几种方案,描述了可能需要处理的操作,但值得一提的是,有些用法是 *没有好处* 的,大多数情况下应该避免使用。下面几节的顺序不分先后。"
7803
7924
7804
- #: ../../howto/logging-cookbook.rst:4080
7925
+ #: ../../howto/logging-cookbook.rst:4178
7805
7926
msgid "Opening the same log file multiple times"
7806
7927
msgstr "多次打开同一个日志文件"
7807
7928
7808
- #: ../../howto/logging-cookbook.rst:4082
7929
+ #: ../../howto/logging-cookbook.rst:4180
7809
7930
msgid ""
7810
7931
"On Windows, you will generally not be able to open the same file multiple "
7811
7932
"times as this will lead to a \" file is in use by another process\" error. "
@@ -7815,33 +7936,33 @@ msgstr ""
7815
7936
"因会导致 \" 文件被其他进程占用 \" 错误,所以在 Windows 中一般无法多次打开同一个文件。但在 POSIX "
7816
7937
"平台中,多次打开同一个文件不会报任何错误。这种操作可能是意外发生的,比如:"
7817
7938
7818
- #: ../../howto/logging-cookbook.rst:4087
7939
+ #: ../../howto/logging-cookbook.rst:4185
7819
7940
msgid ""
7820
7941
"Adding a file handler more than once which references the same file (e.g. by"
7821
7942
" a copy/paste/forget-to-change error)."
7822
7943
msgstr "多次添加指向同一文件的 handler(比如通过复制/粘贴,或忘记修改)。"
7823
7944
7824
- #: ../../howto/logging-cookbook.rst:4090
7945
+ #: ../../howto/logging-cookbook.rst:4188
7825
7946
msgid ""
7826
7947
"Opening two files that look different, as they have different names, but are"
7827
7948
" the same because one is a symbolic link to the other."
7828
7949
msgstr "打开两个貌似不同(文件名不一样)的文件,但一个是另一个的符号链接,所以其实是同一个文件。"
7829
7950
7830
- #: ../../howto/logging-cookbook.rst:4093
7951
+ #: ../../howto/logging-cookbook.rst:4191
7831
7952
msgid ""
7832
7953
"Forking a process, following which both parent and child have a reference to"
7833
7954
" the same file. This might be through use of the :mod:`multiprocessing` "
7834
7955
"module, for example."
7835
7956
msgstr ""
7836
7957
"进程 fork,然后父进程和子进程都有对同一文件的引用。 例如,这可能是通过使用 :mod:`multiprocessing` 模块实现的。"
7837
7958
7838
- #: ../../howto/logging-cookbook.rst:4097
7959
+ #: ../../howto/logging-cookbook.rst:4195
7839
7960
msgid ""
7840
7961
"Opening a file multiple times might *appear* to work most of the time, but "
7841
7962
"can lead to a number of problems in practice:"
7842
7963
msgstr "在大多数情况下,多次打开同一个文件 *貌似* 一切正常,但实际会导致很多问题。"
7843
7964
7844
- #: ../../howto/logging-cookbook.rst:4100
7965
+ #: ../../howto/logging-cookbook.rst:4198
7845
7966
msgid ""
7846
7967
"Logging output can be garbled because multiple threads or processes try to "
7847
7968
"write to the same file. Although logging guards against concurrent use of "
@@ -7852,7 +7973,7 @@ msgstr ""
7852
7973
"由于多个线程或进程会尝试写入同一个文件,日志输出可能会出现乱码。尽管日志对象可以防止多个线程同时使用同一个 handler "
7853
7974
"实例,但如果两个不同的线程使用不同的 handler 实例同时写入文件,而这两个 handler 又恰好指向同一个文件,那么就失去了这种防护。"
7854
7975
7855
- #: ../../howto/logging-cookbook.rst:4106
7976
+ #: ../../howto/logging-cookbook.rst:4204
7856
7977
msgid ""
7857
7978
"An attempt to delete a file (e.g. during file rotation) silently fails, "
7858
7979
"because there is another reference pointing to it. This can lead to "
@@ -7865,17 +7986,17 @@ msgstr ""
7865
7986
"日志条目会出现在意想不到的地方,或者完全丢失。 "
7866
7987
"或者会有应当移除的文件仍然保持存在,文件还会在已经设置了基于文件大小的轮换的情况下仍然增长到预料之外的大小。"
7867
7988
7868
- #: ../../howto/logging-cookbook.rst:4113
7989
+ #: ../../howto/logging-cookbook.rst:4211
7869
7990
msgid ""
7870
7991
"Use the techniques outlined in :ref:`multiple-processes` to circumvent such "
7871
7992
"issues."
7872
7993
msgstr "请用 :ref:`multiple-processes` 中介绍的技术来避免上述问题。"
7873
7994
7874
- #: ../../howto/logging-cookbook.rst:4117
7995
+ #: ../../howto/logging-cookbook.rst:4215
7875
7996
msgid "Using loggers as attributes in a class or passing them as parameters"
7876
7997
msgstr "将日志对象用作属性或传递参数"
7877
7998
7878
- #: ../../howto/logging-cookbook.rst:4119
7999
+ #: ../../howto/logging-cookbook.rst:4217
7879
8000
msgid ""
7880
8001
"While there might be unusual cases where you'll need to do this, in general "
7881
8002
"there is no point because loggers are singletons. Code can always access a "
@@ -7889,13 +8010,13 @@ msgstr ""
7889
8010
"用名称访问一个已有的日志对象实例,因此将实例作为参数来传递,或作为属性留存,都是毫无意义的。请注意,在其他语言中,如 Java 和 "
7890
8011
"C#,日志对象通常是静态类属性。但在 Python 中是没有意义的,因为软件拆分的单位是模块(而不是类)。"
7891
8012
7892
- #: ../../howto/logging-cookbook.rst:4128
8013
+ #: ../../howto/logging-cookbook.rst:4226
7893
8014
msgid ""
7894
8015
"Adding handlers other than :class:`~logging.NullHandler` to a logger in a "
7895
8016
"library"
7896
8017
msgstr "为库中的日志记录器添加 :class:`~logging.NullHandler` 以外的处理器"
7897
8018
7898
- #: ../../howto/logging-cookbook.rst:4130
8019
+ #: ../../howto/logging-cookbook.rst:4228
7899
8020
msgid ""
7900
8021
"Configuring logging by adding handlers, formatters and filters is the "
7901
8022
"responsibility of the application developer, not the library developer. If "
@@ -7906,11 +8027,11 @@ msgstr ""
7906
8027
"来配置日志,这是应用程序开发人员的责任,而不是库开发人员该做的。如果正在维护一个库,请确保不要向任何日志对象添加 "
7907
8028
":class:`~logging.NullHandler` 实例以外的 handler。"
7908
8029
7909
- #: ../../howto/logging-cookbook.rst:4136
8030
+ #: ../../howto/logging-cookbook.rst:4234
7910
8031
msgid "Creating a lot of loggers"
7911
8032
msgstr "创建大量的日志对象"
7912
8033
7913
- #: ../../howto/logging-cookbook.rst:4138
8034
+ #: ../../howto/logging-cookbook.rst:4236
7914
8035
msgid ""
7915
8036
"Loggers are singletons that are never freed during a script execution, and "
7916
8037
"so creating lots of loggers will use up memory which can't then be freed. "
@@ -7924,38 +8045,38 @@ msgstr ""
7924
8045
" :ref:`已有机制 <context-info>` "
7925
8046
"将上下文信息传给自定义日志对象,并将创建的日志对象限制在应用程序内的指定区域(通常是模块,但偶尔会再精细些)使用。"
7926
8047
7927
- #: ../../howto/logging-cookbook.rst:4149
8048
+ #: ../../howto/logging-cookbook.rst:4247
7928
8049
msgid "Other resources"
7929
8050
msgstr "其他资源"
7930
8051
7931
- #: ../../howto/logging-cookbook.rst:4153
8052
+ #: ../../howto/logging-cookbook.rst:4251
7932
8053
msgid "Module :mod:`logging`"
7933
8054
msgstr "模块 :mod:`logging`"
7934
8055
7935
- #: ../../howto/logging-cookbook.rst:4154
8056
+ #: ../../howto/logging-cookbook.rst:4252
7936
8057
msgid "API reference for the logging module."
7937
8058
msgstr "日志记录模块的 API 参考。"
7938
8059
7939
- #: ../../howto/logging-cookbook.rst:4156
8060
+ #: ../../howto/logging-cookbook.rst:4254
7940
8061
msgid "Module :mod:`logging.config`"
7941
8062
msgstr ":mod:`logging.config` 模块"
7942
8063
7943
- #: ../../howto/logging-cookbook.rst:4157
8064
+ #: ../../howto/logging-cookbook.rst:4255
7944
8065
msgid "Configuration API for the logging module."
7945
8066
msgstr "日志记录模块的配置 API 。"
7946
8067
7947
- #: ../../howto/logging-cookbook.rst:4159
8068
+ #: ../../howto/logging-cookbook.rst:4257
7948
8069
msgid "Module :mod:`logging.handlers`"
7949
8070
msgstr ":mod:`logging.handlers` 模块"
7950
8071
7951
- #: ../../howto/logging-cookbook.rst:4160
8072
+ #: ../../howto/logging-cookbook.rst:4258
7952
8073
msgid "Useful handlers included with the logging module."
7953
8074
msgstr "日志记录模块附带的有用处理器。"
7954
8075
7955
- #: ../../howto/logging-cookbook.rst:4162
8076
+ #: ../../howto/logging-cookbook.rst:4260
7956
8077
msgid ":ref:`Basic Tutorial <logging-basic-tutorial>`"
7957
8078
msgstr ":ref:`基础教程 <logging-basic-tutorial>`"
7958
8079
7959
- #: ../../howto/logging-cookbook.rst:4164
8080
+ #: ../../howto/logging-cookbook.rst:4262
7960
8081
msgid ":ref:`Advanced Tutorial <logging-advanced-tutorial>`"
7961
8082
msgstr ":ref:`进阶教程 <logging-advanced-tutorial>`"
0 commit comments