在Rails中过滤长日志参数 [英] 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'
}
}
}
我可以在过滤器参数中添加:data,但这会在整个站点上隐藏很多日志,因为数据是一个通用键(我也无法将其重命名为更晦涩的名称). filter_parameters是否可以采用嵌套参数?还是有另一种方法来限制所有参数的长度,因此,如果它们的大小大于特定大小,则不会将其存储在我的日志文件中.
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.
推荐答案
我最终将这样的内容放入了我的应用程序.rb
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
我找不到更好的方法来做到这一点.因此,我在参数中寻找关键的数据".而且,如果该数据的值是一个String并且超过一定长度,我将其替换,这样日志就不会太混乱.
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.
这篇关于在Rails中过滤长日志参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!