python日志调试

1.日志

  • logging.debug():最低级别,用于小细节,通常用于在诊断问题时,才会关心谢谢消息
  • logging.info():用于记录程序中一般事件的信息,或确认一切工作正常
  • logging.warning():用于表示可能的问题,它不会阻止程序的工作,但将来可能会
  • logging.error():用于记录错误,它导致程序做某事失败
  • logging.critical():最高级别,用于表示致命的错误,它导致或将导致程序完全停止工作。

示例

import logging

#filename指定日志存放文件名称
#level指定日志级别
logging.basicConfig(filename='test.txt',level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program')
def factorial(n):
    logging.debug('Start of factorial(%s%%)' % (n))
    total = 1
    for i in range(1,n + 1):
        total *= i
        logging.debug('i is ' + str(i) + ', total is ' + str(total))
    logging.debug('End of factorial(%s%%)' % (n))
    return total

print(factorial(7))
logging.debug('End of program')


================================================
执行结果如下

 2023-07-17 17:45:27,486 - DEBUG - Start of program
 2023-07-17 17:45:27,486 - DEBUG - Start of factorial(7%)
 2023-07-17 17:45:27,486 - DEBUG - i is 1, total is 1
 2023-07-17 17:45:27,486 - DEBUG - i is 2, total is 2
 2023-07-17 17:45:27,486 - DEBUG - i is 3, total is 6
 2023-07-17 17:45:27,486 - DEBUG - i is 4, total is 24
 2023-07-17 17:45:27,486 - DEBUG - i is 5, total is 120
 2023-07-17 17:45:27,486 - DEBUG - i is 6, total is 720
 2023-07-17 17:45:27,487 - DEBUG - i is 7, total is 5040
 2023-07-17 17:45:27,487 - DEBUG - End of factorial(7%)
 2023-07-17 17:45:27,487 - DEBUG - End of program

 

posted @ 2023-07-17 17:46  摩天居士-谢烟客  阅读(21)  评论(0编辑  收藏  举报