import logging # Create or get the root logger logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # Set the logging level # Create a file handler file_handler = logging.FileHandler('app.log') file_handler.setLevel(logging.DEBUG) # Set the level for this handler # Create a console handler (optional, for logging to the console too) console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) # Create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler.setFormatter(formatter) console_handler.setFormatter(formatter) # Add the handlers to the logger logger.addHandler(file_handler) logger.addHandler(console_handler) # Log some messages logger.debug("This is a debug message") logger.info("This is an info message") logger.warning("This is a warning message") logger.error("This is an error message") logger.critical("This is a critical message")