首页 > 解决方案 > 使用关注点的 Rails 自定义记录器不起作用

问题描述

我有一个看起来像这样的logger_module.rb文件app/lib/concerns/

module Concerns
  module LoggerModule

    def logger(text, logs)
      log_into_file(text, logs)
      log_into_kibana(text)
      # By calling his method, it will log into both Kibana and File
    end

    def log_into_kibana(text)
      tagged_logger ||= Logger.new("log/#{Rails.env}.log")

      tagged_logger.info "[#{self.class}] #{text}"
    end

    def log_into_file(text, logs)
      logs << "#{text}"
    end
  end
end

我正在使用这个 Concerns 将任何内容记录到文件中(可以作为.log文件下载)和 Kibana(用于跟踪/搜索日志)

我正在使用这样的日志记录方法

class ActivateAccount
  include Concerns::LoggerModule

  def perform(logs)
    logger("Processing total of #{accounts.count} accounts", logs)
    # or
    log_into_kibana("Processing total of #{accounts.count} accounts")
    # or
    log_into_file("Processing total of #{accounts.count} accounts", logs)
  end
end

请注意,这logs是一个来自另一个 RequestWorker 类的空数组。

我遇到的问题是,当我在本地运行它时它起作用了(我可以在其中看到它,development.log如果production.log我这样做Logger.new("log/production.log")了,但在生产中,没有任何记录。只有阵列logs在工作。

有人能帮助我吗?我花了将近一个月的时间试图弄清楚这一点。

标签: ruby-on-railsrubyactivesupport

解决方案


推荐阅读