No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

70 líneas
2.1KB

  1. # Copyright (C) 2015 ACSONE SA/NV
  2. # Part of this file come from the Acsone git-aggregator project
  3. # https://github.com/acsone/git-aggregator
  4. # Copyright (C) 2019 - Today: GRAP (http://www.grap.coop)
  5. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. from colorama import Fore, Style
  8. import time
  9. import logging
  10. logger = logging.getLogger(__name__)
  11. LEVEL_COLORS = {
  12. 'DEBUG': Fore.BLUE,
  13. 'INFO': Fore.GREEN,
  14. 'WARNING': Fore.YELLOW,
  15. 'ERROR': Fore.RED,
  16. 'CRITICAL': Fore.RED
  17. }
  18. def setup_logger(level, file_path=False):
  19. if not file_path:
  20. handler = logging.StreamHandler()
  21. handler.setFormatter(OdooMigrateFormatter())
  22. else:
  23. handler = logging.FileHandler(file_path)
  24. handler.setFormatter(logging.Formatter(
  25. "%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s"))
  26. logger.addHandler(handler)
  27. logger.setLevel(getattr(logging, str(level)))
  28. class OdooMigrateFormatter(logging.Formatter):
  29. def format(self, record):
  30. """Overwrite format() function to use custom formatter"""
  31. record.message = record.getMessage()
  32. record.asctime = time.strftime(
  33. '%H:%M:%S', self.converter(record.created)
  34. )
  35. prefix = self.default_prefix_template(record) % record.__dict__
  36. return (prefix + " " + record.message).replace(
  37. "\n", "\n" + "".ljust(23, " "))
  38. def default_prefix_template(self, record):
  39. """Return the prefix for the log message. Template for Formatter.
  40. :param: record: :py:class:`logging.LogRecord` object. this is passed in
  41. from inside the :py:meth:`logging.Formatter.format` record.
  42. """
  43. reset = [Style.RESET_ALL]
  44. levelname = [
  45. LEVEL_COLORS.get(record.levelname), Style.BRIGHT,
  46. '%(levelname)-10s',
  47. Style.RESET_ALL, ' '
  48. ]
  49. asctime = [
  50. '', Fore.BLACK, Style.DIM, Style.BRIGHT,
  51. '%(asctime)-10s',
  52. Fore.RESET, Style.RESET_ALL, ' '
  53. ]
  54. return "".join(reset + asctime + levelname + reset)
上海开阖软件有限公司 沪ICP备12045867号-1