【问题标题】:why scrapy logs different in the console and external log file为什么scrapy在控制台和外部日志文件中的日志不同
【发布时间】: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


    【解决方案1】:

    您在控制台中查看的可能是 Twisted Logs。 它将调试级别消息打印到控制台。 您可以使用以下命令将它们重定向到您的日志文件:

    from twisted.python import log
    observer = log.PythonLoggingObserver(loggerName='logname')
    observer.start()
    

    (如How to make Twisted use Python logging?中给出的)

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2014-12-09
      相关资源
      最近更新 更多