Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter long log parameters in Rails

I allow users to upload files on my site. And some of those files can be very large and it eats up a huge chunk of my log files. So I would like to not have it show up. I know about:

config.filter_parameters += [:password]

To filter certain parameters. But the problem with this is that it the parameter is in a hash like this:

{
   :person => { 
      :name => 'bob', 
      :file => { 
         :data => 'really long data. this can be tens of thousands of characters long' 
      }
   }
}

I could add :data to the filter_parameters but that would hiding lots of logs across the whole site since data is a common key (I also can't rename this to something more obscure). Is it possible for filter_parameters to take in a nested parameter? Or is there another way to limit the length of all parameters so if they come in bigger than a certain size, it would not be stored in my log files.

like image 858
Dragonfly Avatar asked Oct 21 '14 19:10

Dragonfly


1 Answers

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.

like image 87
Dragonfly Avatar answered Sep 20 '22 13:09

Dragonfly