Rails log过滤

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

1, 过滤http请求参数

initializers/filter_parameter_logging.rb

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

  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,这里重写过滤了。

  config.after_initialize do
      module ActiveStorage
        class LogSubscriber
          def debug(*args, &block)
          end
        end
      end
  end