Skip to content

Instantly share code, notes, and snippets.

@kalpesh-fulpagare
Last active March 20, 2024 13:13
Show Gist options
  • Save kalpesh-fulpagare/b1944c862d2b31f96078ada156653499 to your computer and use it in GitHub Desktop.
Save kalpesh-fulpagare/b1944c862d2b31f96078ada156653499 to your computer and use it in GitHub Desktop.
Handy Commands for Rails Server Debugging
# Rails console - handy commands
#
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Change log level to debug
Rails.logger.level = Logger::DEBUG
# Print the line & file from where SQL got triggerred
ActiveRecord::Base.verbose_query_logs = true
# View only Response headers for a Request
curl -s -I -X GET http://0.0.0.0/api/v1/products
# Send JSON request using curl
curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://0.0.0.0/products
# Send request from Server itself by adding host header
curl -i -N -H "Host: example.com" -H "Origin: https://example.com" http://10.20.30.40:80
# Send Basic Auth credentials using curl
curl --user 'username:password' http://api.example.com/v1/products
curl --user 'username' http://api.example.com/v1/products
# Send json payload using curl
curl -H "Content-Type: application/json" -X POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/v1/login
# ---------------------------------------------------
# Search all request for 1 matching text
prodlog="/var/apps/sample-project-name/shared/log/production.log"
OUTPUT1=$(grep -a "logged in device: c17d0784-a1e5-4223-a124-a5935abd1be4" $prodlog | grep -o "........-....-....-....-............" | awk 'ORS="|"' | sed 's/.$//'); if [ -z "$OUTPUT1" ]; then echo "No Request Logs"; else egrep -a $OUTPUT1 $prodlog; fi
# ---------------------------------------------------
# Search all request for 2 matching text
prodlog="/var/apps/sample-project-name/shared/log/production.log"
OUTPUT1=$(grep -a "logged in device: c17d0784-a1e5-4223-a124-a5935abd1be4" $prodlog | grep -o "........-....-....-....-............" | awk 'ORS="|"' | sed 's/.$//'); if [ -z "$OUTPUT1" ]; then echo "No Request Logs"; else OUTPUT2=$(egrep -an $OUTPUT1 $prodlog | grep "profile sync request" | grep -o "........-....-....-....-............" | awk 'ORS="|"' | sed 's/.$//'); fi; if [ -z "$OUTPUT2" ]; then echo "No Request Logs for 2nd filter"; else egrep -a $OUTPUT2 $prodlog; fi
# ---------------------------------------------------
# HELPER FUNCTION FOR Rails logs search
prodlog="/var/apps/sample-project-name/shared/log/production.log"
sidekiqlog="/var/apps/sample-project-name/shared/log/sidekiq.log"
search_logs () {
searchText=$1
echo $searchText
OUTPUT1=$(grep -a "$searchText" $prodlog | grep -o "........-....-....-....-............" | awk 'ORS="|"' | sed 's/.$//');
if [ -z "$OUTPUT1" ]; then echo "No Request Logs"; else egrep -a $OUTPUT1 $prodlog; fi
}
search_sidekiq_logs () {
searchText=$1
echo $searchText
OUTPUT1=$(grep -a "$searchText" $sidekiqlog | grep -o "........-....-....-....-............" | awk 'ORS="|"' | sed 's/.$//');
if [ -z "$OUTPUT1" ]; then echo "No Request Logs"; else egrep -a $OUTPUT1 $sidekiqlog; fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment