1
我有一個蜘蛛類從BaseSpider
派生。我打電話self.log
,但沒有寫入日誌文件。我在命令行LOG_FILE
和LOG_LEVEL
上配置日誌文件,但spider日誌輸出未寫入該文件。如何將蜘蛛日誌寫入正常的日誌文件?scrapy蜘蛛日誌不寫入日誌文件
我有一個蜘蛛類從BaseSpider
派生。我打電話self.log
,但沒有寫入日誌文件。我在命令行LOG_FILE
和LOG_LEVEL
上配置日誌文件,但spider日誌輸出未寫入該文件。如何將蜘蛛日誌寫入正常的日誌文件?scrapy蜘蛛日誌不寫入日誌文件
你確定你的回調獲取調用?
由於在文件中example.py
有這個簡單的蜘蛛:
from scrapy.spider import BaseSpider
class ExampleSpider(BaseSpider):
name = "example"
start_urls = ['http://www.example.com/']
def parse(self, response):
self.log('************* my log ***********')
而且隨着scrapy runspider example.py --set LOG_FILE=logfile
運行它,這是文件內容:
2013-09-30 22:55:12-0400 [scrapy] INFO: Scrapy 0.16.5 started (bot: mybot)
2013-09-30 22:55:12-0400 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2013-09-30 21:55:12-0500 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-09-30 21:55:12-0500 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-09-30 21:55:12-0500 [scrapy] DEBUG: Enabled item pipelines: MybotPipeline
2013-09-30 21:55:12-0500 [example] INFO: Spider opened
2013-09-30 21:55:12-0500 [example] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-09-30 21:55:12-0500 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-09-30 21:55:12-0500 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-09-30 21:55:13-0500 [example] DEBUG: Crawled (200) <GET http://www.example.com/> (referer: None)
2013-09-30 21:55:13-0500 [example] DEBUG: ************* my log ***********
2013-09-30 21:55:13-0500 [example] INFO: Closing spider (finished)
2013-09-30 21:55:13-0500 [example] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 221,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 1611,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2013, 10, 1, 2, 55, 13, 315807),
'log_count/DEBUG': 8,
'log_count/INFO': 4,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2013, 10, 1, 2, 55, 12, 991150)}
2013-09-30 21:55:13-0500 [example] INFO: Spider closed (finished)
嘗試添加失敗而回調,以確保被召喚。簡單的像raise Exception
。如果在運行時沒有發生異常,那麼您的回調沒有被調用。
我敢肯定我的回調被調用(正如我得到的結果和報表打印輸出到stdout)。然而,'self.log'調用都不會寫入日誌文件。 –
默認情況下,日誌顯示爲DEBUG級別,這就是爲什麼我沒有看到輸出(我只寫INFO +級別)。 –