Skip to content

siwikm/loggrep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

loggrep

A simple Python CLI tool to search log files (single file or directory). No external dependencies — requires Python 3.8+.

Requirements

  • Python 3.8 or newer

Installation (global)

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/loggrep

After this you can run the tool using the loggrep command.

Alternative: run without installation:

python3 loggrep.py <path> <phrase1> [phrase2 ...] [options]

Syntax (CLI)

loggrep <path_to_file_or_directory> <phrase1> [phrase2 ...] [options]

Output: each matched line is printed in the format path:line: content.

Options

  • -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 (like grep -l)

Examples

  1. Search for lines that contain both ERROR and database in a file:
loggrep tests/test_files/t2.log ERROR database
  1. Search for lines that contain ERROR OR WARNING (any mode):
loggrep /var/logs 'ERROR' 'WARNING' --any
  1. Recursive search in a directory:
loggrep /var/logs 'ERROR' --recursive
  1. Ignore case:
loggrep app.log 'error' 'failed' --ignore-case
  1. Save results to a file:
loggrep app.log 'payment_intent' 'succeeded' -o results.txt
  1. Verbose mode (diagnostics):
loggrep /var/logs 'ERROR' --recursive --verbose
  1. Search for phrases across a window of adjacent lines (e.g., 3-line window):
loggrep app.log 'ERROR' 'database' --window 3
  1. Show only filenames or match counts:
loggrep tests/test_files 'cardType' --files-only
loggrep tests/test_files/t2.log 'INFO' --count
  1. 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.