Log Keyword Counter
Build a Perl script that scans log files and counts how often chosen keywords and severity levels appear, such as ERROR, WARN, timeout or out of memory, showing the totals for each file and overall, and the first time each keyword appeared. The keywords come from a small config file, matching can ignore upper and lower case, and the script can read several files at once, including compressed .gz logs. It's a good first Perl project: you'll learn reading files line by line, regular expressions, hashes for counting, reading a config file, command-line options with Getopt::Long, printing aligned reports, and testing with Test::More.
- Estimate
- ~6h
- Steps
- 5
- Completed by
- 0
- Proposed by
- codeseed.app
Perl core modules · Getopt::Long · Test::More
Project roadmap
- 01
Define the input and output
~1hWrite README.md: the keywords file (one keyword or phrase per line, with # comments, and an optional label, such as timeout = Timeouts), the command (logcount.pl --keywords FILE [--ignore-case] [--whole-word] LOGFILES...), and an example report with counts per file, totals, and the first line number where each keyword appeared. Done when: README.md describes the files, options and report.
- 02
Load the keywords
~1hRead the keywords file, skipping comments and blank lines, and build one precompiled regular expression per keyword with qr//, escaping special characters with quotemeta, adding \b around words for --whole-word and the i flag for --ignore-case. Report an empty or missing keywords file clearly. Done when: the script prints the loaded keywords and their patterns.
- 03
Count keywords in the logs
~1.5hRead each log file line by line (opening .gz files through IO::Uncompress::Gunzip or a gzip pipe), and for each keyword count the matching lines in a hash of hashes (file, then keyword), remembering the first line number of each. Report files that can't be opened and continue with the others. Done when: counts for a test log match counts made with grep -c.
- 04
Print the report
~1hPrint a table with printf: one row per keyword with its count in each file and the total, sorted by total, and a line showing where each keyword first appeared. Add --csv to print the same data as CSV. Done when: the report is aligned and the CSV opens in a spreadsheet.
- 05
Test it
~1.5hWrite Test::More tests with small log files created in the test: case-sensitive and case-insensitive matching, whole words, keywords with special characters (such as [ERROR]), .gz files, a missing file, and totals across several files. Done when: prove -l t/ passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack