如何在Python中logging模块?

而已。 如果你想logging一个函数或一个类,你可以在这个定义之后加一个string。 例如:

def foo(): """This function does nothing.""" pass 

但是模块呢? 我如何logging一个file.py的function?

对于这些包,你可以在__init__.pylogging它。 对于模块,您可以简单地在模块文件中添加文档string。

所有的信息在这里: http : //www.python.org/dev/peps/pep-0257/

添加您的文档string作为模块中的第一条语句 。

既然我喜欢看一个例子:

 """ Your module's verbose yet thorough docstring. """ import foo # ... 

你做同样的方式。 把一个string作为模块中的第一个语句。

很简单,只需在模块的顶部添加一个文档string即可。

下面是一个示例Google Style Python Docstrings ,了解如何logging模块。 基本上有一个关于模块的信息,如何执行它以及关于模块级variables和ToDo项目列表的信息。

 """Example Google style docstrings. This module demonstrates documentation as specified by the `Google Python Style Guide`_. Docstrings may extend over multiple lines. Sections are created with a section header and a colon followed by a block of indented text. Example: Examples can be given using either the ``Example`` or ``Examples`` sections. Sections support any reStructuredText formatting, including literal blocks:: $ python example_google.py Section breaks are created by resuming unindented text. Section breaks are also implicitly created anytime a new section starts. Attributes: module_level_variable1 (int): Module level variables may be documented in either the ``Attributes`` section of the module docstring, or in an inline docstring immediately following the variable. Either form is acceptable, but the two should not be mixed. Choose one convention to document module level variables and be consistent with it. Todo: * For module TODOs * You have to also use ``sphinx.ext.todo`` extension .. _Google Python Style Guide: http://google.github.io/styleguide/pyguide.html """ module_level_variable1 = 12345 def my_function(): pass ... ...