import logging # Create a custom logger logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # Set the lowest level for the logger # Create formatters debug_formatter = logging.Formatter('%(asctime)s - [DEBUG] - %(message)s') error_formatter = logging.Formatter('%(asctime)s - [ERROR] - %(message)s') info_formatter = logging.Formatter('%(asctime)s - [INFO] - %(message)s') # Create file handlers debug_file_handler = logging.FileHandler('debug.log') debug_file_handler.setLevel(logging.DEBUG) # Capture all logs debug_file_handler.setFormatter(debug_formatter) error_file_handler = logging.FileHandler('error.log') error_file_handler.setLevel(logging.ERROR) # Capture only ERROR and CRITICAL logs error_file_handler.setFormatter(error_formatter) info_file_handler = logging.FileHandler('info.log') info_file_handler.setLevel(logging.INFO) # Capture INFO, WARNING, ERROR, and CRITICAL logs info_file_handler.setFormatter(info_formatter) # Add handlers to the logger logger.addHandler(debug_file_handler) logger.addHandler(error_file_handler) logger.addHandler(info_file_handler) # Log messages logger.debug('This is a debug message') logger.info('This is an info message') logger.error('This is an error message')
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter