Filter long log options in Rails

I allow users to upload files to my site. And some of these files can be very large, and it eats up a huge chunk of my log files. Therefore, I would not want him to appear. I know about:

config.filter_parameters += [:password] 

To filter specific parameters. But the problem is that 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 parameters, but this will hide a lot of logs on the whole site, since the data is a shared key (I also can not rename this to something more obscure). Is it possible for filter_parameters to take a nested parameter? Or there is another way to limit the length of all parameters, so if they exceed a certain size, they will not be stored in my log files.

+5
source share
1 answer

I ended up adding something like this in my .rb application

 config.filter_parameters << lambda do |k, v| if k == 'data' && v && v.class == String && v.length > 1024 v.replace('[FILTER]') end end 

I could not find a better way to do this. Therefore, I am looking for key "data" in the parameters. And if the value for this data is a string and over a certain length, I will simply replace it so that the logs are not so cluttered.

+5
source

Source: https://habr.com/ru/post/1205221/


All Articles