新建test.py
import logging logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) handler = logging.FileHandler("runtest.log") handler.setLevel(logging.INFO) formatter = logging.Formatter('%(levelname)s-%(asctime)s -> %(message)s')# handler.setFormatter(formatter) logger.addHandler(handler) logger.info("begin run !")
运行
python test.py
然后查看当前目录的runtest.log,会看到下面的结果
INFO-2018-11-19 18:16:47,031 -> begin run !