Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Grep Rails log file targeting particular client/action

More info in my blog post Exploring Latest ruby-lib Additions

#!/usr/bin/env ruby

if ARGV.empty?
  puts <<-USAGE
Use like tail -f log/production.log | #{ $PROGRAM_NAME } request_regexp

Examples:

Show only local requests:
tail -f log/production.log | #{ $PROGRAM_NAME } 127.0.0.1

Show only GET requests:
tail -f log/production.log | #{ $PROGRAM_NAME } GET

Show only requests to /updates path:
tail -f log/production.log | #{ $PROGRAM_NAME } /updates
  USAGE
  exit(0)
end

request_regexp = /#{ ARGV[0] }/

STDIN.slice_before(/Started (GET|POST|PUT|DELETE)/).each do |request_log|
  puts request_log if request_log.first =~ request_regexp
end

You need to create an account or log in to post comments to this site.


Click here to browse all 7718 code snippets