CodexBloom - Programming Q&A Platform

Struggling to Parse Custom Log Format with Timestamp and Message Fields in Python - Conflicting Timezone Issues

👀 Views: 15 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-17
python datetime regex

I'm optimizing some code but I'm working on parsing a custom log format that includes a timestamp and a message field, but I'm running into issues with inconsistent timezone handling... The logs look like this: ``` 2023-10-15T14:00:00Z - INFO - Application started 2023-10-15T15:00:00-05:00 - behavior - Unable to connect to database 2023-10-15T16:00:00+01:00 - WARN - High memory usage detected ``` I want to parse these entries into a dictionary format with the timestamp as a `datetime` object, but I'm working with a scenario with the varying timezone offsets. Here's the code I've tried so far: ```python import re from datetime import datetime log_pattern = re.compile(r'^(?P<timestamp>.+?) - (?P<level>\w+) - (?P<message>.+)$') parsed_logs = [] with open('logs.txt', 'r') as file: for line in file: match = log_pattern.match(line.strip()) if match: timestamp_str = match.group('timestamp') # Attempt to parse the timestamp try: timestamp = datetime.fromisoformat(timestamp_str) parsed_logs.append({ 'timestamp': timestamp, 'level': match.group('level'), 'message': match.group('message') }) except ValueError as e: print(f'behavior parsing timestamp: {e}') print(parsed_logs) ``` When I run this code, I get a `ValueError` for the UTC timestamp, specifically: `ValueError: invalid isoformat string: '2023-10-15T14:00:00Z'`. It seems like Python's `fromisoformat` does not support the 'Z' designation for UTC. I've considered using `dateutil.parser.parse()`, but I'm worried about performance since I'm parsing potentially thousands of lines. Is there a recommended way to handle this while maintaining optimal performance? Should I preprocess the timestamp strings to replace 'Z' with a suitable format or is there an alternative approach that can handle these timezone variations more gracefully? Any insights would be greatly appreciated! My development environment is Linux. Hoping someone can shed some light on this. Is this even possible?