在Rails中通过Actionmailer发送电子邮件时,它会记录如下内容:
Sent mail to example@example.com (72ms)
Rendered mailer/_header.html.erb (0.0ms)
...
我想过滤来自日志ala参数过滤的电子邮件
Sent mail to [FILTERED] (72ms)
Rendered mailer/_header.html.erb (0.0ms)
...
有干净的方法吗?或者,不记录整个第一行就没问题。
答案 0 :(得分:1)
在Rails源代码./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没有提供过滤电子邮件的方法,因此您可以:
答案 1 :(得分:0)
您可以为当前的动作邮件进行猴子修补:
使用这样的内容在“lib”目录中创建一个文件(复制并粘贴步骤2中的代码并将“收件人”更改为“[FILTERED]”:
module ActionMailer
class LogSubscriber < ActiveSupport::LogSubscriber
def deliver(event)
return unless logger.info?
#recipients = Array(event.payload[:to]).join(', ')
info("\nSent mail to [FILTERED] (#{event.duration.round(1)}ms)")
debug(event.payload[:mail])
end
end
end
对于我的action_mailer版本,代码如下:
def deliver!(mail = @mail)
raise "no mail object available for delivery!" unless mail
unless logger.nil?
logger.info "Sent mail to [FILTERED]"
# instead of original logger.info "Sent mail to #{Array(recipients).join(', ')}"
logger.debug "\n#{mail.encoded}"
end
begin
__send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
raise e if raise_delivery_errors
end
return mail
end