I see that I can filter keys from Rails logs here, but it's not entirely clear how I can filter a key that is nested inside the parameter hash.
我看到我可以从Rails日志中过滤键,但是还不完全清楚如何过滤在参数散列中嵌套的键。
My params hash looks like this:
我的params散列是这样的:
{"download"=>{"attachment_id"=>"54039", "data"=>"data:image/png;base64,iVBORw0..."}}
Where params[:download][:data]
is a base64 string. It is a large amount of data and I would like to remove it from my logs.
其中params[:download][:data]是一个base64字符串。这是大量的数据,我想从我的日志中删除它。
Is this possible?
这是可能的吗?
I'm using Rails 4.0.4
我使用Rails 4.0.4
2 个解决方案
#1
4
Simply put this in application.rb:
简单地把这个应用。
config.filter_parameters += [:data]
配置。filter_parameters + =(数据):
This would filter nested [:data] keys also.
这也将过滤嵌套的[:data]键。
In rails 5, you can define hierarchy of the key:
在rails 5中,您可以定义键的层次结构:
config.filter_parameters += ["download.data"]
配置。filter_parameters + =(“download.data”)
This would filter all the [:data] keys which have [:download] as the immediate parent.
这将过滤所有以[:data]键作为父键的[:download]。
#2
0
I guess the most straight-forward way is to monkeypatch Rails code for parameter filtering in your config/initializers/filter_parameter_logging.rb
:
我想最直接的方法是在配置/初始化器/ filter_parameter_logg. rb中使用monkeypatch Rails代码进行参数筛选。
# monkeypatch to filter nested parameters
class ActionDispatch::Http::ParameterFilter::CompiledFilter
def call(original_params, path = [])
filtered_params = {}
original_params.each do |key, value|
if regexps.any? { |r| key =~ r || (path + [key]).join('/') =~ r }
value = ActionDispatch::Http::ParameterFilter::FILTERED
elsif value.is_a?(Hash)
value = call(value, path + [key])
elsif value.is_a?(Array)
value = value.map { |v| v.is_a?(Hash) ? call(v, path + [key]) : v }
elsif blocks.any?
key = key.dup
value = value.dup if value.duplicable?
blocks.each { |b| b.call(key, value) }
end
filtered_params[key] = value
end
filtered_params
end
end
and then:
然后:
Rails.application.config.filter_parameters += ['download/data']
Rails.application.config。filter_parameters + =(“下载/数据”)
#1
4
Simply put this in application.rb:
简单地把这个应用。
config.filter_parameters += [:data]
配置。filter_parameters + =(数据):
This would filter nested [:data] keys also.
这也将过滤嵌套的[:data]键。
In rails 5, you can define hierarchy of the key:
在rails 5中,您可以定义键的层次结构:
config.filter_parameters += ["download.data"]
配置。filter_parameters + =(“download.data”)
This would filter all the [:data] keys which have [:download] as the immediate parent.
这将过滤所有以[:data]键作为父键的[:download]。
#2
0
I guess the most straight-forward way is to monkeypatch Rails code for parameter filtering in your config/initializers/filter_parameter_logging.rb
:
我想最直接的方法是在配置/初始化器/ filter_parameter_logg. rb中使用monkeypatch Rails代码进行参数筛选。
# monkeypatch to filter nested parameters
class ActionDispatch::Http::ParameterFilter::CompiledFilter
def call(original_params, path = [])
filtered_params = {}
original_params.each do |key, value|
if regexps.any? { |r| key =~ r || (path + [key]).join('/') =~ r }
value = ActionDispatch::Http::ParameterFilter::FILTERED
elsif value.is_a?(Hash)
value = call(value, path + [key])
elsif value.is_a?(Array)
value = value.map { |v| v.is_a?(Hash) ? call(v, path + [key]) : v }
elsif blocks.any?
key = key.dup
value = value.dup if value.duplicable?
blocks.each { |b| b.call(key, value) }
end
filtered_params[key] = value
end
filtered_params
end
end
and then:
然后:
Rails.application.config.filter_parameters += ['download/data']
Rails.application.config。filter_parameters + =(“下载/数据”)