Skip to content

Instantly share code, notes, and snippets.

@pmontrasio
Created December 7, 2012 18:09
Show Gist options
  • Save pmontrasio/4235159 to your computer and use it in GitHub Desktop.
Save pmontrasio/4235159 to your computer and use it in GitHub Desktop.
Gets the last POST parameters in Rails's log/development.log and pretty prints them
#!/bin/bash
# DO NOT USE ON PRODUCTION LOGS: maybe there are ways to inject Ruby code into the
# parameters hash and you'll end up evaluating it
#
# usage: post_params
# run this in the main Rails directory
# tested on Linux with ruby 1.9.3-p327, bash 4.2.24
last_post_line=`grep -n "Started POST " log/development.log |tail -1|cut -f1 -d":"`
hash_line=`expr $last_post_line + 2`
hash=`sed -n -e "${hash_line}p" log/development.log |sed -e 's/Parameters:/hash =/'`
echo "require 'pp';$hash;pp hash" | ruby -E UTF-8
@pmontrasio
Copy link
Author

A naive shell script to monitor the development log:

while true; do
  inotifywait -q log/development.log
  post_params
done

This is suboptimal because Rails modifies the log file many times during the processing of a request. The monitoring program should be smart enough to run post_params only when the file has been left alone for a few seconds.

@maca
Copy link

maca commented May 21, 2014

It wasn't working for me with Rails 3.2.x

#!/bin/bash

echo $(grep -e "Started \(GET\|POST\|PUT\)" log/development.log | tail -1)
grep -e "Parameters:" log/development.log | tail -1 | sed "s|Parameters:\s||" | ruby -ne 'require "pp"; pp eval($_)'

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