【发布时间】:2015-12-25 13:41:41
【问题描述】:
显然,我不应该再使用 ScrapyFileLogObserver (http://doc.scrapy.org/en/1.0/topics/logging.html)。但我仍然希望能够将我的日志消息保存到文件中,并且我仍然希望将所有标准 Scrapy 控制台信息也保存到文件中。
通过阅读如何使用日志记录模块,这是我尝试使用的代码:
class BlahSpider(CrawlSpider):
name = 'blah'
allowed_domains = ['blah.com']
start_urls = ['https://www.blah.com/blahblahblah']
rules = (
Rule(SgmlLinkExtractor(allow=r'whatever'), callback='parse_item', follow=True),
)
def __init__(self):
CrawlSpider.__init__(self)
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
logging.basicConfig(filename='debug_log.txt', filemode='w', format='%(asctime)s %(levelname)s: %(message)s',
level=logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
simple_format = logging.Formatter('%(levelname)s: %(message)s')
console.setFormatter(simple_format)
self.logger.addHandler(console)
self.logger.info("Something")
def parse_item(self):
i = BlahItem()
return i
它运行良好,并将“某物”保存到文件中。但是,我在命令提示符窗口中看到的所有内容,所有在我使用 ScrapyFileLogObserver 时曾经保存到文件中的内容,现在都没有保存。
我认为我的带有“logging.StreamHandler()”的“控制台”处理程序应该可以处理这个问题,但这只是我读过的内容,我并不真正理解它是如何工作的。
谁能指出我遗漏了什么或哪里出错了?
谢谢。
【问题讨论】: