log.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import os
  4. import datetime
  5. import sys
  6. def info(str):
  7. script_path = os.path.split(os.path.realpath(__file__))[0]
  8. nowTime = datetime.datetime.now().strftime('%Y%m%d')
  9. path = os.path.join(script_path + r'\logs', 'info_' + nowTime + ".log")
  10. isExists = os.path.exists(script_path + r'\logs')
  11. if not isExists:
  12. os.makedirs(script_path + r'\logs')
  13. logger = logging.getLogger("run")
  14. logger.setLevel(level=logging.INFO)
  15. handler = logging.FileHandler(path)
  16. handler.setLevel(logging.INFO)
  17. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s => %(message)s')
  18. handler.setFormatter(formatter)
  19. logger.addHandler(handler)
  20. logger.info(str)
  21. logger.removeHandler(handler)
  22. def warning(str):
  23. script_path = os.path.split(os.path.realpath(__file__))[0]
  24. nowTime = datetime.datetime.now().strftime('%Y%m%d')
  25. path = os.path.join(script_path + r'\logs', 'info_' + nowTime + ".log")
  26. isExists = os.path.exists(script_path + r'\logs')
  27. if not isExists:
  28. os.makedirs(script_path + r'\logs')
  29. logger = logging.getLogger("run")
  30. logger.setLevel(level=logging.WARNING)
  31. handler = logging.FileHandler(path)
  32. handler.setLevel(logging.WARNING)
  33. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s => %(message)s')
  34. handler.setFormatter(formatter)
  35. logger.addHandler(handler)
  36. logger.warning(str)
  37. logger.removeHandler(handler)
  38. def error(str):
  39. script_path = os.path.split(os.path.realpath(__file__))[0]
  40. nowTime = datetime.datetime.now().strftime('%Y%m%d')
  41. path = os.path.join(script_path + r'\logs', 'error_' + nowTime + ".log")
  42. isExists = os.path.exists(script_path + r'\logs')
  43. if not isExists:
  44. os.makedirs(script_path + r'\logs')
  45. logger = logging.getLogger("run")
  46. logger.setLevel(level=logging.ERROR)
  47. handler = logging.FileHandler(path)
  48. handler.setLevel(logging.ERROR)
  49. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s => %(message)s')
  50. handler.setFormatter(formatter)
  51. logger.addHandler(handler)
  52. logger.error(str)
  53. logger.removeHandler(handler)