Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “logs

Hiding Values from Appearing in Rails Logs

I had some fields that were sensitive, so I didn’t want the values appearing in the logs.  The solution was filtering them.  Doing that was easy.

In config/initializers/filter_parameter_logging.rb, and your fields, like so:

Rails.application.config.filter_parameters += [:password, :secret_field]

Now, when I look at the logs, I see this:

“user”=>{“password”=>”[FILTERED]”, “secret_field”=>”[FILTERED]”

Advertisements

Showing Errors in the Logs

I had some errors occurring in my controller, but it wasn’t displaying in my views.  So I had to figure out how to see what was happening since I didn’t see any messages in the logs that were helpful either.

I found the solution in one of the answers here on Stack Overflow.

By adding this to my controller (it was the creation of a model – @user):

logger.warn("====error==========#{@user.error.full_messages.inspect}============")

This recorded the error in my logs (one of my model validations needed to be changed).


Silencing Asset Pipeline Details in Rails Logs

The asset pipeline details can get quite extensive in Rails logging and since the details you are analyzing typically are not related to the loading of assets, you may want to silence them, or prevent them from logging – especially since you can always turn logging back on if you run into asset troubles.

I’ve found three ways to do so:

  1. you can use the Quiet Assets gem

These other two were mentioned on Stack Overflow, but I was not able to get them to work:

  1. you can add this as an initializer in config/initializers/quiet_assets.rb:
if Rails.env.development?
  Rails.application.assets.logger = Logger.new('/dev/null')
  Rails::Rack::Logger.class_eval do
    def call_with_quiet_assets(env)
      previous_level = Rails.logger.level
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

3.  if you are using Rails 3.2 or above, you can include this setting in config/environments/development.rb:

config.assets.logger = false


Making the Asset Pipeline Less Verbose

I was getting a little overwhelmed by the amount of messages regarding each asset in my development logs, so I wanted to make my logs less verbose.  It was making it more challenging to find the real actions and queries I was trying to analyze.

I found the answer in this post on Stack Overflow.

For some people, adding the initializer code worked, but for me, I went with adding:

config.assets.debug = false

in config/enviroments/development.rb 

and that was sufficient for me.  I’m using Rails 3.1 and I noticed different settings people used in 3.2, so it may also be different if you use 3.2.