logger.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. 功能:日志模块
  3. 版本:v1.0
  4. 日期:2019/3/17
  5. """
  6. import logging
  7. import os.path
  8. import time
  9. # 第一步,创建一个logger
  10. custom_logger = logging.getLogger()
  11. custom_logger.setLevel(logging.INFO) # Log等级总开关
  12. # 第二步,创建一个handler,用于写入日志文件
  13. rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
  14. log_path = os.getcwd() + '/Logs/'
  15. if not os.path.exists(log_path):
  16. os.makedirs(log_path)
  17. log_name = log_path + rq + '.log'
  18. logfile = log_name
  19. fh = logging.FileHandler(logfile, mode='w')
  20. systemhandler=logging.StreamHandler()
  21. fh.setLevel(logging.INFO) # 输出到file的log等级的开关
  22. # 第三步,定义handler的输出格式
  23. formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
  24. fh.setFormatter(formatter)
  25. # 第四步,将logger添加到handler里面
  26. custom_logger.addHandler(fh)
  27. custom_logger.addHandler(systemhandler)
  28. def info(msg):
  29. custom_logger.info(msg)
  30. def debug(msg):
  31. custom_logger.debug(msg)
  32. def warn(msg):
  33. custom_logger.warning(msg)
  34. def error(msg):
  35. custom_logger.error(msg)
  36. def critical(msg):
  37. custom_logger.critical(msg)