我通过这样做来运行scrapy的日志:
from scrapy import log
class MySpider(BaseSpider):
name = "myspider"
def __init__(self, name=None, **kwargs):
LOG_FILE = "logs/spider.log"
log.log.defaultObserver = log.log.DefaultObserver()
log.log.defaultObserver.start()
log.started = False
log.start(LOG_FILE, loglevel=log.INFO)
super(MySpider, self).__init__(name, **kwargs)
def parse(self,response):
....
raise Exception("Something went wrong!")
log.msg('Something went wrong!', log.ERROR)
# Somehow write to a separate error log here.
然后我像这样运行蜘蛛:
scrapy crawl myspider
这会将所有 log.INFO 数据以及 log.ERROR 存储到spider.log
.
如果发生错误,我还想将这些详细信息存储在一个名为spider_errors.log
. 这将更容易搜索发生的错误,而不是尝试扫描整个spider.log
文件(可能很大)。
有没有办法做到这一点?
编辑:
尝试使用 PythonLoggingObserver:
def __init__(self, name=None, **kwargs):
LOG_FILE = 'logs/spider.log'
ERR_File = 'logs/spider_error.log'
observer = log.log.PythonLoggingObserver()
observer.start()
log.started = False
log.start(LOG_FILE, loglevel=log.INFO)
log.start(ERR_FILE, loglevel=log.ERROR)
但我明白了ERROR: No handlers could be found for logger "twisted"