Log.py 668 B

123456789101112131415161718192021
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from logging import handlers
  4. # 创建一个日志记录文件,名称是testlog
  5. logger = logging.getLogger('testlog')
  6. # 设置日志级别
  7. logger.setLevel(logging.INFO)
  8. # 创建一个RotatingFileHandler,用来轮转日志文件
  9. th = handlers.RotatingFileHandler(filename='testlog.log', mode='w', encoding='utf-8')
  10. # 设置日志格式
  11. fmt = '[%(asctime)s] %(levelname)s %(message)s'
  12. formatter = logging.Formatter(fmt)
  13. th.setFormatter(formatter)
  14. # 将处理器添加到日志记录器
  15. logger.addHandler(th)
  16. # # 创建一个输出日志到控制台的StreamHandler
  17. # hdr = logging.StreamHandler()
  18. # hdr.setFormatter(formatter)