show_by_prettytable.py 731 B

123456789101112131415161718192021222324252627282930
  1. # update: 2021-1-15-18
  2. import prettytable as pt
  3. import logging
  4. logger = logging.getLogger('log_method')
  5. def show_logs(tags, args, is_work=True):
  6. """log格式化"""
  7. if not is_work:
  8. return
  9. if type(args) == dict:
  10. text = pt.PrettyTable()
  11. text.field_names = ['参数', '类型', '值']
  12. text.align = 'l'
  13. for name, value in args.items():
  14. too_long = len(str(value)) > 200
  15. if too_long:
  16. dots = '..'
  17. else:
  18. dots = ''
  19. text.add_row([name, str(type(value)), str(repr(value))[:200] + dots])
  20. logger.info(f"{tags}:\n{text}")
  21. elif type(args) == list:
  22. lines = f"{tags}:"
  23. for count, line in enumerate(args):
  24. lines += f"\n- {count + 1} - {line}"
  25. logger.info(lines)
  26. else:
  27. logger.info(f"{tags}: {args}")