如何在Ruby on Rails中为耙任务使用自定义日志? [英] How do I use a custom log for my rake tasks in Ruby on Rails?
问题描述
我有一个rake任务,调用以下函数:
I have a rake task that calls functions like this:
namespace :blah do
task :hello_world => :environment do
logger.info("Hello World")
helloworld2
end
end
def helloworld2
logger.info("Hello Again, World")
end
我希望将日志输出到自定义日志,并且我真的不想每次进行函数调用时都必须传递日志引用.我在某个地方找到了它(无法再次找到它):
I want the log output to a custom log, and I really don't want to have to pass a log reference every time I make a function call. I found this somewhere (can't find it again):
def logger
@@logger ||= Logger.new("#{RAILS_HOME}/log/blah.log")
end
但这对我不起作用,我不确定它能做什么,因为我很久以前就抓取了代码,直到现在才使用它.我无法在Google上搜索@@(尝试+"@@"导轨)来查看其功能.在这个问题上的任何帮助将是巨大的.我希望有一个快速的解决方案,而不必安装gem或插件(除非有确实非常好的理由.
But this does not work for me and I am not sure what it even does because I grabbed the code a long time ago and haven't used it until now. I can't search for @@ on google (tried +"@@" rails) to see what it does. Any help on this issue would be great. I am hoping for a quick solution and not having to install a gem or plugin (unless there is a really really good reason to.
谢谢!
推荐答案
rake禁用生产模式下的日志记录.如果要记录日志,请确保您正在开发模式下运行
rake disables logging in production mode. make sure you're running in development mode if you want it to log
这篇关于如何在Ruby on Rails中为耙任务使用自定义日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!