A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import logging
  23. __all__ = ["BotFormatter"]
  24. class BotFormatter(logging.Formatter):
  25. def __init__(self, color=False):
  26. self._format = super(BotFormatter, self).format
  27. if color:
  28. fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
  29. self.format = lambda rec: self._format(self.format_color(rec))
  30. else:
  31. fmt = "[%(asctime)s %(levelname)-8s] %(name)s: %(message)s"
  32. self.format = self._format
  33. datefmt = "%Y-%m-%d %H:%M:%S"
  34. super(BotFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
  35. def format_color(self, record):
  36. l = record.levelname.ljust(8)
  37. if record.levelno == logging.DEBUG:
  38. record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
  39. if record.levelno == logging.INFO:
  40. record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
  41. if record.levelno == logging.WARNING:
  42. record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
  43. if record.levelno == logging.ERROR:
  44. record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
  45. if record.levelno == logging.CRITICAL:
  46. record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
  47. return record