[英]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__}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.