Rails: How do I print the request parameters?
I would use debug(params)
. That will give you a nicely formatted view of them.
How to log specific request details to rails server logs
I believe most, if not all, of the info you requested can be found in the request headers and response. Information on how to add that to logs has been answered before, but basically you can use an around_filter in the ApplicationController to log the information you care about form the request headers. For example here's how you could log the user agent from the request and status code from the response:
class ApplicationController < ActionController::Base
around_filter :global_request_logging
def global_request_logging
logger.info "USERAGENT: #{request.headers['HTTP_USER_AGENT']}"
begin
yield
ensure
logger.info "response_status: #{response.status}"
end
end
end
As far as getting the format you want, if you wrap calls you can output whatever format you want. Also, the Rails logger is also very customizable, and a few projects already exist that might suit your needs to replace default logging or as inspiration for how to go about creating a useful format:
- lograge - replaces default Rails logging with single line, key-value output, but doesn't yet do request parameters
- scrolls - More generic contextual, key-value
Replay Ruby on Rails logs including parameters and session information?
I am looking at paper_trail to get this kind of functionality in the future. For the app in question, we had to do some heavy-duty log parsing to get the results we needed. It included separating the actions out by IP address (thus similating sessions, although some IP addresses contained multiple user sessions) and parsing the actions and params in the logs. It was not 100% effective at reproducing the exact state of the database, but it was pretty close.
Info about parameters and rendered partials missing from Rails development log file
Check that the Logger
used by ActionController::Base
is the same as the one used for the rest of the app. In the rails console, ActionController::Base.logger == Rails.logger
should be true
.
If it is false
, then the app is somewhere setting up a specific Logger
for ActionController::Base
using something like ActionController::Base.logger = ActiveSupport::TaggedLogging.new(Logger.new('log/controllers.log'))
ruby on rails log file to big - remove params from it
You can set the log level to be less verbose.
See the rails guide on debugging.
So for your entire application (in development), add this to config/environments/development.rb:
config.log_level = :warn # In any environment initializer, or
Or, to change the logging level directly in your application:
Rails.logger.level = 0 # at any time
Filter long log parameters in Rails
I ended up putting something like this in my application.rb
config.filter_parameters << lambda do |k, v|
if k == 'data' && v && v.class == String && v.length > 1024
v.replace('[FILTER]')
end
end
I couldn't find a better way to do this. So I look for the key 'data' in the params. And if the value for that data is a String and over a certain length, I just replace it so the logs isn't so cluttered.
Related Topics
If Java People Go to Scala, C# Go to F#, Where Do Ruby People Go for Functional Nirvana
Generating Unique, Hard-To-Guess "Coupon" Codes
Strip Signatures and Replies from Emails
How to List All Versions of a Gem Available at a Remote Site
Check Whether a String Contains One of Multiple Substrings
Imagemagick 7 with Rmagick 2.16 on MACos Sierra Can't Find Magickwand.H
When to Use Struct Instead of Hash in Ruby
Installing Ruby 1.9.1 on Ubuntu
Stop Rails Console from Printing Out the Object at the End of a Loop
All Ruby Tests Raising: Undefined Method 'Authenticate' for Nil:Nilclass
Ruby Convert Array into Function Arguments
If String Is Empty Then Return Some Default Value
Ruby Metaprogramming Online Tutorial