logging的使用方法

logging的使用方法

1,简单使用方法

>>> import logging
>>> logging.warning('this is a warning')
WARNING:root:this is a warning 2,通用的记录日志的方法,可加入日志的级别
>>> import logging
>>> logging.log(logging.WARNING,"this is a warning")
WARNING:root:this is a warning 3,通过logger记录日志
>>> import logging
>>> logger = logging.getLogger(__name__)
>>> logger.warning("this is a warning")
WARNING:__main__:this is a warning 在scrapy中的使用 情景一:
import scrapy class MySpider(scrapy.Spider): name = 'myspider'
start_urls = ['http://scrapinghub.com'] def parse(self, response):
# 在scrapy.Spider中默认就有logger,所以下面直接引用
self.logger.info('Parse function called on %s', response.url) 情景二:
import logging
import scrapy
#在这里设置logger的名称
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) logging级别
logging.CRITICAL - for critical errors (highest severity)
logging.ERROR - for regular errors
logging.WARNING - for warning messages
logging.INFO - for informational messages
logging.DEBUG - for debugging messages (lowest severity)
上一篇:【Java并发编程实战】-----“J.U.C”:CountDownlatch


下一篇:ElasticSearch本人启动报错总结