aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/loghandler.py
blob: 2cc7cebc3a99d3adb7f84d9d73b2c00f34caa57a (plain)
  1. import logging, time
  2. class LogFormatter(logging.Formatter):
  3. def __init__(self, docolor, timestamps=False):
  4. super().__init__()
  5. self.docolor = docolor
  6. self.timestamps = timestamps
  7. def formatMessage(self, record):
  8. if self.docolor:
  9. c_lvl = 33
  10. c_mod = 32
  11. c_msg = 0
  12. if record.levelno == logging.WARNING:
  13. c_lvl = 31
  14. #c_mod = 33
  15. c_msg = 33
  16. elif record.levelno > logging.WARNING:
  17. c_lvl = 31
  18. c_mod = 31
  19. c_msg = 31
  20. fmt = '\x1b['+str(c_lvl)+'m%(levelname)8s\x1b[0m \x1b['+str(c_mod)+'m%(name)s\x1b['+str(c_msg)+'m: %(message)s\x1b[0m'
  21. else:
  22. fmt = '%(levelname)8s %(name)s: %(message)s'
  23. if self.timestamps:
  24. fmt = '%(asctime)s '+fmt
  25. if not 'asctime' in record.__dict__:
  26. record.__dict__['asctime']=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(record.__dict__['created']))
  27. return fmt % record.__dict__
  28. class LogHandler(logging.StreamHandler):
  29. def __init__(self, docolor, timestamps):
  30. super().__init__()
  31. self.setFormatter(LogFormatter(docolor,timestamps))