import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.debug('Some debugging details.')
# Output: 2015-05-18 19:04:26,901 - DEBUG - Some debugging details.
logging.info('The logging module is working.')
# Output: 2015-05-18 19:04:35,569 - INFO - The logging module is working.
logging.warning('An error message is about to be logged.')
# Output: 2015-05-18 19:04:56,843 - WARNING - An error message is about to be logged.
logging.error('An error has occurred.')
# Output: 2015-05-18 19:05:07,737 - ERROR - An error has occurred.
logging.critical('The program is unable to recover!')
# Output: 2015-05-18 19:05:45,794 - CRITICAL - The program is unable to recover!
Disable Logging
import logging
logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s')
logging.critical('Critical error!') # Output: 2015-05-22 11:10:48,054 - CRITICAL - Critical error!
logging.disable(logging.CRITICAL) # Disable critical level errors and below
logging.critical('Critical error!!!') # Will not be printed
logging.error('Error!') # Will not be printed
logging.warning('Warning!') # Will not be printed
Logging to a File
import logging
logging.basicConfig(filename='log.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.critical('Critical error!') # Output to file: 2016-10-28 16:17:35,121 - CRITICAL - Critical error!