LoginSignup
3
2

More than 3 years have passed since last update.

メモ:ActionMailerのログにメールアドレスが出ないようにする

Last updated at Posted at 2020-01-24

この記事は

  • 意外とぱっとできなかったので個人メモです

やりたかった事

  • ActionMailerでメールを送信する際に、下記のようにログにメールアドレスが出てしまうのですが、セキュリティのレギュレーション上、これを止めたかったです
INFO -- : [ActiveJob] [ActionMailer::DeliveryJob] Sent mail to test@example.com (95.4ms)

やり方

  • Rails側で設定はできません
  • モンキーパッチあてればいい説があり、やってみたのですが、これが全く反映されない
  • 結局、下記のように上書き用のモジュールを作って、ActionMailerが読み込まれたあとでprependしたら反映されました
config/initializers/log_subscriber_ext.rb
ActiveSupport.on_load(:action_mailer) do
  module CustomLogSubscriberFilter
    def deliver(event)
      info do
        recipients = Array(event.payload[:to]).join(", ")
        "Sent mail to [FILTERED] (#{event.duration.round(1)}ms)"
      end

      debug { event.payload[:mail] }
    end
  end
  ActionMailer::LogSubscriber.prepend CustomLogSubscriberFilter
end
  • ちなみにメソッドの中身は/actionmailer-5.2.0/lib/action_mailer/log_subscriber.rbをコピーしてFILTER文字列で潰しただけです
  • Railsのバージョンによって中身が異なるかもしれないので注意

終わりに

  • Web側はfilter_parameter_loggingみたいな仕組みがあるんだから、こっちにも準備して欲しいなー

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2