且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

临时记录到特定的错误日志文件

更新时间:2022-11-26 10:05:09

只需记录做好这项工作.尝试使用PythonLoggingObserver代替DefaultObserver:

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

  • 直接在python中或通过fileconfig或dictconfig配置两个记录器(一个用于INFO消息,一个用于ERROR消息)(请参阅
  • 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.

另一种选择是在__init__.py中启动两个文件日志观察器:

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)

    ...