在Rails服务器日志中查看Resque日志输出 [英] View Resque log output in Rails server logs
问题描述
我在Puma服务器上有一个Rails 4应用程序,其中Resque/Resque-Scheduler运行后台作业.我想知道的是如何将两个Resque worker的日志输出合并到服务器日志中,或者,如何将我的Resque worker的日志输出合并到服务器日志中.目前,我无法弄清楚如何查看工人的日志输出,因此我不知道幕后情况.我发现了此博客文章,建议添加以下是我的resque.rake
文件的喜好内容:
I've got a Rails 4 app on a Puma server with Resque/Resque-Scheduler running background jobs. What I'd like to know is how I merge the log output of my two Resque workers into my server log, or, of that is not possible, how I can view the log output of my Resque workers. Currently I have not been able to figure out how to view the log output for the workers, so I have no idea what's happening under the hood. I found this blogpost, which suggests adding the following likes to my resque.rake
file:
task "resque:setup" => :environment do
Resque.before_fork = Proc.new {
ActiveRecord::Base.establish_connection
# Open the new separate log file
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')
# Activate file synchronization
logfile.sync = true
# Create a new buffered logger
Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
Resque.logger.level = Logger::INFO
Resque.logger.info "Resque Logger Initialized!"
}
end
那没有用.我还尝试了注释中的建议,该建议是将Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
替换为Resque.logger = ActiveSupport::Logger.new(logfile)
,但是那也不起作用.使用第二个选项,当我尝试启动工作程序时,仍然出现NoMethodError: undefined method 'logger=' for Resque:Module
错误.
That didn't work. I also tried the suggestion in the comments, which was to replace Resque.logger = ActiveSupport::BufferedLogger.new(logfile)
with Resque.logger = ActiveSupport::Logger.new(logfile)
, however that didn't work either. With the second option, I still get a NoMethodError: undefined method 'logger=' for Resque:Module
error when I try to boot up a worker.
这是我当前的resque.rake
文件:
require 'resque/tasks'
require 'resque_scheduler/tasks'
namespace :resque do
puts "Loading Rails environment for Resque"
task :setup => :environment do
require 'resque'
require 'resque_scheduler'
require 'resque/scheduler'
require 'postman'
end
end
我查看了有关日志记录的文档,但不确定如何使用那里的东西,因为我承认我对登录Rails不太了解.我没有找到有关该主题的其他有用资源的运气.
I've looked at the Resque docs on logging, but am not sure how to use what's there as I admittedly don't know very much about logging in Rails. I haven't had any luck finding other useful resources on the subject.
推荐答案
我如何解决它,它并不完美,但可以正常工作.
How I fix it, it is not perfect but just works.
我的环境:rails 5.0.1,resque:1.26.0
my environment: rails 5.0.1, resque: 1.26.0
在第一时间,我按照大多数文档的建议在config/initializers/resque.rb
中设置了Resque.logger
和Resque.logger.level
:
at the first time, I set the Resque.logger
and Resque.logger.level
in config/initializers/resque.rb
as most docs suggest:
# config/initializers/resque.rb
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::DEBUG
然后在作业中,我通过Resque.logger.info
输出日志:
then in the job, I output log by Resque.logger.info
:
# update_xxx_job.rb
class UpdateXxxJob
def self.perform
Resque.logger.info 'Job starts'
...
end
end
它不起作用,在log/resque.log
中我什么也看不到.
it doesn't work, I can see nothing in log/resque.log
.
then someone said should set the logfile sync always, no buffer, so I update the config/initializers/resque.rb
according a question from stackoverflow:
# config/initializers/resque.rb
logfile = File.open(File.join(Rails.root, 'log', 'resque.log'), 'a')
logfile.sync = true
Resque.logger = ActiveSupport::Logger.new(logfile)
Resque.logger.level = Logger::INFO
仍然不起作用.
我还尝试了lib/tasks/resque.rake
中的config resque logger:
I also tried config resque logger in lib/tasks/resque.rake
:
# lib/tasks/resque.rake
require 'resque'
require 'resque/tasks'
require 'resque/scheduler/tasks'
require 'resque-scheduler'
require 'resque/scheduler/server'
namespace :resque do
task setup: :environment do
Resque.schedule = YAML.load_file(Rails.root + "config/resque_scheduler_#{Rails.env}.yml")
Resque.redis.namespace = "xxx_#{Rails.env}"
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::INFO
end
end
不起作用.
最后,我决定将记录器配置从初始值设定项移至作业,因此作业现在看起来像:
finally, I decide to move the logger configuration from initializer to the job, so the job now looks like:
# update_xxx_job.rb
class UpdateXxxJob
def self.perform
Resque.logger = Logger.new("#{Rails.root}/log/resque.log")
Resque.logger.level = Logger::DEBUG
Resque.logger.info 'Job starts'
...
end
end
然后我可以在log/resque.log
中得到想要的东西.
then I can get what I want in the log/resque.log
.
您可以尝试.
这篇关于在Rails服务器日志中查看Resque日志输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!