I have been working on a Scrapy project and so far everything works quite well. However, I'm not satisfied with Scrapy's logging configuration possibilities. At the moment, I have set LOG_FILE = 'my_spider.log'
in the settings.py
of my project. When I execute scrapy crawl my_spider
on the command line, it creates one big log file for the entire crawling process. This is not feasible for my purposes.
How can I use Python's custom log handlers in combination with the scrapy.log
module? Especially, I want to make use of Python's logging.handlers.RotatingFileHandler
so that I can split the log data into several small files instead of having to deal with one huge file. The documentation of Scrapy's logging facility is not very extensive, unfortunately. Many thanks in advance!
Scrapy uses the standard python loggers, which means you can grab and modify them as you create your spider.
you can log all scrapy logs to file by first disabling root handle in scrapy.utils.log.configure_logging and then adding your own log handler.
In settings.py file of scrapy project add the following code:
Also we customize log level (DEBUG to INFO) and formatter as required. To add custom logs inside you spider, pipeline we can easily do it like a normal python logging as follows:
Inside pipelines.py
Hope this helps!
You could integrate a custom log file like so (i'm not sure how to integrate the rotator):
In your spider class file:
And the output file might look like:
scrapy_example_2012-08-25 12:34:48.823896.log