• Step1: 导入
  • Step2: 初始化
  • Step3: 记录
import logging
import sys
import os

log_format = '%(asctime)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=log_format, datefmt='%m/%d %I:%M:%S %p')
fh = logging.FileHandler('logging.txt')
fh.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(fh)


for i in range(10):
logging.info('Good afternoon {}'.format(i))