The scrapy python logging module logs

How to print information scrapy the output of each module to the same log file?

1, the knowledge points

"""
logging :
    scrapy:
        settings set LOG_LEVEL = "WARNING"
        settings provided LOG_FILE = log location settings saved # "./ log.log", after setting the terminal does not display the contents of the log
        import logging instantiate a logger logger manner using any file outputting content
                logger = logging.getLogger (__ name__) # instantiated
    Common project:
        import logging
        logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='myapp.log',
                filemode = 'w') # Set the log output format
        Examples of a ogger = logging.getLogger (__ name__)
        Call logger can be in any py file
"""

2, scrapy use the logging project

# -*- coding: utf-8 -*-
import scrapy
import  logging

logger = logging.getLogger(__name__)
class JulyeduSpider(scrapy.Spider):
    name = 'julyedu'
    allowed_domains = ['julyedu.com']
    start_urls = [ ' http://julyedu.com/ ' ]
     # The name of the parse method can be changed 
    DEF parse (Self, Response):
         "" "
        July reptile online mentor list
        :param response:
        :return:
        """
        list_li = response.xpath("//div[@class='swiper-wrapper']//li")
        #print(list_li)
        item = {}
        for li in list_li:
            item["name"] = li.xpath(".//h3/text()").extract_first()
            Item [ " Content " ] = li.xpath ( " .// P [@ class = 'teacherBrief'] / text () " ) .extract_first ()
             # Item [ "Content"] = li.xpath ( ".// P [@ class = 'teacherIntroduction'] / text () "). extract_first () 
            # Print (Item) 
            # transfer data track pipelines, yield only accept Request, BaseItem, dict, None four types 
            logger.warning (item) # Print log 
            yield   Item

2, the general project

  a) establish a common log_a.py

# coding = utf-8
import  logging
logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='myapp.log',
                fileMode = ' a ' )

logger = logging.getLogger(__name__)

if __name__ == '__main__':
    logger.info("this is a log ")

b) log_b.py files using common log_a.py

# coding = utf-8
from log_a import logger

if __name__ == '__main__':
    logger.warning ( " b Files " )

From: https: //www.cnblogs.com/ywjfx/p/11079621.html

Guess you like

Origin www.cnblogs.com/yoyowin/p/12162585.html