擅长:python、mysql、java
<p>是的,@ekhuroo是对的。<strong>似乎<code>logging</code>需要绝对路径。</strong>Python团队应该将此错误消息更改为更可读的内容;它只是看不到我的文件,而不是因为配置文件本身是错误的。</p>
<p>我通过在配置文件中定义一个<code>BASE_DIR</code>变量并将其作为路径前缀导入来解决这个问题,就像Django所做的那样。而且,如果没有创建日志文件的父目录,请记住创建它们。</p>
<p>我在<code>config.py</code>中定义路径和记录器:</p>
<pre><code>import os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
import logging
import logging.config
logging.config.fileConfig(os.path.join(BASE_DIR, 'utils', 'logger.conf')) # the `logger.conf` locates under 'myproject/utils/'
logger = logging.getLogger("mylog") # 'mylog' should exist in `logger.conf` in the logger part
</code></pre>
<p>在其他模块中:</p>
<pre><code>from config import logger
...
logger.info("Your loggings modules should work now!! - WesternGun")
</code></pre>