Monday, June 07, 2010

Quick and simple log filter

When I am developing locally, I frequently need to search through my local logs. There are already many useful tools already for this: UltraEdit has a great feature that shows all the lines matching your search term; so does Apache Chainsaw. But I want something even quicker, so I wrote a script. :)

This script relies on grep and Cygwin, and is actually a DOS batch file.

@echo off
REM Variables you probably should customize
SET DEFAULT_SEARCH_TERM=someco
SET LOG=%D:\apps\Alfresco-Community-3.3\alfresco.log%
SET EDITOR=C:\Program Files\IDM Computer Solutions\UltraEdit\Uedit32.exe
SET OUTPUT_FILE=C:\Temp\someco.log
SET GREP=C:\cygwin\bin\grep.exe

SET SEARCH_TERM=%1
IF NOT DEFINED SEARCH_TERM SET SEARCH_TERM=%DEFAULT_SEARCH_TERM%
"%GREP%" %2 %3 %4 %5 %6 %7 %8 %9 "%SEARCH_TERM%" "%LOG%" > "%OUTPUT_FILE%"
"%EDITOR%" "%OUTPUT_FILE%"

To use it, save it somewhere and at the very least, edit the variables to reflect whatever you want the default search term to be (DEFAULT_SEARCH_TERM), the path to your log file (LOG) and the path to your text editor (EDITOR). If you run it without any arguments, it will search through your log for the default search term, output the results into a file and open the file in your favourite text editor. Override the search term by giving the script a (quoted) argument.

And for those of you who run the magnificent Launchy, it is even simpler. Save the script into a directory that Launchy is indexing (for *.bat files). Then in Launchy, type the script's name hit TAB, type the quoted search term and the script will run with your search term.

The unquoted positional arguments (%2 %3 %4 %5 %6 %7 %8 %9) mean you can put arguments to grep after the search term when you execute the script, as long you ensure the search term is quoted if it contains spaces. So I can search for "SOMECO" in a case insensitive manner by invoking the script with arguments: "SOMECO" -i.