如何让所有 rake 任务写入同一个日志文件?

发布于 2024-12-20 20:25:58 字数 1108 浏览 2 评论 0原文

我有两个我想每晚运行的 rake 任务。我希望他们记录到一个文件。我认为这可以解决问题(在这里得到它:Rake:记录任何正在执行的任务 ):

application.rb

module Rake
  class Task
    alias_method :origin_invoke, :invoke if method_defined?(:invoke)
    def invoke(*args)
      @logger = Logger.new('rake_tasks_log.log')
      @logger.info "#{Time.now} -- #{name} -- #{args.inspect}"
      origin_invoke(args)
    end
  end
end

然后在 rakefile 中:

task :hello do
  @logger.warn "Starting Hello task"
  puts "Hello World!"
  puts "checking connection "
  checkConnection
  puts "done checking"
  @logger.debug "End hello rake task"
end

但是当我运行任务时,我得到:

private method 'warn' Called for nil:NilClass

我已经尝试了几种对日志记录的调用(@、@@、无@),但均无济于事。阅读此处有关它的几个主题。这 rubyonrails.org 站点 没有提及登录 rake 任务。我调用的任务相当复杂(大约需要 20-40 分钟才能完成),因此如果失败,我真的很想知道出了什么问题。出于 DRY 原因,我更愿意只创建一次 logger 对象。

I have two rake tasks that I'd like to run nightly. I'd like them to log to one file. I thought this would do the trick (got it here: Rake: logging any task executing):

application.rb

module Rake
  class Task
    alias_method :origin_invoke, :invoke if method_defined?(:invoke)
    def invoke(*args)
      @logger = Logger.new('rake_tasks_log.log')
      @logger.info "#{Time.now} -- #{name} -- #{args.inspect}"
      origin_invoke(args)
    end
  end
end

and then in the rakefile:

task :hello do
  @logger.warn "Starting Hello task"
  puts "Hello World!"
  puts "checking connection "
  checkConnection
  puts "done checking"
  @logger.debug "End hello rake task"
end

But when I run the task I get:

private method 'warn' called for nil:NilClass

I've tried a couple of flavors of that call to logging (@, @@, no @) to no avail. Read several threads on here about it. The
rubyonrails.org site doesn't mention logging in rake tasks. The tasks that I'm invoking are fairly complex (about 20-40 mins to complete) so I'll really want to know what went wrong if they fail. I'd prefer for DRY reasons to only create the logger object once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷雨优美回忆 2024-12-27 20:25:58

除非您将所有内容都包装在巨大的开始/救援中并以这种方式捕获错误,否则记录错误的最佳方法是捕获来自 stderr 和 stdout 的所有输出,例如:

rake your:job 2>&1 >> /var/log/rake.log

您还可以将 Rails 环境设置为使用系统记录器。

Unless you're wrapping everything in giant begin/rescue's and catching errors that way, the best way to log errors is to catch all output from stderr and stdout with something like:

rake your:job 2>&1 >> /var/log/rake.log

You could also set your Rails environment to use the system logger as well.

路还长,别太狂 2024-12-27 20:25:58

我最终通过创建“日志”任务并依赖于其他任务来解决这个问题(或者至少足够好)。不太理想,因为这意味着必须在任何新任务中包含该依赖项,但我只有几个任务,所以这会很好。我知道有一个“文件”任务,但它似乎不想在 Windows 中工作,所以我选择这个,因为它似乎更跨平台并且更明确。

我需要一个 logger 对象,因为我将该对象传递到 [...] 部分中的某些方法调用中。那里有足够的开始/救援/结束,写入输出流将不起作用(我认为)。

@log_file =  "log/tasks.log"
directory "log"

task :check_log => ["log"] do
  log = @log_file
  puts 'checking log existence'
  if not FileTest.exists? ("./#{log}")
    puts 'creating log file'
    File.open(log, 'w')
  end
end

task :check_connection  => [:check_log] do
  begin
    conn = Mongo::Connection.new
    [...]
  end
end


task :nightly_tasks => [:check_connection, :environment ] do
  for i in 1..2
     logger.warn "#########################"
  end
  [...]
  logger.warn "nightly tasks complete"
end

def logger
  @@logger ||= Logger.new( File.join(Rails.root, @log_file) )
end

I ended up solving this (or at least well enough) by making a "log" task and depending on that in other tasks. Not really ideal, since that means having to include that dependency in any new task, but I have only a few tasks so this will do fine. I'm aware that there is a "file" task but it didn't seem to want to work in Windows, so I chose this because it seems to be more cross platform and it's more explicit.

I need a logger object because I am passing that object into some method calls in the [...] sections. There's enough begin/rescue/end in there that writing to the output stream wouldn't work (I think).

@log_file =  "log/tasks.log"
directory "log"

task :check_log => ["log"] do
  log = @log_file
  puts 'checking log existence'
  if not FileTest.exists? ("./#{log}")
    puts 'creating log file'
    File.open(log, 'w')
  end
end

task :check_connection  => [:check_log] do
  begin
    conn = Mongo::Connection.new
    [...]
  end
end


task :nightly_tasks => [:check_connection, :environment ] do
  for i in 1..2
     logger.warn "#########################"
  end
  [...]
  logger.warn "nightly tasks complete"
end

def logger
  @@logger ||= Logger.new( File.join(Rails.root, @log_file) )
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文