Rails log过滤

由于采用了active storage log的东西太多,收集一些log 过滤方法

1, 过滤http请求参数

initializers/filter_parameter_logging.rb

1
2
3
4
5
6
7
Rails.application.config.filter_parameters += [
:password,
# Filter ActiveStorage blob keys so that the logs aren't full of as much stuff.
:encoded_key,
:signed_blob_id,
:variation_key
]

或者在environments/ 的环境配置文件里设置

2, 针对controller过滤
由于我采用了lograge ,这里针对lograge说明

config/initializers/lograge.rb

1
2
3
4
5
6
7
8
9
Rails.application.configure do
config.lograge.enabled = true
config.lograge.base_controller_class = 'ActionController::Base'
config.lograge.ignore_actions = [
'ActiveStorage::DiskController#show',
'ActiveStorage::RepresentationsController#show',
'ActiveStorage::BlobsController#show'
]
end

3, overwrite LogSubscriber

前面两种针对了http params和controller过滤,但是ActiveStorage::LogSubscriber
在debug信息里面还有ActiveStorage::Variant的log信息,判断Variant文件是否存在,需不需要生成。
由于我上传的文件名都是经过hash的一大串字符,实在干扰查看log,这里重写过滤了。

1
2
3
4
5
6
7
8
config.after_initialize do
module ActiveStorage
class LogSubscriber
def debug(*args, &block)
end
end
end
end
使用Capistrano自动化部署 如何避免社交网络禁止外国号码使用外国手机号码注册
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×