How to Log Specific Request Details to Rails Server Logs

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

In Rails, how can one log the entirety of each incoming HTTP request?

I appreciate your response, gunderson, even though it didn't quite lead me to a solution. I ended up using a third party tool, PacketPeeper, to attach to my loopback device and report all TCP traffic.

My issue was caused by an oversight in the client-side networking library I was using (that erroneously reported my XML/JSON as a multipart form).

How do I see the whole HTTP request in Rails

You can rapidly see the request.env in a view via:

  • VIEW: <%= request.env.inspect %>

If instead you want to log it in development log, from your controller:

  • CONTROLLER: Rails.logger.info(request.env)

Here you can see a reference for the Request object.

Rails automatically sets up logging to a file in the log/ directory using Logger from the Ruby Standard Library. The logfile will be named corresponding to your environment, e.g. log/development.log.

To log a message from either a controller or a model, access the Rails logger instance with the logger method:

class YourController < ActionController::Base
def index
logger.info request.env
end
end

About the user, what are you using to authenticate It?

How to log something in Rails in an independent log file?

You can create a Logger object yourself from inside any model. Just pass the file name to the constructor and use the object like the usual Rails logger:

class User < ActiveRecord::Base
def my_logger
@@my_logger ||= Logger.new("#{Rails.root}/log/my.log")
end

def before_save
my_logger.info("Creating user with name #{self.name}")
end
end

Here I used a class attribute to memoize the logger. This way it won't be created for every single User object that gets created, but you aren't required to do that. Remember also that you can inject the my_logger method directly into the ActiveRecord::Base class (or into some superclass of your own if you don't like to monkey patch too much) to share the code between your app's models.

logging info with rails

I just found this railtie gem that might help although I imagine it will take some "custom code" to append username to logs. See the Readme section on logging specific controllers and models.

logging-rails railtie

How to get a copy of the log output of the current request?

You can configure a different logger, once, in your application setup. What that logger does is then entirely within your control, so it can provide clear_buffer and contents_of_buffer methods, it can operate at the :debug level, and it can choose to both record messages in that buffer and pass them along to a second logger (which might be configured for a higher level, and to write to a file as usual).



Related Topics



Leave a reply



Submit