木屑手

2024-04-25 08:58:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在以下两个问题中寻求您的帮助-如何为不同的日志级别(如python中的日志级别)设置处理程序。目前,我有

STATS_ENABLED = True
STATS_DUMP = True 

LOG_FILE = 'crawl.log'

但是由Scrapy生成的调试消息也被添加到日志文件中。这些消息非常长,理想情况下,我希望在标准错误上留下调试级别的消息,并将信息消息转储到我的LOG_FILE

其次,在文档中,它说The logging service must be explicitly started through the scrapy.log.start() function.我的问题是,我在哪里运行这个scrapy.log.start()?它在我的蜘蛛里面吗?


Tags: 文件logtrue消息处理程序statsenabled级别
3条回答

Secondly, in the docs, it says The logging service must be explicitly started through the scrapy.log.start() function. My question is, where do I run this scrapy.log.start()? Is it inside my spider?

如果您使用scrapy crawl my_spider运行一个spider——如果STATS_ENABLED = True日志将自动启动

如果手动启动爬网程序进程,则可以在启动爬网程序进程之前执行scrapy.log.start()

from scrapy.crawler import CrawlerProcess
from scrapy.conf import settings


settings.overrides.update({}) # your settings

crawlerProcess = CrawlerProcess(settings)
crawlerProcess.install()
crawlerProcess.configure()

crawlerProcess.crawl(spider) # your spider here

log.start() # depends on LOG_ENABLED

print "Starting crawler."
crawlerProcess.start()
print "Crawler stopped."

我对你的第一个问题所知甚少:

因为您必须手动启动scrapy日志,这允许您使用自己的记录器。

我认为您可以复制scrapy源中的模块scrapy/scrapy/log.py,修改它,导入它而不是scrapy.log,然后运行start()——scrapy将使用您的日志。函数start()中有一行写着log.startLoggingWithObserver(sflo.emit, setStdout=logstdout)

制作自己的观察者(http://docs.python.org/howto/logging-cookbook.html#logging-to-multiple-destinations)并在那里使用它。

I would like the DEBUG level messages to left on standard error and INFO messages to be dump to my LOG_FILE.

您可以在settings.py中设置^{},但它将完全禁用调试消息。

只是想更新我可以通过使用

from twisted.python import log
import logging
logging.basicConfig(level=logging.INFO, filemode='w', filename='log.txt'""")
observer = log.PythonLoggingObserver()
observer.start()

但是,我无法让日志显示蜘蛛的名称,就像标准错误中的from twisted那样。我贴了这个question

相关问题 更多 >