import logging logging.basicConfig(filename='example.log', level=logging.INFO) logging.info('This is an info message.')
import logging logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.info('This is another info message.')In this example, we create a logger object and set its logging level to INFO. We also create a StreamHandler, which will log messages to the console. We set the logging level for the handler to INFO and attach a formatter to it. Finally, we log an information message using the LOGGER info method. Python's built-in logging module is used for these examples.