Python

Logging

Logging is a very powerful way of validating your program executes correctly. In addition you can use it to debug your program.

You can log a process in your program using the module logging.

Online Courses
Complete Python Bootcamp: Go from zero to hero in Python

Example
This message would be shown in the terminal.

 
import logging
logging.warning('Something went wrong.')

Levels of severity
There are several levels of severity: DEBUG, INFO, WARNING, ERROR, CRITICAL.
You can configure a minimum level of severity, if it’s lower than the set level it’s ignored.

 
import logging

logging.basicConfig(level=logging.WARNING)
logging.debug('Debug message')
logging.error('This is an error')

Another example:

 
import logging

logging.basicConfig(level=logging.ERROR)
logging.debug('Debug message')
logging.info('Program started..')
logging.info('Loading files')
logging.error('This is an error')

While developing you could set the severity level to logging.DEBUG which will show all messages. This will help you develop faster, find bugs quicker and so on.

Upon release of your program to the market you could set it to debugging.WARNING or debugging.ERROR.

 
import logging
logging.basicConfig(level=logging.WARNING)

Previous Post

Cookie policy | Privacy policy | ©