A simple CLI tool to scan log files and count occurrences of specific keywords.
This tool helps analyze log files by searching for and counting occurrences of specified keywords. It's particularly useful for quickly identifying error patterns, warning frequencies, or other important events in large log files.
- Keyword Search: Search for multiple keywords in a single scan
- Case Sensitivity: Optional case-sensitive or case-insensitive matching
- Line Numbers: Display line numbers where keywords appear
- Multiple Formats: Works with any text-based log format
- Performance: Efficiently handles large log files
No installation required! Just ensure you have Python 3.6+ installed.
# Clone the repository
git clone https://github.com/sumanth-0/100LinesOfPythonCode.git
cd 100LinesOfPythonCode/#1010_Tiny_Log_Analyzerpython log_analyzer.py <log_file> <keyword1> [keyword2] ...Search for ERROR and WARNING in a log file:
python log_analyzer.py server.log ERROR WARNINGCase-sensitive search:
python log_analyzer.py app.log -c Error WarningShow line numbers where keywords appear:
python log_analyzer.py system.log -l ERROR CRITICALCombine options:
python log_analyzer.py debug.log -c -l Exception Tracebacklogfile: Path to the log file to analyze (required)keywords: One or more keywords to search for (required)-c, --case-sensitive: Make keyword matching case-sensitive (default: case-insensitive)-l, --lines: Show line numbers where keywords appear-h, --help: Show help message
==================================================
LOG ANALYSIS RESULTS
==================================================
ERROR: 45 occurrences
Lines: 12, 34, 56, 78, 90, 123, 145, 167, 189, 201 ... (35 more)
WARNING: 23 occurrences
Lines: 5, 15, 25, 35, 45, 55, 65, 75, 85, 95 ... (13 more)
INFO: 567 occurrences
==================================================
Total matches: 635
==================================================
- Opens and reads the specified log file
- Scans each line for the specified keywords
- Counts occurrences (including multiple occurrences per line)
- Optionally tracks line numbers where keywords appear
- Displays results sorted by frequency
- Python 3.6 or higher
- No external dependencies (uses only standard library)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is part of the 100LinesOfPythonCode repository.
Created for Issue #1010 - Tiny Log Analyzer
Fixes #1010