簡體   English   中英

Rails日志文件中的輸出行號

[英]Output line number in Rails log file

從有關調試的Rails指南中,我發現可以使用以下簡單方法將輸出自定義到日志文件:

logger.debug "Person attributes hash: #{@person.attributes.inspect}"

我決定使用它來跟蹤變量如何更改並通過流控制。

我希望能夠看到調用logger#debug方法的代碼的行號。 像這樣:

logger.debug "Person attributes hash: #{@person.attributes.inspect} from line #{LINE_NUMBER_VAR}"

在Logger上使用裝飾器:

class LoggerDecorator
  def initialize(logger)
    @logger = logger
  end

  %w{debug info warn error fatal}.each do |method|
    eval(<<-eomethod)
      def #{method}(msg)
        @logger.#{method}(position) {msg}
      end
    eomethod
  end

  private
  def position
    caller.at(1).sub(%r{.*/},'').sub(%r{:in\s.*},'')
  end
end
logger.debug "Person attributes hash: #{@person.attributes.inspect} from line #{__LINE__}"

從Rails 5開始,現在可以通過以下方式進行烘焙:

ActiveRecord::Base.verbose_query_logs = true

有關更多信息,請參見文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM