summaryrefslogtreecommitdiff
path: root/voctocore/lib/loghandler.py
blob: d2fcb13813b778036c576bf95b1d09227bcefbfa (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. if self.timestamps:
  22. fmt = '%(asctime)s \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'
  23. else:
  24. 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'
  25. else:
  26. if self.timestamps:
  27. fmt = '%(asctime)s %(levelname)8s %(name)s: %(message)s'
  28. else:
  29. fmt = '%(levelname)8s %(name)s: %(message)s'
  30. if not 'asctime' in record.__dict__:
  31. record.__dict__['asctime']=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(record.__dict__['created']))
  32. return fmt % record.__dict__
  33. class LogHandler(logging.StreamHandler):
  34. def __init__(self, docolor, timestamps):
  35. super().__init__()
  36. self.setFormatter(LogFormatter(docolor,timestamps))