2016-08-13 64 views
-1

我使用scrapy 1.1.0,并且我在“蜘蛛”文件夹中有5个蜘蛛。scrapy使用python3日志记录模块问题

在每个蜘蛛中,我尝试使用python3日志模块。和代码结构是这样的:

import other modules 
import logging 
class ExampleSpider(scrapy.Spider): 
    name = 'special' 

    def __init__(self): 
     # other initializations 
     # set log 
     self.log = logging.getLogger('special') 
     self.log.setLevel(logging.DEBUG) 
     logFormatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s') 

     # file handler 
     fileHandler = logging.FileHandler(LOG_PATH) # LOG_PATH has defined 
     fileHandler.setLevel(logging.DEBUG) 
     fileHandler.setFormatter(logFormater) 
     self.log.addHandler(fileHandler) 

    # other functions 

每个蜘蛛我运行这些蜘蛛一样structure.When,我检查日志文件,他们确实存在,但它们的大小始终为0字节。

而另一个问题是,当我运行一个蜘蛛时,它总是生成两个或更多的日志文件。像我运行a蜘蛛,它会生成a.logb.log

任何答案将不胜感激。

+0

你用'self.log'登录什么?即'self.log.warning('something')'? scrapy有它自己的记录器,其他所有组件也是如此,你在这里做的是只为你的记录器设置文件处理程序等。 – Granitosaurus

+0

@Granitosaurus我只想将scrapy输出保存为一个日志文件,我知道现在scrapy(版本1.1.0)已经被弃用,并且支持显式调用Python标准日志记录。 –

回答

0

您可以设置通过LOG_FILE设置在settings.py或通过命令行参数--logfile FILE日志文件,即scrapy crawl myspider --logfile myspider.log

As described in the official docs

+0

好的,它的工作原理!谢谢。但是如果我想要它输出到标准输出并保存为一个文件,我该怎么办? –

+0

你可以使用基本的unix输出重定向:'scrapy crawl myspider 2>&1> spider.log',其中2>&1将stderr重定向到标准输出,或者如果你想看到输出以及使用'tee'和'scrapy crawl myspider 2 >&1 |发球区域spider.log' – Granitosaurus