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