Skip to content

Instantly share code, notes, and snippets.

@nrdsrfr
Last active August 29, 2015 14:01
Show Gist options
  • Save nrdsrfr/4549ecefd2849fc4d7b8 to your computer and use it in GitHub Desktop.
Save nrdsrfr/4549ecefd2849fc4d7b8 to your computer and use it in GitHub Desktop.
Tailing Log Files

When debugging, it's nice to be able to easily see only what you want from your application log in real time. For *nix guys this is the norm, but Windows people have been crippled by the cmd.exe shell for ages and don't realise how easy it is. Luckily if you use Git for source control you will have Git Bash and/or Git Shell installed, which have some useful commands you can use.

The first is tail, which prints the last n lines of an input you give it (10 lines by default). You can use the -f option to "follow" the input, meaning it will give you nice scrolling text as the file updates assuming you tail a file as input.

Next up is grep which finds text patterns from an input and prints each line that matches. You can use simple text search grep search or regex with the -E option grep -E 'regex'.

Finally, you should be aware of "pipes" |, which allow you to pass output from one command into the input of another.

So, to put all this together, let's say you want to watch a log file for any lines with "foo" as they appear, you would use the shell command: tail -f log.txt | grep "foo". This watches for updates to the end of log.txt, then pipes the output of tail to the input of grep, which then filters the output to only lines with "foo".

Another example is to follow multiple files and search for multiple results via regex. This is easy. tail -f access.log error.log | grep -E 'ERROR|SEVERE' would give you real time output of lines containing "ERROR" or "SEVERE" in both access.log and error.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment