一般我的配置如下:
import logging logger = logging.getLogger(__name__)
if os.path.exists('./log'): pass else: os.makedirs('./log') handler = logging.FileHandler("./log/" + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + ".log", encoding='utf-8') handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler)
具体logging的用法解析,详见https://docs.python.org/3/library/logging.html