1. 日志模块 logging
要启用 logging 模块,在程序运行时将日志信息显示在屏幕上,请将下面的代码复制到程序顶部(但在 Python 的#!行之下):
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
logging.error('log add here')
logging.debug('log add here')
logging.info('log add here')
logging.warning('log add here')
实例如下:
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
logging.error('Start of program')
def factorial(n):
logging.debug('Start of factorial(%s%%)' % (n))
total = 1
for i in range(n + 1):
total *= i+1
logging.debug('i is ' + str(i) + ', total is ' + str(total))
logging.info('End of factorial(%s%%)' % (n))
return total
print(factorial(5))
logging.warning('End of program')
输出结果为:
PS C:\Users\Administrator\Desktop\tmp> python .\Untitled-1.py
2019-03-09 14:16:48,767 - ERROR- Start of program
2019-03-09 14:16:48,767 - DEBUG- Start of factorial(5%)
2019-03-09 14:16:48,767 - DEBUG- i is 0, total is 1
2019-03-09 14:16:48,768 - INFO- End of factorial(5%)
2019-03-09 14:16:48,768 - DEBUG- i is 1, total is 2
2019-03-09 14:16:48,768 - INFO- End of factorial(5%)
2019-03-09 14:16:48,768 - DEBUG- i is 2, total is 6
2019-03-09 14:16:48,769 - INFO- End of factorial(5%)
2019-03-09 14:16:48,769 - DEBUG- i is 3, total is 24
2019-03-09 14:16:48,769 - INFO- End of factorial(5%)
2019-03-09 14:16:48,769 - DEBUG- i is 4, total is 120
2019-03-09 14:16:48,770 - INFO- End of factorial(5%)
2019-03-09 14:16:48,770 - DEBUG- i is 5, total is 720
2019-03-09 14:16:48,770 - INFO- End of factorial(5%)
720
2019-03-09 14:16:48,770 - WARNING- End of program
PS C:\Users\Administrator\Desktop\tmp>
2. 不要用 print()调试
输入 import logging 和 logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s - %(levelname)s - %(message)s’)有一点不方便。
你可能想使用 print() 调用代替,但不要屈服于这种诱惑!在调试完成后,你需要花很多时间,从代码中清除每条日志消息的 print() 调用。
你甚至有可能不小心删除一些 print() 调用,而它们不是用来产生日志消息的。
日志消息的好处在于,你可以随心所欲地在程序中想加多少就加多少,
稍后只要加入一次 logging.disable(logging.CRITICAL)调用,就可以禁止日志。
动态关闭log 打印实例:
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
logging.warning('Start of program')
def factorial(n):
logging.debug('Start of factorial(%s%%)' % (n))
total = 1
for i in range(n + 1):
total *= i+1
logging.debug('i is ' + str(i) + ', total is ' + str(total))
logging.info('End of factorial(%s%%)' % (n))
### 当i 等于1 的时候,关闭 Info 以下的log
if(i == 1):
print('\n')
logging.debug('Start to disable logging Info and below, Error also could print')
logging.disable(logging.INFO)
logging.error('==========================================')
### 当i 等于3 的时候,关闭 Debug 以下的log
if(i == 3):
print('\n')
logging.error('Start to disable logging Debug and below, so info log could be print')
logging.disable(logging.DEBUG)
logging.info('==========================================')
return total
print(factorial(5))
logging.error('End of program')
输出结果为:
PS C:\Users\Administrator\Desktop\tmp> python .\Untitled-1.py
2019-03-09 14:30:11,285 - WARNING- Start of program
2019-03-09 14:30:11,285 - DEBUG- Start of factorial(5%)
2019-03-09 14:30:11,286 - DEBUG- i is 0, total is 1
2019-03-09 14:30:11,286 - INFO- End of factorial(5%)
2019-03-09 14:30:11,286 - DEBUG- i is 1, total is 2
2019-03-09 14:30:11,286 - INFO- End of factorial(5%)
2019-03-09 14:30:11,287 - DEBUG- Start to disable logging Info and below, Error also could print
2019-03-09 14:30:11,287 - ERROR- ==========================================
2019-03-09 14:30:11,288 - ERROR- Start to disable logging Debug and below, so info log could be print
2019-03-09 14:30:11,288 - INFO- ==========================================
2019-03-09 14:30:11,288 - INFO- End of factorial(5%)
2019-03-09 14:30:11,288 - INFO- End of factorial(5%)
720
2019-03-09 14:30:11,288 - ERROR- End of program
PS C:\Users\Administrator\Desktop\tmp>
3. 禁用日志 CRITICAL > ERROR > WARNING > INFO > DEBUG
从高到低:
CRITICAL | logging.critical() | 最高级别。用于表示致命的错误,它导致或将要导致程序完全停止工作 |
ERROR | logging.error() | 用于记录错误,它导致程序做某事失败 |
WARNING | logging.warning() | 用于表示可能的问题,它不会阻止程序的工作,但将来可能会 |
INFO | logging.info() | 用于记录程序中一般事件的信息,或确认一切工作正常 |
DEBUG | logging.debug() | 最低级别。用于小细节。通常只有在诊断问题时,你才会关心这些消息 |
调试程序时,你可能只对错误感兴趣。
在这种情况下,可以将 basicConfig() 的 level 参数设置为 logging.ERROR,
这将只显示 ERROR和 CRITICAL 消息,跳过 DEBUG、INFO 和 WARNING 消息。
如果想要禁用所有日志,只要在程序中添加 logging. disable(logging.CRITICAL)。
4.将日志记录到文件
除了将日志消息显示在屏幕上,还可以将它们写入文本文件。logging.basic Config() 函数接受 filename 关键字参数,
import logging
logging.basicConfig( filename='myProgramLog.txt',level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
在前面2中的实例中,将上述代码加入其中,就会将输出结果保存在 myProgramLog.txt 文件中,截图如下:
5.IDLE 的调试器
在IDLE 中,点击 Debug->Debuger 启动调试器
Go: 程序继续运行,直至下一个断点,或者结束
Step: 步进,运行下一行代码 Over: 运行下一行代码,遇到函数则跳过
Out: 在函数中点击这个,就会全速跑完函数
Quit : 退出调试
可以通过右键 设置断点:
按F5 或者 鼠标点击 Run Module 就会在 IDLE 中运行,如果IDLE开了 调试器的话,就可以进行调试了。