Python debug: Logging and PDB (instance parsing two)

Source: Internet
Author: User
In the previous article we learned why Python is debugging, and the two methods of Python debugging, but the debugging method is not finished, so in this article we will talk about the remaining two debugging methods. Hopefully, these kinds of debugging methods will help you get on the path to learning python faster.

Logging

The third method is: replacing print () with logging is the 3rd way, and the assert ratio, logging does not throw an error, and can be output to a file:

Import loggings = ' 0 ' n = Int (s) logging.info (' n =%d '% n) print (10/n)

Logging.info () can output a piece of text. Run, found no information except Zerodivisionerror. What's going on?

Don't worry, add a line configuration after import logging and try again:

Import Logginglogging.basicconfig (Level=logging.info)

See the output:

$ python err.pyinfo:root:n = 0Traceback (most recent call last):  File "err.py", line 8, in <module>    print (10 /n) zerodivisionerror:division by zero

This is the benefit of logging, which allows you to specify the level of logging information, Debug,info,warning,error, and so on, when we specify Level=info, Logging.debug does not work. Similarly, after specifying level=warning, debug and info will not work. This way, you can confidently output different levels of information, do not delete, and finally unified control the output of which level of information.

Another benefit of logging is that with a simple configuration, a single statement can be output to a different place at the same time, such as the console and the file.

Pdb

The 4th Way is to launch the Python debugger pdb, to let the program run in a single step, and to view the running state at any time. We'll get the program ready.

# Err.pys = ' 0 ' n = Int (s) print (10/n)

Then start:

$ python-m pdb err.py>/users/michael/github/learn-python3/samples/debug/err.py (2) <module> () s = ' 0 '

After starting with the parameter-m PDB, the PDB navigates to the next code to execute, s = ' 0 '. Enter command l to view the code:

(PDB) L  1     # err.py  2-  s = ' 0 '  3     n = Int (s)  4     print (10/n)

Enter command N to step through the code:

(PDB) n>/users/michael/github/learn-python3/samples/debug/err.py (3) <module> () n = Int (s) (PDB) n>/ users/michael/github/learn-python3/samples/debug/err.py (4) <module> () print (10/n)

At any time, you can enter the command p variable name to view the variable:

(PDB) p s ' 0 ' (PDB) p N0

Enter command q to end debugging, exit the program:

(PDB) Q

(This method of debugging through the PDB on the command line is theoretically omnipotent, but it's too much trouble.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.