Logging in Scrapy

后端 未结 3 1131
半阙折子戏
半阙折子戏 2021-01-02 07:53

I am having trouble with logging in scrapy, and most of what I can find is out of date.

I have set LOG_FILE=\"log.txt\" in the settings.py

3条回答
  •  旧时难觅i
    2021-01-02 07:55

    For logging I just put this on the spider class:

    import logging
    from scrapy.utils.log import configure_logging 
    
    
    class SomeSpider(scrapy.Spider):
        configure_logging(install_root_handler=False)
        logging.basicConfig(
            filename='log.txt',
            format='%(levelname)s: %(message)s',
            level=logging.INFO
        )
    

    This will put all scrapy output into the project root directory as a log.txt file

    If you want to log something manually you shouldn't use the scrapy logger, it's deprecated. Just use the python one

    import logging
    logging.error("Some error")
    

提交回复
热议问题