Output line number in Rails log file

会有一股神秘感。 提交于 2019-12-04 07:48:24
DNNX
logger.debug "Person attributes hash: #{@person.attributes.inspect} from line #{__LINE__}"

Use a decorator on 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

As of Rails 5, this is now baked in via:

ActiveRecord::Base.verbose_query_logs = true

See the documentation for more

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!