Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Last active July 3, 2016 09:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save huacnlee/8312826 to your computer and use it in GitHub Desktop.
Save huacnlee/8312826 to your computer and use it in GitHub Desktop.
Output request time spend and parameters in log for Grape
require 'grape'
class API < Grape::API
before do
@log_start_t = Time.now
Rails.logger.info " Parameters: #{params.to_hash.except("route_info")}"
end
after do
@log_end_t = Time.now
total_runtime = ((@log_end_t - @log_start_t) * 1000).round(1)
db_runtime = (ActiveRecord::RuntimeRegistry.sql_runtime || 0).round(1)
Rails.logger.info "Completed in #{total_runtime}ms (ActiveRecord: #{db_runtime}ms)"
end
end
@huacnlee
Copy link
Author

huacnlee commented Jan 8, 2014

And then, the output log will be:

Started GET "/api/v1/photos/5323" for 127.0.0.1 at 2014-01-08 14:49:57 +0800
  Parameters: {"id"=>"5323"}
   (5.3ms)  SELECT COUNT(*) FROM `user_like_photos` WHERE `user_like_photos`.`user_id` = 26 AND `user_like_photos`.`photo_id` = 5323
   (14.4ms)  SELECT COUNT(*) FROM `user_favorite_photos` WHERE `user_favorite_photos`.`user_id` = 26 AND `user_favorite_photos`.`photo_id` = 5323
Completed in 24.9ms (ActiveRecord: 19.8ms)

@aserafin
Copy link

created a gem for grape with exact this functionality, check it out: https://rubygems.org/gems/grape_logging

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