Python debugging study notes the log

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  If you added in the code print () statement, the output value of certain variables at runtime, you use the diary way to debug code. The diary is a good way,

We can understand what happened in the program, as well as the order of what happened. Python's logging module allows you to easily create custom recorded message. The program execution log messages will be described

When reaching the log function calls, and lists the value of any variable you specify at the time. On the other hand, deletion of part of the log information indicates that the code is skipped, never performed.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  1, using the log module

  Logging module is enabled, the program runs the log information displayed on the screen, the sample code:

! Python. 3 # 
# - * - Coding: UTF-. 8 - * - 
# Autor: Rong Yang of Li 
Import the logging 
logging.basicConfig (Level = logging.DEBUG, the format = '% (the asctime) S -% (levelname) S -% (Message) S ') 
# log presentation program starts with 
logging.debug (' of the start program ') 
DEF factorial (n-): 
    # prompt function starts with log 
    logging.debug (' start of factorial (% s %%) '% (n-)) 
    Total =. 1 
    for I in Range (n-+. 1): 
        Total I = * 
        # with variable results prompted the log function cycle 
        logging.debug ( 'i is' + str (i) + ', total is' STR + (Total)) 
    # log by the end of the prompt function 
    logging.debug ( 'end of factorial (% S %%)'% (n-)) 
    return Total 
Print (factorial (. 5)) 
# log presentation program ends with 
logging.debug ( 'End of program')

  operation result:

 

   According to operating results, we directly see where the problem lies, total have been 0.

  2. Do not use print () Debugging

  Import logging input and logging.basicConfig (level = logging.DEBUG, format = '% (asctime) s -% (levelname) s -% (message) s') is inconvenient. You may want to use print) calls instead of (but do not succumb to this temptation!

After commissioning, you need to spend a lot of time, remove each print log messages from the code () call. You might even accidentally delete some of the print () call, but they are not used to generate log messages. The benefits of log messages that you may want to drive in

How many would like to add in order to increase the number, as long as the later addition of a logging.disable (logging.CRITICAL) call, you can disable the log. Unlike print (), logging module logs so that the switching between displaying and hiding the information becomes easy.

  Disabling logging, the sample code:

! Python. 3 # 
# - * - Coding: UTF-. 8 - * - 
# Autor: Rong Yang of Li 
Import the logging 
logging.basicConfig (Level = logging.DEBUG, the format = '% (the asctime) S -% (levelname) S -% (Message) S ') logging.disable (logging.CRITICAL) 
# log presentation program starts with 
logging.debug (' of the start program ') 
DEF factorial (n-): 
    # prompt function starts with log 
    logging.debug (' start of factorial (% S %%) '% (n-)) 
    Total =. 1 
    for I in Range (n-+. 1): 
        Total I = * 
        # with the variables in the log function prompts cycle results 
        logging.debug (' i is' + str ( I) + ', IS Total' + STR (Total)) 
    # log by the end of the prompt function 
    logging.debug ( 'end of factorial (% S %%)'% (n-)) 
    return Total 
Print (factorial (. 5))

Tip # program ends with a log 
logging.debug ( 'End of program')

  operation result:

  3, log level

  "Log level" provides a way to log messages in order of importance are classified. 5 As shown in Table 10-1 logging level, from the most important to least important. Use different log functions, messages can be logged by a certain level.

  Sample code:

! Python. 3 # 
# - * - Coding: UTF-. 8 - * - 
# Autor: Rong Yang of Li 
Import the logging 
logging.basicConfig (Level = logging.DEBUG, the format = '% (the asctime) S -% (levelname) S -% (the Message) S ') 

# lowest level. For small details. Usually only when diagnosing problems, you will care about these messages 
logging.debug ( 's Some the debugging the Details.') 
# Cookies for the event in general, or to confirm everything is working properly 
logging.info ( 'The logging module is working . ') 
# is used to indicate possible problems, it does not prevent the work program, but the future may be 
logging.warning (' an error to the About the Message iS bE logged. ') 
# records for errors, which caused the program to do a things fail 
logging.error ( 'An error has occurred.') 
# the highest level. Is used to indicate a fatal error that causes or is about to cause the program to stop working completely 
logging.critical ( 'The program is unable to recover!')

  operation result:

  Log message as a string is passed to these functions. Log level is a recommendation. In the final analysis, or to you to decide what type of log message belongs.

   日志级别的好处在于,你可以改变想看到的日志消息的优先级。向basicConfig()函数传入logging.DEBUG作为level关键字参数,这将显示所有日志级别的消息

(DEBUG是最低的级别)。但在开发了更多的程序后,你可能只对错误感兴趣。在这种情况下,可以将 basicConfig() 的 level 参数设置为 logging.ERROR,这将只

显示 ERROR和 CRITICAL 消息,跳过 DEBUG、INFO 和 WARNING 消息。

  4、将日志信息写入文件

  虽然日志消息很有用,但它们可能塞满屏幕,让你很难读到程序的输出。将日志信息写入到文件,让屏幕保持干净,又能保存信息,这样在运行程序后,可以阅读

这些信息。

  示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import logging
logging.basicConfig(filename='d:\\MyProgramLog.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(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(5))
#用日志提示程序结束
logging.debug('End of program')

  运行结果:

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/9688482.html