setting文件中LOG_LEVEL和LOG_FILE参数

在scrapy运行的时候就不会出现WARNGIN以下的信息

如果设置了这个参数,程序就没有任何现象,所有日志都保存到文件中了,不在console中显示
logging使用
在爬虫文件中的parse方法中可以如下使用:
1 2 3 4 5 |
def parse(self, response): item = {} for i in range(5): item["test"] = i logging.warning(item["test"]) |

直接使用logging模块有一个问题就是日志生成位置都是一致的,这是scrapy默认的,可以使用logging.getLogger方法生成日志对象去修改
1 2 3 4 5 6 7 8 9 10 11 12 |
logger = logging.getLogger(__name__) class ExamSpider(scrapy.Spider): name = 'exam' allowed_domains = ['baidu.com'] start_urls = ['http://baidu.com/'] def parse(self, response): item = {} for i in range(5): item["test"] = i logger.warning(item["test"]) |

编写一个log文件直接调用:
1 2 3 4 5 6 7 8 |
import logging logging.basicConfig(level=logging.INFO,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') logger = logging.getLogger(__name__) if __name__ == '__main__': logger.info("11111111") logger.info("22222222") |

这样直接import文件中的logger就能直接生日日志,不用每次都去初始化
logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,’w’或’a’
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
logging打印信息函数:
logging.debug(‘This is debug message’)
logging.info(‘This is info message’)
logging.warning(‘This is warning message’)