[英]Log inside Sidekiq worker
I'm trying to log the progress of my sideqik worker using tail -f log/development.log
in development and heroku logs
in production.我正在尝试在开发中使用tail -f log/development.log
并在生产中使用heroku logs
来记录我的 sideqik worker 的进度。
However, everything inside the worker and everything called by the worker does not get logged.但是,worker 内部的所有内容以及 worker 调用的所有内容都不会被记录下来。 In the code below, only TEST 1 gets logged.在下面的代码中,只有 TEST 1 被记录。
How can I log everything inside the worker and the classes the worker calls?我如何记录工作人员内部的所有内容以及工作人员调用的类?
# app/controllers/TasksController.rb
def import_data
Rails.logger.info "TEST 1" # shows up in development.log
DataImportWorker.perform_async
render "done"
end
# app/workers/DataImportWorker.rb
class DataImportWorker
include Sidekiq::Worker
def perform
Rails.logger.info "TEST 2" # does not show up in development.log
importer = Importer.new
importer.import_data
end
end
# app/controllers/services/Importer.rb
class Importer
def import_data
Rails.logger.info "TEST 3" # does not show up in development.log
end
end
Update更新
I still don't understand why Rails.logger.info
or Sidekiq.logger.info
don't log into the log stream. Got it working by replacing Rails.logger.info
with puts
.我仍然不明白为什么Rails.logger.info
或Sidekiq.logger.info
不登录日志 stream。通过将Rails.logger.info
替换为puts
使其工作。
There is a Sidekiq.logger
and simply logger
reference that you can use within your workers.有一个Sidekiq.logger
和简单的logger
参考,您可以在您的工作人员中使用。 The default should be to STDOUT and you should just direct your output in production to the log file path of your choice.默认应为 STDOUT,您应该将生产中的输出定向到您选择的日志文件路径。
@migu,您是否在config/initializer.rb
尝试过以下命令?
Rails.logger = Sidekiq::Logging.logger
It works in rails 6:它适用于 Rails 6:
# config/initializers/sidekiq.rb
Rails.logger = Sidekiq.logger
ActiveRecord::Base.logger = Sidekiq.logger
I've found this solution here , it seems to work well.我在这里找到了这个解决方案,它似乎运作良好。
Sidekiq uses the Ruby Logger class with default Log Level as INFO
, and its settings are independent from Rails. Sidekiq 使用 Ruby Logger class,默认日志级别为INFO
,其设置独立于 Rails。
You may set the Sidekiq Log Level for the Logger
used by Sidekiq in config/initializers/sidekiq.rb
:您可以在config/initializers/sidekiq.rb
中为 Sidekiq 使用的Logger
设置 Sidekiq 日志级别:
Sidekiq.configure_server do |config|
config.logger.level = Rails.logger.level
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.