【问题标题】:Is there any way to change the log message format in scrapy?有什么方法可以更改scrapy中的日志消息格式?
【发布时间】:2015-03-03 12:06:56
【问题描述】:

我想修改scrapy日志消息以在其开头包含用户ID。例如,而不是这个

2015-03-03 17:09:34+0530 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware

有没有可能让它看起来像

**user_id**:2015-03-03 17:09:34+0530 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware

另外,有没有办法只记录用户消息;即,只记录我想记录的消息,我将从代码中提供?

任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: python-2.7 web-scraping scrapy scraper twisted.internet


    【解决方案1】:

    Scrapy 使用构建在日志标准库之上的 Twisted 日志系统。我没有答案,如何更改scrapy本身生成的日志的日期格式。

    对于你的第二个问题,我可能有答案。我认为你应该通过设置来完全关闭scrapy日志记录

    LOG_ENABLED = False
    

    在你的 settings.py 中。然后,为自己配置日志模块(在蜘蛛模块之上)

    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)8s\t%(message)s', )
    

    然后使用logging.loglogging.infologging.debug 等登录。

    我留给你找出所需的日志记录格式。

    【讨论】:

      【解决方案2】:

      在settings.py中添加

      LOG_FORMAT = user_id +' %(asctime)s %(levelname)8s\t%(message)s'
      

      更多信息https://doc.scrapy.org/en/latest/topics/logging.html#logging-settings

      【讨论】:

        猜你喜欢
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-23
        • 2015-02-03
        • 1970-01-01
        • 2013-01-28
        相关资源
        最近更新 更多