我有parent.py和child.py脚本(许多child),并且我需要为每个脚本都创建日志,因此parent.py中的任何日志都应该在parent.log中,child.py应该在child.log中
我有下面的每个脚本,但我得到空日志。。。为什么
#main.py
import child
handler = logging.FileHandler('logs/main.log')
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %
(funcName)10s()] %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
child.child_func()
logger.info('testing parent...')
#child.py
handler = logging.FileHandler('logs/child.log')
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %
(funcName)10s()] %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
def child_func():
logger.info('testing child...')
我需要的是
#parent.log
{format} testing parent...
#child.log
{format} testing child...
上面的人对记录器的默认级别是正确的。另外,我发现在应用程序的早期整合日志记录配置更容易管理,而不是到处散布配置。请参见下面的示例
注意:我不希望这个被选为答案。我只是想指出我认为组织代码的更好方法
main.py
child.py
设置根记录器和子记录器(无主记录器)
下面是一个设置根记录器到
logs/main.log
,子记录器到logs/child.log
的示例您可以在处理程序和记录器上设置严重性级别—我相信记录器在默认情况下设置为
logging.WARNING
,因此您只能使用代码获取警告日志您可以在以下线程中阅读更多内容:What is the point of setLevel in a python logging handler?
相关问题 更多 >
编程相关推荐