[轉]scrapy中的logging


logging模塊是Python提供的自己的程序日志記錄模塊。

在大型軟件使用過程中,出現的錯誤有時候很難進行重現,因此需要通過分析日志來確認錯誤位置,這也是寫程序時要使用日志的最重要的原因。

scrapy使用python內置的logging模塊記錄日志


日志的級別

1. logging.CRITICAL - for critical errors (highest severity)

2. logging.ERROR - for regular errors

3. logging.WARNING - for warning messages

4. logging.INFO - for informational messages

5. logging.DEBUG - for debugging messages (lowest severity)


基本使用方法

1.簡單使用方法

import logging

Logging.warning(“this is a test ”)

執行結果:

Python1.png 

2.通用的記錄日志的方法,可加入日志的級別

import logging

Logging.log(logging.WARNING,”this is a warning”)

3,通過logger記錄日志

import logging

logger=logging.getLogger(_name_)

Logger.warning(“this is a warning”)


在scrapy中使用

Scrapy provides a logger within each Spider instance, that can be accessed and used like this:

import scrapy

class MySpider(scrapy.Spider):

name = 'myspider'

start_urls = ['http://scrapinghub.com']

def parse(self, response):

self.logger.info('Parse function called on %s', response.url)

That logger is created using the Spider’s name, but you can use any custom Python logger you want. For example:

import logging import scrapy

logger = logging.getLogger('mycustomlogger')

class MySpider(scrapy.Spider):

name = 'myspider'

start_urls = ['http://scrapinghub.com']

def parse(self, response):

logger.info('Parse function called on %s', response.url)


 

在settings.py中配置

These settings can be used to configure the logging:

 

• LOG_FILE 

• LOG_ENABLED

• LOG_ENCODING

• LOG_LEVEL 

• LOG_FORMAT 

• LOG_DATEFORMAT 

• LOG_STDOUT

轉載自:http://www.maiziedu.com/wiki/crawler/logging/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM