Logging in Python
Logging in Python
Logging is a means of tracking events that happen when some software runs. Logging is important for
software developing, debugging, and running. If you don't have any logging record and your program
crashes, there are very few chances that you detect the cause of the problem. And if you detect the cause, it
will consume a lot of time. With logging, you can leave a trail of breadcrumbs so that if something goes
There are a number of situations like if you are expecting an integer, you have been given a float and you can
a cloud API, the service is down for maintenance, and much more. Such problems are out of control and are
hard to determine.
Some developers use the concept of printing the statements to validate if the statements are executed
correctly or if some error has occurred. But printing is not a good idea. It may solve your issues for simple
scripts but for complex scripts, the printing approach will fail.
Python has a built-in module logging which allows writing status messages to a file or any other output
streams. The file can contain information on which part of the code is executed and what problems have
arisen.
- Debug: Used to give detailed information, typically of interest only when diagnosing problems.
- Warning: Used as an indication that something unexpected happened, or is indicative of some problem in
Logging in Python
- Error: Indicates a more serious problem; the software has not been able to perform some function.
- Critical: Tells serious error, indicating that the program itself may be unable to continue running.
NOTSET 0
DEBUG 10
INFO 20
WARNING 30
ERROR 40
CRITICAL 50