April 5th, 2011 No Comments »

Logging line numbers in Ruby Logger can be done with a simple decorator:

  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

Pages

Adsense