import argparse
import re
def convert_log(input_file, output_file, version="1.0"):
"""
Converts log files from older formats to a newer format.
Supports version 1.0 and older formats.
"""
try:
with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
for line in infile:
if version == "1.0":
# Example conversion for version 1.0
# Assuming older format: "Timestamp - Message"
match = re.match(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) - (.*)", line)
if match:
timestamp = match.group(1)
message = match.group(2)
outfile.write(f"[{timestamp}] {message}\n")
else:
outfile.write(line) # Write unchanged if no match
elif version == "0.9":
# Example conversion for version 0.9
# Assuming older format: "Date: Message"
match = re.match(r"Date: (.*)", line)
if match:
date_message = match.group(1)
parts = date_message.split(" ")
if len(parts) >= 2:
date = parts[0]
message = " ".join(parts[1:])
outfile.write(f"[{date}] {message}\n")
else:
outfile.write(line)
else:
outfile.write(line)
else:
outfile.write(line) # Pass through if version not supported
except FileNotFoundError:
print(f"Error: Input file '{input_file}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert log files to a newer format.")
parser.add_argument("input_file", help="Path to the input log file.")
parser.add_argument("output_file", help="Path to the output log file.")
parser.add_argument("--version", default="1.0", help="Version of the log format to convert to (default: 1.0). Supported versions: 0.9, 1.0")
args = parser.parse_args()
convert_log(args.input_file, args.output_file, args.version)
Add your comment