python 打印log 日志方法 请详细描述 且 举例说明使用方法

在python中,可以使用logging模块来打印日志。

使用方法:

(1)首先引入logging模块

import logging

(2)定义日志级别

logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')

可以使用logging.basicConfig()来设置日志的输出格式以及日志级别,level参数可以设置为:DEBUG,INFO,WARNING,ERROR,CRITICAL等,可以根据自己的需求来设置。

(3)输出日志

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

可以通过logging模块的5个不同的级别来输出日志,例如logging.debug(),logging.info(),logging.warning()等等,每个级别都有相应的输出方法。

例如:

import logging

logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

输出:

2020-07-09 17:17:57,822 - DEBUG - This is a debug message
2020-07-09 17:17:57,822 - INFO - This is an info message
2020-07-09 17:17:57,822 - WARNING - This is a warning message
2020-07-09 17:17:57,822 - ERROR - This is an error message
2020-07-09 17:17:57,822 - CRITICAL - This is a critical message

你可能感兴趣的:(开发语言,python)