简体   繁体   English

我们如何检查日志 output 是否在 Rails 4 中使用 STDOUT?

[英]How can we check if the log output is using STDOUT in Rails 4?

In Rails, we can put this in development.rb if we are outputting the logs in the console:在 Rails 中,如果我们在控制台中输出日志,我们可以将其放在 development.rb 中:

config.logger = ActiveSupport::Logger.new(STDOUT)

So in Rails 5.x we can check if the log output is using STDOUT by using the code below:因此,在 Rails 5.x 中,我们可以使用以下代码检查日志 output 是否使用 STDOUT:

# Returns true if the logger destination matches one of the sources which is STDOUT
ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT)

Here's the documentation: https://github.com/rails/rails/blob/492b9b413d3aa3a9bf3bf986ae25c7f90ec1ab62/activesupport/lib/active_support/logger.rb#L16这是文档: https://github.com/rails/rails/blob/492b9b413d3aa3a9bf3bf986ae25c7f90ec1ab62/activesupport/lib/active_support/logger.rb#L16

My question now is how can we check if the log output is using STDOUT in Rails 4?我现在的问题是我们如何检查日志 output 是否在 Rails 4 中使用 STDOUT? Because this script ActiveSupport::Logger.logger_outputs_to?因为这个脚本ActiveSupport::Logger.logger_outputs_to? is not available in Rails 4.在 Rails 4 中不可用。

Any ideas on this?对此有什么想法吗? Your help would be greatly appreciated.您的帮助将不胜感激。 Thanks.谢谢。

Since the ActiveSupport::Logger.logger_outputs_to?() is not available in Rails 4, I just created a function.由于 ActiveSupport::Logger.logger_outputs_to?() 在 Rails 4 中不可用,我刚刚创建了一个 function。

def self.logger_stdout
   logdev = ::Rails.logger.instance_variable_get(:@logdev)
   logger_source = logdev.dev if logdev.respond_to?(:dev)
   sources = [$stdout]
   found = sources.any? { |source| source == logger_source }
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM