1.可以创建一个新的日志记录器
logger = getLogger(__name__)2.基础配置
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)3.开始记录
logger.debug('This message should go to the log file')
logger.info('So should this')
logger.warning('And this, too')
logger.error('And non-ASCII stuff, too, like Øresund and Malmö')
记录器对象上使用最广泛的方法分为两类:配置和消息发送。 Logger.setLevel() 指定记录器将处理的最低严重性日志消息,其中 debug 是最低内置严重性级别, critical 是最高内置严重性级别。 例如,如果严重性级别为 INFO ,则记录器将仅处理 INFO 、 WARNING 、 ERROR 和 CRITICAL 消息,并将忽略 DEBUG 消息。
记录变量数据
import logging
logging.warning('%s before you %s','Look','leap!')
更改显示消息的格式
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')
在消息中显示日期/时间 datefmt和time.strftime()是一样的格式:
import logging
logging.basicConfig(format='%(asctime)s %(message)s',datefmt="%m%d%Y %I%M%S %p:%f")
logging.warning('is when this event was logged.')