Extract structured information from simple logs using regular expressions.
How It Works
Compile a clear pattern for a known log format and iterate through matches. Report rejected or malformed records rather than silently treating every line as valid; preserve enough source context for troubleshooting.
Step-by-Step Example
import re
log_lines = ['INFO: connected', 'ERROR: timeout', 'INFO: closed']
log_pattern = re.compile(r'(?P<level>INFO|ERROR): (?P<message>.+)')
error_messages = [match.group('message') for line in log_lines if (match := log_pattern.fullmatch(line)) and match.group('level') == 'ERROR']
print(error_messages)
Understanding the Example
- The expression "['INFO: connected', 'ERROR: timeout', 'INFO: closed']" supplies the value stored as 'log_lines'. This value is then used or checked by the later statements.
- The expression "re.compile(r'(?P<level>INFO|ERROR): (?P<message>.+)')" supplies the value stored as 'log_pattern'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
['timeout']
Where This Fits in a Real Program
Start by writing down examples that should match and examples that must fail. Check whether you need to locate a substring, match a prefix or validate an entire field; these are different operations. Unicode and escape sequences can also change which characters are matched, so test accented text and empty input.
Practice: Try It Yourself
Do not log credentials or sensitive information from real systems.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Assuming every log message follows an identical format or trying to parse nested structured logs with a fragile regex.
- Count events by severity and report malformed input lines.
Key Takeaways
- Extract structured information from simple logs using regular expressions.
- Compile a clear pattern for a known log format and iterate through matches.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.