import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) logger.debug('Debug message') logger.info('Info message') logger.warning('Warning message') logger.error('Error message') logger.critical('Critical message')
import logging formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler = logging.FileHandler('example.log') file_handler.setLevel(logging.ERROR) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(formatter) logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) logger.addHandler(console_handler) logger.debug('Debug message') logger.info('Info message') logger.warning('Warning message') logger.error('Error message') logger.critical('Critical message')In this example, we configure a Logger instance with two handlers, one for logging to a file and one for logging to the console. Each handler has its log level and formatter. The logger is set to the DEBUG level and publishes log messages to both handlers. Package library: logging