logger.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. import logging
  3. import tempfile
  4. from . import constants
  5. LOG_FILE = None
  6. LOGGER = None
  7. LEVELS = {
  8. constants.DEBUG: logging.DEBUG,
  9. constants.INFO: logging.INFO,
  10. constants.WARNING: logging.WARNING,
  11. constants.ERROR: logging.ERROR,
  12. constants.CRITICAL: logging.CRITICAL
  13. }
  14. def init(filename, level=constants.DEBUG):
  15. global LOG_FILE
  16. LOG_FILE = os.path.join(tempfile.gettempdir(), filename)
  17. with open(LOG_FILE, 'w'):
  18. pass
  19. global LOGGER
  20. LOGGER = logging.getLogger('Three.Export')
  21. LOGGER.setLevel(LEVELS[level])
  22. stream = logging.StreamHandler()
  23. stream.setLevel(LEVELS[level])
  24. format_ = '%(asctime)s - %(name)s - %(levelname)s: %(message)s'
  25. formatter = logging.Formatter(format_)
  26. stream.setFormatter(formatter)
  27. file_handler = logging.FileHandler(LOG_FILE)
  28. file_handler.setLevel(LEVELS[level])
  29. file_handler.setFormatter(formatter)
  30. LOGGER.addHandler(stream)
  31. LOGGER.addHandler(file_handler)
  32. def info(*args):
  33. LOGGER.info(*args)
  34. def debug(*args):
  35. LOGGER.debug(*args)
  36. def warning(*args):
  37. LOGGER.warning(*args)
  38. def error(*args):
  39. LOGGER.error(*args)
  40. def critical(*args):
  41. LOGGER.critical(*args)