import logging LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p" logging.basicConfig(filename='my.log', level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) #下面就可以在程序内以以下方式记录入my.log文档内: logging.debug("This is a debug log.") logging.info("This is a info log.") logging.warning("This is a warning log.") logging.error("This is a error log.") logging.critical("This is a critical log.") 输出结果格式: 05/08/2017 14:29:04 PM - DEBUG - This is a debug log. 05/08/2017 14:29:04 PM - INFO - This is a info log. 05/08/2017 14:29:04 PM - WARNING - This is a warning log. 05/08/2017 14:29:04 PM - ERROR - This is a error log. 05/08/2017 14:29:04 PM - CRITICAL - This is a critical log.