この記事は
- 意外とぱっとできなかったので個人メモです
やりたかった事
- 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みたいな仕組みがあるんだから、こっちにも準備して欲しいなー
参考
- blousonのこのコードが非常に役に立ちました