📂 Python Log File Analyzer
A modular Python utility that analyzes log files, summarizes log levels, and extracts error and critical messages for easier debugging.
Log files help developers understand application behavior and investigate failures. However, large logs can make it difficult to identify errors and warnings quickly.
Python Log File Analyzer processes a log file line by line, counts entries by log level, identifies empty lines, and displays error and critical messages with their original line numbers.
The application uses Python's pathlib to handle file paths and collections.Counter to maintain log-level statistics. Its modular structure separates analysis logic from user interaction.
- Analyze log files line by line
- Count
INFO,WARNING,ERROR,CRITICAL, andDEBUGentries - Categorize unrecognized entries as
OTHER - Count total, empty, and non-empty lines
- Extract error and critical messages
- Display line numbers for important entries
- Validate file paths
- Handle file-reading errors
- Process files without loading the entire file into memory
- Python 3
pathlib— filesystem path handlingcollections.Counter— counting log levels- File handling, loops, functions, and exception handling
No third-party packages are required.
python-log-file-analyzer/
├── main.py
├── log_analyzer.py
└── README.md
main.pyhandles user input and displays the analysis report.log_analyzer.pycontains the file-reading, log classification, counting, and error-extraction logic.
Python 3.8 or newer.
- Download or clone the repository.
- Open the project folder in your terminal.
- Run:
python main.py- Enter the path to the log file you want to analyze.
Input file, sample.log:
2026-10-20 09:00:00 INFO Application started
2026-10-20 09:01:00 WARNING Disk space is low
2026-10-20 09:02:00 ERROR Database connection failed
2026-10-20 09:03:00 INFO Retry started
2026-10-20 09:04:00 CRITICAL Service unavailable
Expected summary:
Total lines: 6
Empty lines: 1
Non-empty lines: 5
INFO: 2
WARNING: 1
ERROR: 1
CRITICAL: 1
DEBUG: 0
OTHER: 0
The program also displays the line numbers and text of ERROR and CRITICAL entries.
- Reading files line by line
- Working with
pathlib - Counting values with
Counter - String processing and classification
- Enumerating lines with line numbers
- Handling empty lines
- Separating logic into reusable modules
- Handling file errors with exceptions
- Log classification uses keyword matching, so a word such as
ERRORappearing in unrelated message text may affect its classification. - The analyzer does not modify the original log file.
- Use log files you are authorized to access.
- Support configurable log formats
- Filter logs by date and time
- Export reports to CSV or JSON
- Search for custom keywords
- Generate visual summaries
- Add automated unit tests
Ayushman Tiwari
GitHub: @ayushman-ece
Built with Python to practise file processing, log analysis, and modular programming.