Logging to specific error log file in scrapy

后端 未结 1 1356
终归单人心
终归单人心 2021-02-06 04:48

I am running a log of scrapy by doing this:

from scrapy import log
class MySpider(BaseSpider):
  name = \"myspider\"  

  def __init__(self, name=None, **kwargs)         


        
1条回答
  •  后悔当初
    2021-02-06 04:57

    Just let logging do the job. Try to use PythonLoggingObserver instead of DefaultObserver:

    • configure two loggers (one for INFO and one for ERROR messages) directly in python, or via fileconfig, or via dictconfig (see docs)
    • start it in spider's __init__:

      def __init__(self, name=None, **kwargs):
          # TODO: configure logging: e.g. logging.config.fileConfig("logging.conf")
          observer = log.PythonLoggingObserver()
          observer.start()
      

    Let me know if you need help with configuring loggers.

    EDIT:

    Another option is to start two file log observers in __init__.py:

    from scrapy.log import ScrapyFileLogObserver
    from scrapy import log
    
    
    class MySpider(BaseSpider):
        name = "myspider"  
    
        def __init__(self, name=None, **kwargs):
            ScrapyFileLogObserver(open("spider.log", 'w'), level=logging.INFO).start()
            ScrapyFileLogObserver(open("spider_error.log", 'w'), level=logging.ERROR).start()
    
            super(MySpider, self).__init__(name, **kwargs)
    
        ...
    

    0 讨论(0)
提交回复
热议问题