擅长:python、mysql、java
<p>这里有一个替代方法,它使用<a href="https://docs.python.org/2/howto/logging-cookbook.html#using-filters-to-impart-contextual-information" rel="nofollow">logging.Filter</a>来修改<code>record.msg</code>。通过修改消息而不是添加<code>%(prefix)s</code>字段,
格式不需要更改。在</p>
<p>{cd3>哪个伐木工人能更容易地使用它</p>
<p>要获取前缀,应使用调用<code>add_prefix_filter</code>初始化记录器:</p>
<pre><code>logger = UL.add_prefix_filter(logging.getLogger(__name__))
</code></pre>
<p>要将标签附加到前缀,函数应该像以前一样用<code>@log_prefix(label)</code>修饰。在</p>
<hr/>
<p>实用工具_日志记录.py公司名称:</p>
^{pr2}$
<p>在主.py公司名称:</p>
^{3}$
<p>在棒.py公司名称:</p>
<pre><code>import logging
import utils_logging as UL
logger = UL.add_prefix_filter(logging.getLogger(__name__))
@UL.log_prefix("GetConnection")
def get_connection(url):
logger.info("Making connection")
logger.info("Finished making connection")
</code></pre>
<p>收益率</p>
<pre><code>Starting...
Download:Downloading file..
Download:GetConnection:Making connection
Download:GetConnection:Finished making connection
GetConnection:Making connection
GetConnection:Finished making connection
</code></pre>