Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter out email addresses from ActionMailer logs

When sending emails via Actionmailer in Rails, it logs something like:

Sent mail to [email protected] (72ms)
  Rendered mailer/_header.html.erb (0.0ms)
  ...

I would like to filter the emails from logs ala parameter filtering

Sent mail to [FILTERED] (72ms)
  Rendered mailer/_header.html.erb (0.0ms)
  ...

Is there a clean way to do this? Alternatively, not logging the whole first line would be OK.

like image 258
Ross Avatar asked Apr 29 '13 03:04

Ross


1 Answers

In Rails sourcecode ./actionmailer/lib/action_mailer/log_subscriber.rb:

module ActionMailer
  class LogSubscriber < ActiveSupport::LogSubscriber
    def deliver(event)
      return unless logger.info?
      recipients = Array(event.payload[:to]).join(', ')
      info("\nSent mail to #{recipients} (#{event.duration.round(1)}ms)")
      debug(event.payload[:mail])
    end

    def receive(event)
      return unless logger.info?
      info("\nReceived mail (#{event.duration.round(1)}ms)")
      debug(event.payload[:mail])
    end

    def logger
      ActionMailer::Base.logger
    end
  end
end

Rails is not providing a method to filter email, so you can:

  • fork rails, remove this info, and use your forked version of rails.
  • edit this code, add some filter, and make a pull request.
like image 137
linjunhalida Avatar answered Oct 21 '22 08:10

linjunhalida