| #!/usr/bin/env python3
|
| import re
|
| from datetime import datetime
|
| import sys
|
| import argparse
|
| from pathlib import Path
|
|
|
| def convert_nginx_date(nginx_date):
|
| """Convert Nginx date format to ISO8601."""
|
| try:
|
| # Parse typical Nginx date format: 10/Oct/2023:13:55:36 +0000
|
| dt = datetime.strptime(nginx_date, '%d/%b/%Y:%H:%M:%S %z')
|
| return dt.isoformat()
|
| except ValueError as e:
|
| return nginx_date # Return original if parsing fails
|
|
|
| def process_log_file(input_file, output_file):
|
| """Process Nginx log file and convert timestamps to ISO8601."""
|
| # Match Nginx log timestamp format within square brackets
|
| timestamp_pattern = r'\[(.*?)\]'
|
|
|
| with open(input_file, 'r') as infile, open(output_file, 'w') as outfile:
|
| for line in infile:
|
| # Find and replace timestamps
|
| def replace_timestamp(match):
|
| timestamp = match.group(1)
|
| iso_timestamp = convert_nginx_date(timestamp)
|
| return f'[{iso_timestamp}]'
|
|
|
| # Replace timestamps while preserving the rest of the line
|
| new_line = re.sub(timestamp_pattern, replace_timestamp, line)
|
| outfile.write(new_line)
|
|
|
| def main():
|
| parser = argparse.ArgumentParser(description='Convert Nginx log timestamps to ISO8601 format')
|
| parser.add_argument('input_file', type=str, help='Input log file path')
|
| parser.add_argument('-o', '--output', type=str, help='Output file path (default: input_file.iso8601)')
|
|
|
| args = parser.parse_args()
|
|
|
| input_path = Path(args.input_file)
|
| if not input_path.exists():
|
| print(f"Error: Input file '{args.input_file}' not found")
|
| sys.exit(1)
|
|
|
| # Generate output filename if not specified
|
| output_file = args.output if args.output else f"{input_path}.iso8601"
|
|
|
| try:
|
| process_log_file(args.input_file, output_file)
|
| print(f"Successfully converted timestamps in '{args.input_file}'")
|
| print(f"Output written to '{output_file}'")
|
| except Exception as e:
|
| print(f"Error processing file: {e}")
|
| sys.exit(1)
|
|
|
| if __name__ == "__main__":
|
| main()
|