From 698c0c2a818bccf82f3643b9616c526d80f9015b Mon Sep 17 00:00:00 2001 From: chenjintang-shrimp Date: Fri, 8 Aug 2025 17:38:37 +0000 Subject: [PATCH] chore: add a little script to clean ANSI charactors from logs from uvicorn --- remove_ansi.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 remove_ansi.py diff --git a/remove_ansi.py b/remove_ansi.py new file mode 100644 index 0000000..1720888 --- /dev/null +++ b/remove_ansi.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +""" +Script to remove ANSI escape codes from log files +""" + +from __future__ import annotations + +import re +import sys + + +def remove_ansi_codes(text): + """ + Remove ANSI escape codes from text + """ + # Regular expression to match ANSI escape codes + ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") + return ansi_escape.sub("", text) + + +def process_log_file(input_file, output_file=None): + """ + Process log file and remove ANSI escape codes + """ + if output_file is None: + output_file = ( + input_file.replace(".log", "_clean.log") + if ".log" in input_file + else input_file + "_clean" + ) + + with open(input_file, "r", encoding="utf-8") as infile: + content = infile.read() + + # Remove ANSI escape codes + clean_content = remove_ansi_codes(content) + + with open(output_file, "w", encoding="utf-8") as outfile: + outfile.write(clean_content) + + print(f"Processed {input_file} -> {output_file}") + + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python remove_ansi.py [output_file]") + sys.exit(1) + + input_file = sys.argv[1] + output_file = sys.argv[2] if len(sys.argv) > 2 else None + + process_log_file(input_file, output_file)