123456789101112131415161718192021222324252627282930313233343536
  1. import logging
  2. class LogSystem(object):
  3. handlerList = []
  4. showOnCmd = True
  5. loggingLevel = logging.INFO
  6. loggingFile = None
  7. def __init__(self):
  8. self.logger = logging.getLogger('itchat')
  9. self.logger.addHandler(logging.NullHandler())
  10. self.logger.setLevel(self.loggingLevel)
  11. self.cmdHandler = logging.StreamHandler()
  12. self.fileHandler = None
  13. self.logger.addHandler(self.cmdHandler)
  14. def set_logging(self, showOnCmd=True, loggingFile=None,
  15. loggingLevel=logging.INFO):
  16. if showOnCmd != self.showOnCmd:
  17. if showOnCmd:
  18. self.logger.addHandler(self.cmdHandler)
  19. else:
  20. self.logger.removeHandler(self.cmdHandler)
  21. self.showOnCmd = showOnCmd
  22. if loggingFile != self.loggingFile:
  23. if self.loggingFile is not None: # clear old fileHandler
  24. self.logger.removeHandler(self.fileHandler)
  25. self.fileHandler.close()
  26. if loggingFile is not None: # add new fileHandler
  27. self.fileHandler = logging.FileHandler(loggingFile)
  28. self.logger.addHandler(self.fileHandler)
  29. self.loggingFile = loggingFile
  30. if loggingLevel != self.loggingLevel:
  31. self.logger.setLevel(loggingLevel)
  32. self.loggingLevel = loggingLevel
  33. ls = LogSystem()
  34. set_logging = ls.set_logging