Rails Log File Line Number

From the Rails Debugging Guide, I found that I can customize the output for my log files using this simple method:

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

I decided to use this to track how a variable changes and passes flow control.

I would like to be able to see the line number of my code where the logger#debug method was called. Something like that:

 logger.debug "Person attributes hash: #{@person.attributes.inspect} from line #{LINE_NUMBER_VAR}" 
+6
source share
3 answers
 logger.debug "Person attributes hash: #{@person.attributes.inspect} from line #{__LINE__}" 
+6
source

Use the 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 
+9
source

Starting with Rails 5, it is now baked through:

 ActiveRecord::Base.verbose_query_logs = true 

See documentation for more

0
source

Source: https://habr.com/ru/post/1301374/


All Articles