在 Rails 中,如果我们在控制台输出日志,我们可以把它放在 development.rb 中:
config.logger = ActiveSupport::Logger.new(STDOUT)
因此,在 Rails 5.x 中,我们可以使用以下代码检查日志输出是否使用 STDOUT:
# Returns true if the logger destination matches one of the sources which is STDOUT
ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT)
这是文档:https : //github.com/rails/rails/blob/492b9b413d3aa3a9bf3bf986ae25c7f90ec1ab62/activesupport/lib/active_support/logger.rb#L16
我现在的问题是我们如何检查日志输出是否在 Rails 4 中使用 STDOUT?因为这个脚本ActiveSupport::Logger.logger_outputs_to? 在 Rails 4 中不可用。
对此有何想法?您的帮助将不胜感激。谢谢。
由于 ActiveSupport::Logger.logger_outputs_to?() 在 Rails 4 中不可用,我刚刚创建了一个函数。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句