在我的 route ,我有一条类似于 GET 'check/:secret
的路线。
当我向日志中的该路由发送请求时,我看到:
Started GET "/check/the-secret-here" for ::1 at 2021-01-14 16:38:01 -0600
...
我想过滤/编辑 secret ,使其看起来像:
Started GET "/check/[FILTERED]" for ::1 at 2021-01-14 16:38:01 -0600
...
我使用的是 Rails 5.1,我添加了 config.filter_parameters += %i[secret]
过滤值,但仅限于 POST 参数。
最佳答案
您描述的不是参数,而是 url 的一部分。
如果您将您的 secret 作为可以在任何地方共享的 url 的一部分公开,可能不会像您期望的那样如此 secret ,所以改变该操作可能是个好主意到 POST 请求?
无论如何,如果有任何充分的理由以这种方式保留它,我唯一能看到的就是猴子修补你的 rails 实例,特别是 ActionDispatch::Http::FilterParameters
.因此,在您的 config/initializers
文件夹中添加:
module ActionDispatch
module Http
module FilterParameters
def filtered_path
# Keep an eye here adding a really good filtering regex, or potentially
# you'll filter more than you were expecting
secret_path = path.gsub(/\/the-secret-here\//, "\/[FILTERED]\/")
@filtered_path ||= query_string.empty? ? secret_path : "#{secret_path}?#{filtered_query_string}"
end
end
end
end
关于ruby-on-rails - 如何在日志记录中过滤部分路径(不是参数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65728056/