Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 防止Rails记录电子邮件附件_Ruby On Rails_Logging_Actionmailer - Fatal编程技术网

Ruby on rails 防止Rails记录电子邮件附件

Ruby on rails 防止Rails记录电子邮件附件,ruby-on-rails,logging,actionmailer,Ruby On Rails,Logging,Actionmailer,当我发送带有附件的电子邮件时,数据以十六进制记录,并填满我的整个日志。有没有办法禁用附件的日志记录?我知道我可以在Application.rb中使用config.action\u mailer.logger=nil禁用邮件记录。您可以尝试筛选附件参数。我相信这应该能解决问题,但我自己还没有测试过 config.filter_parameters += [:attachment] 不幸的是,如果日志记录级别设置为非生产环境的默认级别:debug,则日志中会包含附件。这意味着,在生产环境中,您

当我发送带有附件的电子邮件时,数据以十六进制记录,并填满我的整个日志。有没有办法禁用附件的日志记录?我知道我可以在
Application.rb
中使用
config.action\u mailer.logger=nil禁用邮件记录。您可以尝试筛选附件参数。我相信这应该能解决问题,但我自己还没有测试过

config.filter_parameters += [:attachment]  

不幸的是,如果日志记录级别设置为非生产环境的默认级别
:debug
,则日志中会包含附件。这意味着,在生产环境中,您应该很好,但是您的开发和登台环境在测试期间可能会膨胀。您可以关闭整个应用程序的日志记录(
config.log\u level=:info
),但这显然不太理想

您可以配置自定义记录器:

config.action_mailer.logger = ActiveSupport::BufferedLogger.new("mailer.log")
config.action_mailer.logger.level = ActiveSupport::BufferedLogger::Severity::INFO
Rails 4

config.action_mailer.logger = ActiveSupport::Logger.new("mailer.log")
config.action_mailer.logger.level = ActiveSupport::Logger::Severity::INFO

这将拆分日志,但您可以将日志级别更改隔离到操作邮件程序

如果仍希望日志级别为调试,可以通过覆盖ActionMailer的LogSubscriber类从日志输出中删除附件。查看actionmailer gem中的类,并进行相应调整。对于my Rails 4.2.10安装,相关文件为:
gems/actionmailer-4.2.10/lib/action\u mailer/log\u subscriber.rb

我的模块是:

module ActionMailer
  class LogSubscriber < ActiveSupport::LogSubscriber
    def deliver(event)
      info do
        recipients = Array(event.payload[:to]).join(', ')
        "\nSent mail to #{recipients}, Subject: #{event.payload[:subject]}, on #{event.payload[:date]} (#{event.duration.round(1)}ms)"
      end

      debug { remove_attachments(event.payload[:mail]) }
    end

  def remove_attachments(message)
    new_message = ''
    skip = false
    message.each_line do |line|
      new_message << line unless skip
      skip = true if line =~ /Content-Disposition: attachment;/
      skip = false if skip && line =~ /----==_mimepart/
    end
    new_message
  end
 end
end
模块ActionMailer
类LogSubscriber新的\u消息
filter\u parameters
是关于过滤发送到您站点的参数的,不是吗?我的理解是,而且一直以来,filter\u参数都会阻止将该参数打印到日志中,这样您就不会意外地将信用卡号或密码打印到日志文件中。但是,如果我呈现一个mailer视图,我认为它不会显示为GET或POST参数,因为这是朝着另一个方向发展的。在Rails 4中,您应该使用
ActiveSupport::Logger
,因为
ActiveSupport::BufferedLogger
已经贬值()。这正是问题所在!只需稍加改进,即可跳过已编码的附件,但保留所有其他内容,包括边界:
new\u消息