log_highlighter.py 999 B

123456789101112131415161718192021222324252627282930
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import re
  5. from html import escape
  6. def highlight_text(input_text):
  7. color_mapping = {
  8. r'\d{2}:\d{2}:\d{2}\.\d{3}': 'cyan',
  9. r'\bTrue\b': 'lime',
  10. r'\bFalse\b': 'red',
  11. r'DEBUG': 'cyan',
  12. r'INFO': 'green',
  13. r'WARNING': 'yellow',
  14. r'ERROR': 'red',
  15. r'CRITICAL': 'darkred'
  16. }
  17. for pattern, color in color_mapping.items():
  18. input_text = re.sub(pattern, f'<span style="color: {color}">\\g<0></span>', input_text)
  19. return input_text + '<br>'
  20. # 测试
  21. # input_string = 'The time is 13:20:36.411. Is it True or False? DEBUG: This is a debug message. INFO: This is an info message. WARNING: This is a warning message. ERROR: This is an error message. CRITICAL: This is a critical message.'
  22. # highlighted_text = highlight_text(input_string)
  23. # html_text = f'<html><body>{highlighted_text}</body></html>'
  24. # print(html_text)