0

我的控制器中有一个下载操作,当用户单击下载链接时会记录该操作。有什么方法可以跟踪下载完成或至少成功所需的时间?

这是控制器中的下载操作(Rails 3.2.8):

def download
    send_file @download.attachment.path, :filename => @download.attachment_file_name,
                                         :content_type => @download.attachment_content_type

    DownloadsLog.debug "log details here! -- at #{Time.now}"
end

downloads_log.rb 模型

class DownloadsLog
  def self.debug(message=nil)
    @@downloads_log ||= Logger.new("#{Rails.root}/log/downloads.log", 10, 1024000)
    @@downloads_log.debug(message) unless message.nil?
  end
end

也许这是不可能的,但我想我会问是否有人有任何想法......

谢谢!

4

1 回答 1

1

在 Rails 3 中,将一段代码包装在一个块中,并将其提供给方法benchmark

benchmark "<My identifying label>" do
  # do this and that...
end

在您的日志中,您将看到一行指示您的块执行所用的时间:

<My identifying label> (138.8ms)

您甚至可以在视图中使用它:

<% benchmark "Process data files" do %>
  <%= expensive_files_operation %>
<% end %>
于 2012-11-06T00:58:17.420 回答