A simple Python CLI tool to search log files (single file or directory). No external dependencies — requires Python 3.8+.
- Python 3.8 or newer
On macOS, Debian, Ubuntu — copy the script to a system binary directory and make it executable:
# while in the project directory
wget https://raw.githubusercontent.com/siwikm/loggrep/main/loggrep.py
sudo cp loggrep.py /usr/local/bin/loggrep
sudo chmod +x /usr/local/bin/loggrepAfter this you can run the tool using the loggrep command.
Alternative: run without installation:
python3 loggrep.py <path> <phrase1> [phrase2 ...] [options]loggrep <path_to_file_or_directory> <phrase1> [phrase2 ...] [options]Output: each matched line is printed in the format path:line: content.
-i,--ignore-case— ignore letter case-a,--any— show lines containing ANY phrase (default: ALL phrases)-r,--recursive— search directories recursively (only when the provided path is a directory)-e,--include <pattern>— include only files matching glob pattern (e.g.,*.log,*.txt). Can be used multiple times-v,--verbose— display detailed operation logs-o,--output <file>— save results to a file-w,--window <N>— search phrases within a window of N adjacent lines (default: 1 line)--no-line-numbers— omit line numbers from the output-c,--count— print only the number of matches per file-l,--files-only— print only filenames that contain matches (likegrep -l)
- Search for lines that contain both
ERRORanddatabasein a file:
loggrep tests/test_files/t2.log ERROR database- Search for lines that contain
ERRORORWARNING(any mode):
loggrep /var/logs 'ERROR' 'WARNING' --any- Recursive search in a directory:
loggrep /var/logs 'ERROR' --recursive- Ignore case:
loggrep app.log 'error' 'failed' --ignore-case- Save results to a file:
loggrep app.log 'payment_intent' 'succeeded' -o results.txt- Verbose mode (diagnostics):
loggrep /var/logs 'ERROR' --recursive --verbose- Search for phrases across a window of adjacent lines (e.g., 3-line window):
loggrep app.log 'ERROR' 'database' --window 3- Show only filenames or match counts:
loggrep tests/test_files 'cardType' --files-only
loggrep tests/test_files/t2.log 'INFO' --count- Filter by file extension (recursive search):
# Search only .log files (note: glob patterns must be quoted)
loggrep /var/logs 'ERROR' -r -e '*.log'
# Search both .log and .txt files
loggrep /var/logs 'ERROR' -r -e '*.log' -e '*.txt'Note: Always quote glob patterns (e.g., '*.log') to prevent shell expansion.
- "No files found to search." — check that the provided path exists and you have permissions.
- UnicodeDecodeError — the file may be binary or use an encoding other than UTF-8.
- To run without installing globally, use
python3 loggrep.py.