import logging logger = logging.getLogger("demo") logger.setLevel(logging.DEBUG) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.DEBUG) logger.addHandler(stream_handler) file_handler = logging.FileHandler("logs.txt") file_handler.setFormatter( logging.Formatter("%(asctime)s - %(filename)s:%(lineno)d - %(message)s", datefmt="%Y-%m-%d %H:%M:%S") ) logger.addHandler(file_handler)