【发布时间】:2015-08-03 12:50:41
【问题描述】:
我是 Scrapy 的新手,我曾经设法在 Scrapy 0.24 上很好地运行我的脚本。但是当我切换到新推出的 1.0 时,我遇到了一个日志记录问题:我想要做的是将文件和控制台日志级别都设置为 INFO,但是我设置了 LOG_LEVEL 或 configure_logging() 函数(使用 Python内部日志包而不是scrapy.log),Scrapy总是将DEBUG级别信息记录到console,它以dict的格式返回整个项目对象。事实上,LOG_LEVEL 选项只适用于外部文件。我怀疑它一定与 Python 日志记录有关,但不知道如何设置它。任何人都可以帮助我吗?
这就是我在 run_my_spider.py 中配置日志记录的方式:
from crawler.settings import LOG_FILE, LOG_FORMAT
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
from scrapy.utils.log import configure_logging
from crawler.spiders.MySpiders import MySpider
import logging
def run_spider(spider):
settings = get_project_settings()
# configure file logging
# It ONLY works for the file
configure_logging({'LOG_FORMAT': LOG_FORMAT,
'LOG_ENABLEED' : True,
'LOG_FILE' : LOG_FILE,
'LOG_LEVEL' : 'INFO',
'LOG_STDOUT' : True})
# instantiate spider
process = CrawlerProcess(settings)
process.crawl(MySpider)
logging.info('Running Crawler: ' + spider.name)
process.start() # the script will block here until the spider_closed signal was sent
logging.info('Crawler ' + spider.name + ' stopped.\n')
......
这是控制台输出:
DEBUG:scrapy.core.engine:Crawled (200) <GET http://mil.news.sina.com.cn/2014-10-09/0450804543.html>(referer: http://rss.sina.com.cn/rollnews/jczs/20141009.js)
{'item_name': 'item_sina_news_reply',
'news_id': u'jc:27-1-804530',
'reply_id': u'jc:27-1-804530:1',
'reply_lastcrawl': '1438605374.41',
'reply_table': 'news_reply_20141009'}
非常感谢!
【问题讨论】:
标签: scrapy