一、根本办法
默认状况下日志打印只显示大于等于 WARNING 级别的日志
FATAL:致命谬误
CRITICAL:特地蹩脚的事件,如内存耗尽、磁盘空间为空,个别很少应用
ERROR:产生谬误时,如IO操作失败或者连贯问题
WARNING:产生很重要的事件,然而并不是谬误时,如用户登录明码谬误
INFO:解决申请或者状态变动等日常事务
DEBUG:调试过程中应用DEBUG等级,如算法中每个循环的中间状态

import logginglogging.debug('It is a debug')logging.info('It is a  info')logging.warning('It is a  warning')logging.error('It is a  Error')logging.critical('It is a  critical')

二、设置日志级别

import logginglogging.basicConfig(level=logging.DEBUG)logging.debug('Python debug')

三、将信息记录到文件

import logginglogging.basicConfig(filename='logging.text', level=logging.DEBUG)logging.debug('It is a debug')logging.info('It is a  info')logging.warning('It is a  warning')

四、更改音讯格局
%(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:打印日志信息

import logginglogging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')logging.warning('is when this event was logged.')

五、配置日志
创立日志记录配置文件并应用该 fileConfig() 性能读取它
logging.conf 配置文件:

[loggers]keys=root,simpleExample[handlers]keys=consoleHandler[formatters]keys=simpleFormatter[logger_root]level=DEBUGhandlers=consoleHandler[logger_simpleExample]level=DEBUGhandlers=consoleHandlerqualname=simpleExamplepropagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=

应用

import loggingimport logging.configlogging.config.fileConfig('logging.conf')logger = logging.getLogger('simpleExample')logging.debug('It is a debug')logging.info('It is a  info')logging.warning('It is a  warning')logging.error('It is a  Error')logging.critical('It is a  critical')

以上就是本次分享的全部内容,当初想要学习编程的小伙伴欢送关注Python技术大本营,获取更多技能与教程。